W3C home > Mailing lists > Public > public-webapps@w3.org > July to September 2011

Re: [selectors-api] Return an Array instead of a static NodeList

From: Aryeh Gregor <ayg@aryeh.name>
Date: Thu, 25 Aug 2011 14:56:15 -0400
Message-ID: <CAKA+Ax=QAVSznoFEHQaEVaT8-g+bBv6dDj9_VcnCrggZPc=u=w@mail.gmail.com>
To: Jonas Sicking <jonas@sicking.cc>
Cc: Julien Richard-Foy <julien@richard-foy.fr>, public-webapps@w3.org
On Thu, Aug 25, 2011 at 2:33 AM, Jonas Sicking <jonas@sicking.cc> wrote:
> That works, but what is the advantage?

The same advantage as having those methods work for Array.  :)
They're useful for lots of stuff.

> And .push/.pop or other mutating functions wouldn't work.

Right.  I'm only talking about the methods that are already generic
and work with anything that looks like an Array: filter, forEach,
every, etc.

On Thu, Aug 25, 2011 at 3:03 AM, Jonas Sicking <jonas@sicking.cc> wrote:
> On Wed, Aug 24, 2011 at 11:47 PM, Julien Richard-Foy
> <julien@richard-foy.fr> wrote:
>> All mutable functions will work (forEach, map, etc.) and bring a better expressiveness to the code.
>
> Not if he 'this' object is a NodeList.

This works fine right now:

alert(
  [].filter.call(document.querySelectorAll("*"), function(elem) {
return elem.textContent.length > 6 })
  .map(function(elem) { return elem.tagName })
  .join(", ")
);

And I use that pattern a *lot*, but it's both verbose and extremely
unintuitive.  Why can't that be just this?

alert(
  document.querySelectorAll("*")
  .filter(function(elem) { return elem.textContent.length > 6 })
  .map(function(elem) { return elem.tagName })
  .join(", ")
);

Likewise for all Array-like types, but NodeList is the most common.
Received on Thursday, 25 August 2011 18:57:15 GMT

This archive was generated by hypermail 2.3.1 : Tuesday, 26 March 2013 18:49:47 GMT