Extra functions on Arrays, Maps, etc..
andrea.giammarchi at gmail.com
Mon Apr 9 21:17:13 PDT 2012
I keep finding the frame problem overrated, specially in this case where
the case you are passing DOM nodes between frames is ... well, extremely
If NodeList.prototype is instanceof Array then this check is all you need
before Array.isArray(nl) but generally speaking I agree is never that easy
to understand what's utterable "as if it is" an Array, i.e. live
collections as well such getElementsByTagName()
obj instanceof Array ||
typeof obj.length === "number" &&
~~obj.length === obj.length &&
(typeof obj != "function" || 0 in obj)
) // latter for functions used as ArrayLike objects
so that Arguments, NodeList, LiveCollections, and Arrays, should all pass
this test and we can iterate with common Array.prototype[extras] method
Do you see problems with above checks ? Anything I am not considering ?
On Sun, Apr 8, 2012 at 2:49 AM, Rick Waldron <waldron.rick at gmail.com> wrote:
> On Sat, Apr 7, 2012 at 8:41 PM, Domenic Denicola <
> domenic at domenicdenicola.com> wrote:
>> On Apr 7, 2012, at 20:37, "Rick Waldron" <waldron.rick at gmail.com> wrote:
>> On Sat, Apr 7, 2012 at 8:31 PM, Erik Arvidsson <
>> erik.arvidsson at gmail.com> wrote:
>>> On Sat, Apr 7, 2012 at 17:28, Rick Waldron <waldron.rick at gmail.com>
>>> > As in, its [[Class]] is "Array" and Array.isArray( nodes ) would
>>> evaluate to
>>> > true?
>>> No, it means that Object.getPrototypeOf(NodeList.prototype) ===
>> Yes, this part was clear in the links you sent. The question I had was
>> about two things that were not addressed in the links (that I could see,
>> please correct if I missed them)
>> There will be much confusion on the web if Array.isArray(nodes) returns
>> false, while nodes.push( node ), nodes.slice( 0, 2 ) "just work",
>> especially during periods of transition from DOMx to DOM4.
>>> Fortunately all the Array.prototype methods are generic so it all just
>> I think the benefits are so great I'm happy to deal with any confusion.
>> Although, it sounds like I'll be wanting to go back to `instanceof
>> Array`, which is kind of funny.
> `instanceof Array` is unreliable across contexts (ie. frames) whereas
> Object.prototype.toString.call( thing ) === "[object Array]" is
> irrefutable... That said, I'm sure there is also plenty of code that that
> still expects Object.prototype.toString.call( nodelist ) === "[object
> NodeList]" (as unadvisable as that might be)
> es-discuss mailing list
> es-discuss at mozilla.org
-------------- next part --------------
An HTML attachment was scrubbed...
More information about the es-discuss