Path: csiph.com!2.us.feeder.erje.net!feeder.erje.net!1.eu.feeder.erje.net!news.roellig-ltd.de!open-news-network.org!weretis.net!feeder1.news.weretis.net!news.solani.org!.POSTED!not-for-mail From: Thomas 'PointedEars' Lahn Newsgroups: comp.lang.javascript Subject: Re: JSlint finds for loops intolerable Date: Tue, 30 Aug 2016 12:11:36 +0200 Organization: PointedEars Software (PES) Lines: 44 Message-ID: <3919372.LvFx2qVVIh@PointedEars.de> References: Reply-To: Thomas 'PointedEars' Lahn Mime-Version: 1.0 Content-Type: text/plain; charset="UTF-8" Content-Transfer-Encoding: 8Bit X-Trace: solani.org 1472551900 30256 eJwFwQkBwDAIA0BLfKFUDqzEv4TdwVPzO5HIAMEmNsinfkWm6uhqqMWNrVdT0w6oZaeNcP0HGrkQzg== (30 Aug 2016 10:11:40 GMT) X-Complaints-To: abuse@news.solani.org NNTP-Posting-Date: Tue, 30 Aug 2016 10:11:40 +0000 (UTC) User-Agent: KNode/4.14.2 X-User-ID: eJwFwQEBwDAIAzBNjBaonMGHfwlP6GExiWCAy91jwK6MqXk+KXxsvyLdV97xHGpY9rmFjJo03Kkyvg79P1cUxw== Cancel-Lock: sha1:zNjPNaQzNGOaDEaG8nhQ9oG6zQ8= X-NNTP-Posting-Host: eJwFwQEBwDAIAzBLwCjFzjeofwlPcMrrMQuVEBTMG+1I3JZ72HxD7tayT1w+2SjQ2p5w4w8OlxDX Xref: csiph.com comp.lang.javascript:31215 Christoph M. Becker wrote: > Once you get the hang of using Array.prototype.forEach(), you'll > probably find many places where a more specific array method is more > appropriate (for instance, .filter() and map()), and these can simplify > the code (even more). Yes, but: Efficiency issues aside (while the callback is usually compiled only once, there is a *function call* for *each* iteration), one has to be really careful when refactoring loops to calls to iterating Array prototype methods because then in the callback you are in a *function* where things work differently. For example, it will be difficult to impossible to convert a “for” or “while” loop that uses the ”break”, “continue” or “return” statements to *equivalent* callback code: “break” and “continue” are not allowed outside of loops. “return” in a callback is equivalent to a “continue” without label, but there is no elegant equivalent for ”break” (keeping to call the callback and querying a non-local flag so that the callback returns early is _not_ elegant IMO), and “return” returns *from the callback*, _not_ from the function that called the Array method, so there is no elegant equivalent for that either. One also has to deal with the fact that the “this” value in the callback is either the “undefined” value (strict mode) or a reference to the global object (otherwise) by default. In an object’s method that used a loop, you have to pass the “this” value of the method as the second argument of a iterating native Array prototype method, and if you define your own general iteration method, you have to make a provision so that the user can call it like that [or they would have to pass theirCallback.bind(theirThis), which is comparably inefficient and not backwards-compatible]. I encountered these issues last week when I wanted to convert “for” loops in a jQuery-based application to .each() calls, as a jQuery array-like object was to be iterated over and this modification would have produced more uniform code – code that in the end would not have worked. -- PointedEars FAQ: | SVN: Twitter: @PointedEars2 | ES Matrix: Please do not cc me. / Bitte keine Kopien per E-Mail.