"not a Date object" vs. "not an object with [[Class]] of ''Date''"

Jorge jorge at jorgechamorro.com
Sun Oct 3 05:26:36 PDT 2010


On 02/10/2010, at 22:51, Brendan Eich wrote:
> On Oct 2, 2010, at 6:49 AM, Jorge wrote:
>> 
>> 
>> Why not simply spec an Array.create() ?
>> 
>> -no need to redefine what an array is.
> 
> It's not clear from kangax's blog post that an array with an extra object on its prototype chain before Array.prototype is enough, but if it is, then yes: Array.create is simpler and more direct than using proxies.

Kangax's example [*1] using __proto__ for subclassing is cutting the "extra object" proto chain arbitrarily, for no good reason : he's assuming that the object being passed as the prototype for the subclass (the "extra object") is not an instanceof Array.

But it *must* be an instanceof Array because it *must* have Array.protoype in its prototype chain, so he's inserting it, cutting the proto chain, mutating liberally the provided "extra object".

But it's not exactly nice to mutate the "extra object" in any way. Imagine for a second that Object.create() did the same... :-/

I think that Array.create(protoArray) should do no more than :

1.- check that protoArray is an instanceof Array,
2.- insert it into the [[prototype]] of a new [ ].
3.- return the new [ ].

>> -no need to learn new concepts ( we're used to Object.create() already )
> 
> There's definitely a new concept here. Right now you can't create an array instance whose [[Prototype]] is not some Array.prototype. Array.create changes that.

I meant : in Object.create(protoObject) you provide a protoObject object and obtain a new object whose [[prototype]] is protoObject, In Array.create(protoArray) you provide a protoArray array and obtain a new array whose [[prototype]] is protoArray.

>> -easy to grasp, expected behaviour. 
>> -it's a 3-liner that would take no more than 3 minutes to implement in JS in any current UA.
>> -it would just need to be in the ES specs.
> 
> "3-liner", "3 minutes" and "just" need demonstration. Are you writing the code and spec patches? Talk is cheap :-|.Arrays are highly optimized in modern engines (up to some array sparseness limit). Adding a prototype object shouldn't hurt if the new proto-object contains no indexed properties, though.


Oops, sorry, I apologize. As a *mere*user* from my perspective it seems to be as simple as : 

[ the "show me code" part :-) ]

Array.create= function ArrayCreate (protoArray) {
  if ( !(protoArray instanceof Array) ) throw Error("protoArray must be an instanceof Array");
  var newArray= [ ];
  newArray.__proto__= protoArray;
  return newArray;
}

Cheers,

[*1] The seed for that article is -very likely- in this comp.lang.javascript thread: http://bit.ly/doLX6s
-- 
Jorge.


More information about the es-discuss mailing list