Re: What type should .findAll return

On 11/12/11 10:22 AM, Allen Wirfs-Brock wrote:
> Note that the only specialness of Array instances relates to what happens when you add new array elements or dynamically change the value of the "length" property.

1)  In ES5 this is just not true; there are various parts of the spec 
that check [[Class]].  Yes, I know we're working on getting rid of them, 
but we haven't gotten to that future world yet.

2)  In implementations the above may or may not be true.

> So, if you want the objects to be an immutable, array-like object that inherits from array.prototype through an intermediate prototype there really is no problem.  A JS programmer could express this today in ES5:
>
> var DOMFindResultProto = Object.create(Array.prototype);  //make it inherit from Object.prototype
> DOMFondResultProto.someMethod = function O() { ...};
> //other prototype methods
> //...
>
> function FindResultFactory(nodes) {
>     var obj = Object.create(DOMFindResultProto);
>     for (var i=0; i<nodes.length;++i) obj[i]=nodes[i];
>     return Object.freeze(obj);
> }

The result will not have the same performance characteristics as an 
actual array in many of today's JITs, for what it's worth.  You can 
consider this a bug in those JITs, of course.

-Boris

Received on Saturday, 12 November 2011 09:30:20 UTC