if O is an Array object

Jorge jorge at jorgechamorro.com
Fri Sep 24 15:11:05 PDT 2010


On 24/09/2010, at 23:26, P T Withington wrote:
> On 2010-09-24, at 16:01, Dmitry A. Soshnikov wrote:
> 
>> An object is array (if and only if by the spec) if its [[Class]] is "Array". Even if the object implements all the interface and has all invariants (such as "length" property) of a casual array.
> 
> Which is a bummer.  I've wanted to be able to subclass both Array and String in the past, and you can't.  You can only fake it to a certain extent.

classProto= [];
classProto.method= function () {};
b= [];
b.__proto__= classProto;  // <- That's all that's needed : an Array.create()
b.method
-> function

b is an array:

Array.isArray(b)
-> true

Its .length works:
b.length
-> 0
b.push(27) ; b.length
-> 1
b[2]= 27 ; b
-> [27, undefined, 27]
b.length
-> 3

So b is truly a subclass of Array, not a fake one.
-- 
Jorge.




More information about the es-discuss mailing list