Subclassing an array and array methods
Axel Rauschmayer
axel at rauschma.de
Fri Nov 11 10:52:40 PST 2011
Got it, related to what you solve in generic classes with This-types (covariance vs. contravariance...).
// assume |this| is the source collection
if (this[Array.derivedArrayKey]) A = this[Array.derivedArrayKey](); else A = [ ];
Another possibility:
if (this[Array.derivedArrayKey]) {
A = this[Array.derivedArrayKey]();
} else if (typeof this.constructor === "function") {
A = new this.constructor();
} else {
A = [ ];
}
Creating a subclass this way (i.e., using @derivedArrayKey) produces code whose meaning is more obvious (to me) than the code below.
>>> function createArraySubclass(proto,...values) {
>>> return proto <| [...values].{
>>> [Array.derivedArrayKey](){return proto<| [ ]}
>>> }
>>> }
--
Dr. Axel Rauschmayer
axel at rauschma.de
home: rauschma.de
twitter: twitter.com/rauschma
blog: 2ality.com
More information about the es-discuss
mailing list