W3C home > Mailing lists > Public > public-webapps@w3.org > October to December 2011

Re: What type should .findAll return

From: Boris Zbarsky <bzbarsky@MIT.EDU>
Date: Sat, 12 Nov 2011 22:29:44 +1300
Message-ID: <4EBE3C88.3030602@mit.edu>
To: Allen Wirfs-Brock <allen@wirfs-brock.com>
CC: public-script-coord@w3.org, public-webapps <public-webapps@w3.org>
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 GMT

This archive was generated by hypermail 2.3.1 : Tuesday, 26 March 2013 18:49:48 GMT