Array.isArray(new Proxy([], {})) should be false (Bug 1096753)

Tom Van Cutsem tomvc.be at gmail.com
Thu Nov 13 13:35:58 PST 2014


2014-11-13 20:31 GMT+01:00 Allen Wirfs-Brock <allen at wirfs-brock.com>:

>
> 3) Proxies. Should Array.isArray treat proxy instances specially? To
> answer this question we need to think about what a ES programmer actually
> thinks Array.isArray means? The meaning that ES5 established is that
> Array.isArray returning true means that the object observably maintains the
> array `length` invariant.
>

Is the `length` invariant really the dominant meaning JS developers
attribute to Array.isArray? I think to most developers Array.isArray(obj)
returning true means that it's safe to call the array utilities (map,
forEach, ...) on obj, not so much that obj.length is special.

My intuition is that Array.isArray is often used to branch based on whether
code received just one versus a collection of values. E.g. a function may
take a single parameter that can be bound to either a single value or a
collection of values, and treat a collection of values differently. In
fact, that is essentially what Array.prototype.concat does: if the argument
is an array, splice its values, otherwise, don't splice. This has nothing
to do with `length` magic. The same goes for JSON.stringify (serialize as
"[]" vs "{}").

Now, if we take the meaning of Array.isArray to be "supports the
Array.prototype utility methods", a proxy-for-array may of course expose a
totally different API, leading a client that expects to be able to use the
Array.prototype methods to fail. But this foregoes the fact that for
virtually all practical use cases of proxies, proxy authors will not do
this. They want to be able to wrap objects, intercept some things, but
mostly faithfully forward those operations to the wrapped target. It would
be rare for a proxy to change the API of the thing it wraps. Indeed, the
whole point of proxies is to be able to intercept operations without
modifying client code.

Regards,
Tom
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.mozilla.org/pipermail/es-discuss/attachments/20141113/4fa440b9/attachment.html>


More information about the es-discuss mailing list