Extra functions on Arrays, Maps, etc..

Andrea Giammarchi 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
edge?

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 ||
Array.isArray(obj) ||
(
  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 ?

br

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>
>>> wrote:
>>> > As in, its [[Class]] is "Array" and Array.isArray( nodes ) would
>>> evaluate to
>>> > true?
>>>
>>>  No, it means that Object.getPrototypeOf(NodeList.prototype) ===
>>> Array.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.
>>
>>
>>  Rick
>>
>>
>>
>>> Fortunately all the Array.prototype methods are generic so it all just
>>> works.
>>>
>>> --
>>> erik
>>>
>>
>>
>> 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)
>
>
> Rick
>
>
>
> _______________________________________________
> es-discuss mailing list
> es-discuss at mozilla.org
> https://mail.mozilla.org/listinfo/es-discuss
>
>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.mozilla.org/pipermail/es-discuss/attachments/20120410/ca1e7dc1/attachment-0001.html>


More information about the es-discuss mailing list