Path: csiph.com!feeder.erje.net!1.eu.feeder.erje.net!weretis.net!feeder1.news.weretis.net!news.solani.org!.POSTED!not-for-mail From: Thomas 'PointedEars' Lahn Newsgroups: comp.lang.javascript Subject: Re: filter queryselectorall Date: Sun, 07 Aug 2016 07:08:02 +0200 Organization: PointedEars Software (PES) Lines: 41 Message-ID: <1546201.XMfzL8yDUt@PointedEars.de> References: <1547678.NO00hCYTYS@PointedEars.de> Reply-To: Thomas 'PointedEars' Lahn Mime-Version: 1.0 Content-Type: text/plain; charset="UTF-8" Content-Transfer-Encoding: 8Bit X-Trace: solani.org 1470546513 25453 eJwFwQkBwDAIA0BL5UlgcqAU/xJ2B6PwhhN0LFZifLcOfDuVXRZfPLEU7Z4azVtoUXuHr9V+IsERTQ== (7 Aug 2016 05:08:33 GMT) X-Complaints-To: abuse@news.solani.org NNTP-Posting-Date: Sun, 7 Aug 2016 05:08:33 +0000 (UTC) User-Agent: KNode/4.14.2 X-NNTP-Posting-Host: eJwNxskNwDAMA7CVJNhWkHHqa/8RGr4YJqqOK+SxsXUOuWB2WacML7DBFfqGZifFt/qYjuofJTgRqQ== X-User-ID: eJwFwYEBgDAIA7CXcNDizsEC/59gAudDZRAMLHY0IRDNPOY1Ph94y/1URtupZcWTryaNq3vV+CSzt3c77Qdf7BYh Xref: csiph.com comp.lang.javascript:31091 Michael Haufe (TNO) wrote: > More like a pin-hammer. This is a general solution to the problem of > manipulating NodeLists, not solving this particular problem. The > alternative formulation, to avoid a potential compilation step for ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ How so? > down-rev environments would be: > > function qsa(s,ctx){return > [].slice.call((ctx||document).querySelectorAll(s))} > > But is a more low-level[1] solution. > >> Instead: >> >> var clickables = [].filter.call( >> document.querySelectorAll('[id^="clickable_"]'), >> function (element) { return /_\d+$/.test(element.id); }); > > Using the form above: > > var clickables = qsa('[id^="clickable_"]').filter(e => /_\d+$/.test(e.id)) Yes, but one problem with converting a NodeList into an Array is that NodeLists are live; Arrays are not. Applying an Array prototype method on a NodeList directly is guaranteed to iterate over all items if matching items are added while the loop is running, or not iterated over if they are removed before iteration has reached them. Another problem is creating an Array instance unnecessarily. (“[].filter” is just the short form. Creation of an Array instance before filtering can be avoided entirely by holding the value of “Array.prototype” or “Array.prototype.filter” in a variable and re-using it as needed.) -- PointedEars FAQ: | SVN: Twitter: @PointedEars2 | ES Matrix: Please do not cc me. / Bitte keine Kopien per E-Mail.