Design principles for extending ES object abstractions

Brendan Eich brendan at mozilla.com
Fri Jul 8 18:29:52 PDT 2011


On Jul 8, 2011, at 6:21 PM, Brendan Eich wrote:

> Sometimes people try to make a new Array instance be the prototype of some other object:
> 
> js> var o = Object.create([])
> js> o[2] = 0
> 0
> js> o.length
> 0
> js> o[1] = 1
> 1
> js> o[0] = 2
> 2
> js> o.toString()
> ""
> js> o.slice(0,1)
> []
> js> o.length = 3
> 3
> js> o.slice(0,1) 
> [2]
> js> o.sort(function(a,b){return a-b}).toString()
> "0,1,2"
> js> o.reverse().toString()
> "2,1,0"
> 
> Note the lack of automagic length maintenance in this case.

And the lack of automagic truncation when length is retracted:

js> o.length=0
0
js> o[2]
0
js> o[1]
1
js> o[0]
2


> I think this is all per ES5.


Indeed this all follows from [[DefineOwnProperty]] internal method usage specification. It's considered a drag by people trying to make an array-like, rather than an array with an extended proto chain. I know folks who want the former and currently have to reach for Proxy as a power-tool, but then the economics are noticeably different.

/be
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.mozilla.org/pipermail/es-discuss/attachments/20110708/2b728bbb/attachment.html>


More information about the es-discuss mailing list