Just A Summary : Javascript scoping makes my head hurt http://www.bofh.org.uk/articles/2008/03/20/javascript-scoping-makes-my-head-hurt.rss en-us 40 Piers Cawley Practices Punditry Comment on Javascript scoping makes my head hurt by Brendan <p>Here&#8217;s how I would have done this sort of thing in Javascript:</p> <div class="typocode"><pre><code class="typocode_javascript ">var subs = []; for (var i in [0,1,2,3,4]) { subs[i] = { execute: function() {return this.i;} }; subs[i].i = i; } alert(subs[0].execute());</code></pre></div> <p>You&#8217;re right, though: Javascript&#8217;s scoping is very often contrary to expectations.</p> Thu, 20 Mar 2008 13:31:29 -0500 urn:uuid:db46cbcf-f74e-43b0-aa66-5d3b4cd9847e http://www.bofh.org.uk/articles/2008/03/20/javascript-scoping-makes-my-head-hurt#comment-1355 Comment on Javascript scoping makes my head hurt by anon I think it&#8217;s perl that&#8217;s being dodgy here, for a &#8216;scripting&#8217; language. The equivalent in python behaves the same as js and ruby: <pre>[lambda: i for i in range(5)][0]() # 4</pre>Note though, if you <strong>use</strong> the value of <code>i</code> in the list comprehension scope rather than keeping the reference alive with the lambda, it acts as you want it to: <pre>[lambda n=i: n for i in range(5)][0]() # 0</pre>A bit of a cheat, but a short way to spell it. Thu, 20 Mar 2008 16:02:40 -0500 urn:uuid:f755c0ac-38ad-4f52-9692-8d253572e549 http://www.bofh.org.uk/articles/2008/03/20/javascript-scoping-makes-my-head-hurt#comment-1357 Comment on Javascript scoping makes my head hurt by Piers Cawley <p>@Brendan: I&#8217;ve always disliked that style &#8211; what&#8217;s the point of manually unpicking a perfectly serviceable closure?</p> Thu, 20 Mar 2008 17:12:28 -0500 urn:uuid:a5687e39-6dbd-4a4e-a6c6-8b020d85df82 http://www.bofh.org.uk/articles/2008/03/20/javascript-scoping-makes-my-head-hurt#comment-1358 Comment on Javascript scoping makes my head hurt by Piers Cawley <p>@anon: Ah, one more reason to maintain my dislike for Python.</p> Thu, 20 Mar 2008 17:17:07 -0500 urn:uuid:55703b4e-d669-4fe8-a513-18bb093d1288 http://www.bofh.org.uk/articles/2008/03/20/javascript-scoping-makes-my-head-hurt#comment-1359 Comment on Javascript scoping makes my head hurt by James Duncan <p>In <a href="http://developer.mozilla.org/en/docs/New_in_JavaScript_1.7" rel="nofollow">JavaScript 1.7</a> you get &#8220;let&#8221; which does what you&#8217;re looking for. Of course that doesn&#8217;t help you in the majority of browsers, but it is a recognized problem.</p> Thu, 20 Mar 2008 19:19:10 -0500 urn:uuid:7920e8ce-9e78-4587-8cce-a1dde583093b http://www.bofh.org.uk/articles/2008/03/20/javascript-scoping-makes-my-head-hurt#comment-1360 Comment on Javascript scoping makes my head hurt by Aristotle Pagaltzis <p>Yeah, scoping is insufficiently thought out in Javascript and downright braindead in Python. At least in Javascript, you can fix this particular example by reformulating it in the same way as the reformulated Ruby example:</p> <pre>var subs = [0,1,2,3,4].map( function(i) { return function() { i }; } );</pre> <p>Of course I’d write this in Perl just like you did in “real Ruby”:</p> <pre>my @subs = map { my $i = $_; sub { $i } } 0 .. 4;</pre> <p>And gosh, here I was annoyed by the verbosity of having to write “<code>sub</code>” in front of every block snippet. Welcome to Javascript which makes you write “<code>function()</code>”!</p> Thu, 20 Mar 2008 19:59:50 -0500 urn:uuid:07141b2f-a02b-428b-86b1-597df3f1cb82 http://www.bofh.org.uk/articles/2008/03/20/javascript-scoping-makes-my-head-hurt#comment-1361 Comment on Javascript scoping makes my head hurt by Piers Cawley <p>Heh. Javascript continues in its mission to turn people onto Lisp (but with extra syntax) eh? Are any of the various Javascript implementations doing tail call optimization yet? It&#8217;d certainly make continuation passing style a good deal easier to manage.</p> Fri, 21 Mar 2008 14:56:20 -0500 urn:uuid:d203d088-2224-495a-8cac-87e5805c2870 http://www.bofh.org.uk/articles/2008/03/20/javascript-scoping-makes-my-head-hurt#comment-1362 Comment on Javascript scoping makes my head hurt by Piers Cawley <p><code>Aristotle: And no way to alias @function</code> either. Giles Bowkett came up with a neat ruby hack:</p> <pre><code>alias :L :lambda</code></pre> <p>Which I find rather delightful.</p> Fri, 21 Mar 2008 15:06:59 -0500 urn:uuid:36649935-e867-492d-b53e-1e8b5d551afa http://www.bofh.org.uk/articles/2008/03/20/javascript-scoping-makes-my-head-hurt#comment-1363 Comment on Javascript scoping makes my head hurt by Aristotle Pagaltzis <p>Ah, then you’ll enjoy <a href="http://search.cpan.org/dist/lambda/lib/lambda.pm" rel="nofollow"><code>lambda</code></a>. <tt>:-)</tt></p> Fri, 21 Mar 2008 18:53:53 -0500 urn:uuid:2bf3c523-189b-48a0-83ec-ba8b401968f1 http://www.bofh.org.uk/articles/2008/03/20/javascript-scoping-makes-my-head-hurt#comment-1364 Comment on Javascript scoping makes my head hurt by Daniel Berger <p>Pssh, Ruby doesn&#8217;t require a third party library:</p> <p><a href="http://www.oreillynet.com/ruby/blog/2007/10/fun_with_unicode_1.html" rel="nofollow">http://www.oreillynet.com/ruby/blog/2007/10/fun_with_unicode_1.html</a></p> Mon, 24 Mar 2008 11:05:01 -0500 urn:uuid:af446739-53f3-4a71-be41-a9ad02d2bda7 http://www.bofh.org.uk/articles/2008/03/20/javascript-scoping-makes-my-head-hurt#comment-1365 Comment on Javascript scoping makes my head hurt by Piers Cawley <p>Daniel: You obviously haven&#8217;t looked at <code>lambda.pm</code> very closely. It&#8217;s not substantially longer than the ruby implementation, but it has some very neat tricks for lexically scoping the &lambda;.</p> <p>Perl&#8217;s conventions for module inclusion and pragma-like modules knock Ruby&#8217;s into a cocked hat, frankly.</p> Mon, 24 Mar 2008 14:13:37 -0500 urn:uuid:8e398cf4-c97f-4c7d-b286-98b3f3435524 http://www.bofh.org.uk/articles/2008/03/20/javascript-scoping-makes-my-head-hurt#comment-1366 Comment on Javascript scoping makes my head hurt by David Cantrell <p>The loop counter is declared outside the for{} block, so to me it makes sense that it could be scoped to the containing block. Perl&#8217;s the odd one out here. I like the way it&#8217;s odd, but it&#8217;s still odd.</p> <p>And, digging into the dusty recesses of my memory, isn&#8217;t that the same as how a for-loop counter would be scoped in C?</p> Tue, 25 Mar 2008 04:00:57 -0500 urn:uuid:ad43f531-8c76-4fa8-bff5-ec70c006a1e0 http://www.bofh.org.uk/articles/2008/03/20/javascript-scoping-makes-my-head-hurt#comment-1367 Comment on Javascript scoping makes my head hurt by Piers Cawley <p>Who cares how it&#8217;s scoped in C? Frankly, the difference in scope only really becomes important/noticeable when your language gets closures. The slight weirdness of the Perl 5 scoping&#8217;s been addressed in Perl 6: It&#8217;s now:</p> <pre><code>foreach @array -&gt; $each { ... }</code></pre> <p>Where <code>-&gt; $whatever { ... }</code> is the new way of declaring parameterized anonymous blocks. I&#8217;m sure it won&#8217;t be long before someone writes a library to let them spell that as <code>λ $foo { ... }</code></p> Tue, 25 Mar 2008 05:26:33 -0500 urn:uuid:72ede2c5-4e41-4f56-bb7b-093101559157 http://www.bofh.org.uk/articles/2008/03/20/javascript-scoping-makes-my-head-hurt#comment-1368