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

Juriy Zaytsev kangax.dev at gmail.com
Fri Oct 1 20:31:48 PDT 2010


On Fri, Oct 1, 2010 at 10:00 PM, Oliver Hunt <oliver at apple.com> wrote:

>
> On Oct 1, 2010, at 6:55 PM, Brendan Eich wrote:
>
> > On Oct 1, 2010, at 6:24 PM, Oliver Hunt wrote:
> >
> >> I really don't like the idea of using proxies simply to essentially
> create an array with a custom prototype.
> >
> > Implement proxies, then we'll talk :-P.
> >
> >
> >> My understanding of what is wanted (in terms of subtyping an array) is
> an ability to have something like
> >>
> >> var arr = new MyArray()
> >>
> >> Where 'new MyArray' behaves in essentially the same manner as 'new
> Array', however it produces an object along the lines of
> >>
> >> <an array instance> <- <a custom prototype> <- <array prototype>
> >
> > I don't think the use-case is satisfied by a direct Array instance,
> whatever its proto. That's the rub.
>
> What part of the use case is not covered?  From reading the blog post
> (which suggests something similar to this) kangax says the problem with this
> solution is the use of the non-standard __proto__, not that it doesn't
> achieve the desired result
>

Object.create([]) doesn't solve the problem of subclassing an array, since
created object is still a plain Object object and so has [[Get]] and [[Put]]
that act like Object's ones, not Array ones.

var o = Object.create([]);
o[2] = 'x';
o.length; /* 0, not 3 as it should be */

o.length = 0;
o[2]; /* "x", not undefined as it should be */

In ES3 (w/o extensions) you can't do much about it, but in ES5 it's possible
to fiddle with length getter/setter to make object act like an array (the
performance of such implementation turns out to be incredibly slow, for
obvious reasons).

So now when you solved "magic" length property (considering that we're in
ES5), and have a plain Object object with [[Prototype]] referencing
Array.prototype, you still have immutable [[Class]]. So Array.isArray would
return false for this pseudo-array object (it could be somehow overwritten
to account for it, but this is kind of getting too far); and then there are
places in spec, where wrong [[Class]] ("Object", not "Array") will make for
a completely different outcome. For example, JSON.stringify:

var o = Object.create([]);
o[2] = 'x';

JSON.stringify(o); // "{"2":"x","length":0}" not [null,null,"x"]

Settable [[Prototype]] helps because you can just create an array object,
then inject something into its prototype chain (right before
Array.prototype). And that extra object can have any methods you wish on
subarray instances — all without polluting global Array.prototype.

[...]

-- 
kangax
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.mozilla.org/pipermail/es-discuss/attachments/20101001/99ad1d14/attachment-0001.html>


More information about the es-discuss mailing list