Re: What type should .findAll return

On Nov 12, 2011, at 1:29 AM, Boris Zbarsky wrote:

> 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.

Class related distinctions are covered in the document I reference: https://docs.google.com/document/d/1sSUtri6joyOOh23nVDfMbs1wDS7iDMDUFVVeHeRdSIw/edit?authkey=CI-FopgC and are generally secondary issues related to various library routines.  For example, whether JSON outputs the properties of an object using [ ] or { }. Notation. The only language level semantic specialness of Array is related to the length invarian
> 
> 2)  In implementations the above may or may not be true.

If it isn't the implementation are out of conformance with the standard that applies to them.  That means they are buggy and should be fixed.

> 
>> 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.

It's an expected variance on optimization strategies that I don't think is particularly relevent to this discussion.  BTW, an equally valid statement would be: the result will have the same performance characteristics as an actual array in many of todays JITs that optimize all integer-indexed properties, regardless of whether or not an object is an actual Array instance.

Allen

Received on Saturday, 12 November 2011 17:11:19 UTC