Subclassing an array and array methods

Allen Wirfs-Brock allen at
Fri Nov 11 08:33:53 PST 2011

My intent is to use a private name property to allow an object to provide a constructor for new "arrays" derived" from it.  something along the lines of:

function createArraySubclass(proto,...values) {
   return proto <| [...values].{
         [Array.derivedArrayKey](){return proto<| [ ]}

The built-ins would create the derived collection by doing the equivalent of:

    // assume |this| is the source collection
    if (this[Array.derivedArrayKey]) A = this[Array.derivedArrayKey](); else A = [ ];

I can't just take the [[Prototype]] of the source array because that would introduce an incompatibility with existing code that uses these functions on non-Array objects.  Also if we are generalizing these functions we shouldn't assume that all collections will what their derived collections to be "subclasses" of Array.  They might want to allocate something else entirely.


On Nov 11, 2011, at 6:08 AM, Jake Verbaten wrote:

> In es-next we should be able to subclass an array
>> function createArraySubclass(proto, ...values) {
>>   return proto <| [...values]; 
>> }
> However when we call `instanceOfSubArray.filter(...)` this method returns a new Array rather then a new SubArray.
> It would seem frustrating to have to overwrite all array methods that return arrays and make them return subarrays.
> Can we have array methods returning new instance of [[Prototype]] to make it easier to overwrite the build in?
> However this should only apply to true array subclasses. In the above example I would assume the [[Class]] of the object is Array. It should not apply to []
> Do we have other situations where it would make sense to return an instance of whatever prototype your operation on rather then a hardcoded new X ?
> _______________________________________________
> es-discuss mailing list
> es-discuss at

-------------- next part --------------
An HTML attachment was scrubbed...
URL: <>

More information about the es-discuss mailing list