if O is an Array object

Jorge jorge at jorgechamorro.com
Sat Sep 25 04:23:51 PDT 2010


On 25/09/2010, at 08:39, Brendan Eich wrote:
> On Sep 25, 2010, at 1:51 AM, Jorge wrote:
>> On 25/09/2010, at 01:28, P T Withington wrote:
>>> On 2010-09-24, at 18:11, Jorge wrote:
>>> 
>>>> So b is truly a subclass of Array, not a fake one.
>>> 
>>> I stand corrected.  At some time in the past, this did not work for me.  Apparently things have improved!
>> 
>> But you are right too: neither .__proto__ nor Array.create() are in the specs, so there's no way to subclass an Array in ES5.
> 
> ES5 supports initializing __proto__ on a new object, just not setting the prototype of an object after it has been created:
> 
> var arrayLike = Object.create([]);

But that's exactly what P T Withington was calling a fake array. To make it a true Array you'd need ES5 to have an Array.create() or .__proto__, because :

a= Object.create([]) ; a[2]=27 ; [a.length, a]
-> [ 0, [undefined, undefined, 27] ]

And:
a.length= 1 ; [a.length, a]
-> [ 1, [undefined, undefined, 27] ]

A true Array.create() would be:

function Array.create (proto) {

 if ( !(proto instanceof Array)) {
   throw Error("Proto must necessarily be an instance of Array");
 }

 var newArray= []; // <- get the special [[ put ]]
 newArray.__proto__= proto;
 return newArray;
}

That, or an inheritable special [[ put ]], as you have said before:

https://mail.mozilla.org/pipermail/es-discuss/2009-December/010424.html
-- 
Jorge.


More information about the es-discuss mailing list