Array.prototype.slice web-compat issue?

Andrea Giammarchi andrea.giammarchi at gmail.com
Tue Sep 10 11:12:55 PDT 2013


As short answer, let me reformulate:
AFAIK 99% of libraries out there believe that slice.call will return an
Array, no matter which realm. In ES6 I see there's too much magic involved
because of subclassing possibility.


Long story:

Historically, `instanceof` has been cross browser/engine the fastest way to
understand if an object is an Array, compared with both
`Object#toString.call(arr)` and/or `Array.isArray` plus the web does not
really pass Arrays between iframes anymore thanks to `postMessage` and
other serialization mechanisms where, once deserialized, `instanceof` will
do the trick (also JSON.parse creates Arrays of the current realm)

Caja is a very-very-very specific use case that does not reflect majority
of websites so `instanceof` worked most of the time for most of the site
but regardless, Caja believes slice.call wil generate an `Array` that will
be `instanceof Array` and this is a valid assumption for every sandboxed
code that believes in `instanceof` rejecting all external "unknown"
instances.

Finally, `Object.prototype.toString.call(object)` instead of `instanceof`
is abusing a hack that is also slow ... subclassing is not only about
`Array` .. or is it? 'cause in such case I'd rather suggest a new
constructor called `ArrayLike` or `ArrayObject`, easier to polyfill and
ready to be subclassed with all the needed magic (as reflection of `Array`
behavior)

Off Topic:
I've personally tried to subclass Array since IE5 so this is a welcome
thing but does not seem to be that practical as specced now considering
existing code.




On Tue, Sep 10, 2013 at 10:46 AM, Allen Wirfs-Brock
<allen at wirfs-brock.com>wrote:

>
> On Sep 10, 2013, at 10:37 AM, Andrea Giammarchi wrote:
>
> > AFAIK 99% of libraries out there believe that slice.call will return an
> Array of the current realm.
>
> Can you explain why the Realm makes a difference to them?  Is it because
> they are doing instanceof Array tests rather than Array.isArray?
>
> Allen
>
>
>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.mozilla.org/pipermail/es-discuss/attachments/20130910/ac886bbe/attachment.html>


More information about the es-discuss mailing list