<?xml version="1.0" encoding="UTF-8"?><rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
		>
<channel>
	<title>Comments on: Closures in JavaScript</title>
	<atom:link href="http://james.padolsey.com/javascript/closures-in-javascript/feed/" rel="self" type="application/rss+xml" />
	<link>http://james.padolsey.com/javascript/closures-in-javascript/</link>
	<description>Just another WordPress weblog</description>
	<lastBuildDate>Mon, 08 Mar 2010 01:21:27 +0000</lastBuildDate>
	<generator>http://wordpress.org/?v=2.9.2</generator>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
		<item>
		<title>By: Johan</title>
		<link>http://james.padolsey.com/javascript/closures-in-javascript/comment-page-1/#comment-26582</link>
		<dc:creator>Johan</dc:creator>
		<pubDate>Fri, 19 Feb 2010 20:07:03 +0000</pubDate>
		<guid isPermaLink="false">http://james.padolsey.com/?p=1017#comment-26582</guid>
		<description>I&#039;m sorry, I don&#039;t usually flame people on the internet. I&#039;ll make an exception this time :) 

Dusan:

&quot;But I am simply wondering: Why are people reading this blog?&quot;
They find it informing, and as you said, it&#039;s well written. Perhaps you in your great experience could produce something of more interest. 

&quot;Why am I reading a blog of an 18 year old “scripter” ?&quot;
Since when did age become of importance. I have a long time M.sc. in computer science, still I learned something reading this article. Your arrogance is your greatest flaw. 

&quot;And on top of that, all is about one poorly made scripting language … Amazing.&quot;
You said yourself you&#039;re using it alot. What does that make you?</description>
		<content:encoded><![CDATA[<p>I&#8217;m sorry, I don&#8217;t usually flame people on the internet. I&#8217;ll make an exception this time <img src='http://james.padolsey.com/wp-includes/images/smilies/icon_smile.gif' alt=':)' class='wp-smiley' />  </p>
<p>Dusan:</p>
<p>&#8220;But I am simply wondering: Why are people reading this blog?&#8221;<br />
They find it informing, and as you said, it&#8217;s well written. Perhaps you in your great experience could produce something of more interest. </p>
<p>&#8220;Why am I reading a blog of an 18 year old “scripter” ?&#8221;<br />
Since when did age become of importance. I have a long time M.sc. in computer science, still I learned something reading this article. Your arrogance is your greatest flaw. </p>
<p>&#8220;And on top of that, all is about one poorly made scripting language … Amazing.&#8221;<br />
You said yourself you&#8217;re using it alot. What does that make you?</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: James Rourke</title>
		<link>http://james.padolsey.com/javascript/closures-in-javascript/comment-page-1/#comment-24367</link>
		<dc:creator>James Rourke</dc:creator>
		<pubDate>Sun, 15 Nov 2009 23:29:01 +0000</pubDate>
		<guid isPermaLink="false">http://james.padolsey.com/?p=1017#comment-24367</guid>
		<description>It does seem extremely arrogant of them. Initially I thought Dean Edwards was being sarcastic, and the first paragraph would still seem sarcastic if not for the second one. Quite surprising really. Why shouldn&#039;t an 18 year-old kid write about what he thinks closures are about, inviting others to contribute to a discussion? After all, the way we imagine certain things in a programming language (like Objects) is worth sharing with each other.</description>
		<content:encoded><![CDATA[<p>It does seem extremely arrogant of them. Initially I thought Dean Edwards was being sarcastic, and the first paragraph would still seem sarcastic if not for the second one. Quite surprising really. Why shouldn&#8217;t an 18 year-old kid write about what he thinks closures are about, inviting others to contribute to a discussion? After all, the way we imagine certain things in a programming language (like Objects) is worth sharing with each other.</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Guillermo Rauch</title>
		<link>http://james.padolsey.com/javascript/closures-in-javascript/comment-page-1/#comment-24357</link>
		<dc:creator>Guillermo Rauch</dc:creator>
		<pubDate>Sun, 15 Nov 2009 21:55:40 +0000</pubDate>
		<guid isPermaLink="false">http://james.padolsey.com/?p=1017#comment-24357</guid>
		<description>I don&#039;t understand Dean Edwards&#039; or Dusan&#039;s comments. How does the guy age play a role in the analysis of the article? How can you question whether he should be blogging or not when you&#039;re both saying that the information is technically correct?</description>
		<content:encoded><![CDATA[<p>I don&#8217;t understand Dean Edwards&#8217; or Dusan&#8217;s comments. How does the guy age play a role in the analysis of the article? How can you question whether he should be blogging or not when you&#8217;re both saying that the information is technically correct?</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Nicolaj Kirkgaard Nielsen</title>
		<link>http://james.padolsey.com/javascript/closures-in-javascript/comment-page-1/#comment-18617</link>
		<dc:creator>Nicolaj Kirkgaard Nielsen</dc:creator>
		<pubDate>Wed, 19 Aug 2009 13:31:06 +0000</pubDate>
		<guid isPermaLink="false">http://james.padolsey.com/?p=1017#comment-18617</guid>
		<description>When trying to encapsulate the jQuery function like @mark suggested, JSLint gives me an error:

&lt;blockquote&gt;
Problem at line 53 character 3: Wrap the entire immediate function invocation in parens.
})(jQuery);
&lt;/blockquote&gt;

The code looks like:
&lt;pre lang=&quot;javascript&quot;&gt;
(function($){
	$(document).ready(function(){
		// Code...
	});
})(jQuery);
&lt;/pre&gt;

Any idea what JSLint is referring to here?</description>
		<content:encoded><![CDATA[<p>When trying to encapsulate the jQuery function like @mark suggested, JSLint gives me an error:</p>
<blockquote><p>
Problem at line 53 character 3: Wrap the entire immediate function invocation in parens.<br />
})(jQuery);
</p></blockquote>
<p>The code looks like:</p>

<div class="wp_syntax"><div class="code"><pre class="javascript" style="font-family:monospace;"><span class="br0">&#40;</span><span class="kw2">function</span><span class="br0">&#40;</span>$<span class="br0">&#41;</span><span class="br0">&#123;</span>
	$<span class="br0">&#40;</span>document<span class="br0">&#41;</span>.<span class="me1">ready</span><span class="br0">&#40;</span><span class="kw2">function</span><span class="br0">&#40;</span><span class="br0">&#41;</span><span class="br0">&#123;</span>
		<span class="co1">// Code...</span>
	<span class="br0">&#125;</span><span class="br0">&#41;</span><span class="sy0">;</span>
<span class="br0">&#125;</span><span class="br0">&#41;</span><span class="br0">&#40;</span>jQuery<span class="br0">&#41;</span><span class="sy0">;</span></pre></div></div>

<p>Any idea what JSLint is referring to here?</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Martin Kirk</title>
		<link>http://james.padolsey.com/javascript/closures-in-javascript/comment-page-1/#comment-17212</link>
		<dc:creator>Martin Kirk</dc:creator>
		<pubDate>Mon, 27 Jul 2009 12:03:18 +0000</pubDate>
		<guid isPermaLink="false">http://james.padolsey.com/?p=1017#comment-17212</guid>
		<description>you might find that using closures will hunt you after some time...

one major problem with event-handling in JS is &#039;this&#039;, you never can be 100% sure what &#039;this&#039; is :)

ie. you scroll or click on something and want to get the target element - some browsers give you the element by the &#039;this&#039; keyword, others give you the page... 

be careful :)</description>
		<content:encoded><![CDATA[<p>you might find that using closures will hunt you after some time&#8230;</p>
<p>one major problem with event-handling in JS is &#8216;this&#8217;, you never can be 100% sure what &#8216;this&#8217; is <img src='http://james.padolsey.com/wp-includes/images/smilies/icon_smile.gif' alt=':)' class='wp-smiley' /> </p>
<p>ie. you scroll or click on something and want to get the target element &#8211; some browsers give you the element by the &#8216;this&#8217; keyword, others give you the page&#8230; </p>
<p>be careful <img src='http://james.padolsey.com/wp-includes/images/smilies/icon_smile.gif' alt=':)' class='wp-smiley' /> </p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Louis</title>
		<link>http://james.padolsey.com/javascript/closures-in-javascript/comment-page-1/#comment-16853</link>
		<dc:creator>Louis</dc:creator>
		<pubDate>Tue, 21 Jul 2009 14:25:25 +0000</pubDate>
		<guid isPermaLink="false">http://james.padolsey.com/?p=1017#comment-16853</guid>
		<description>Nice article, James.

I just wanted to point out, so as not to confuse beginners, that a closure is not always necessray in the initial example you gave. The closure is only required if you want to access the value of &quot;i&quot; directly. Also, just because &quot;i&quot; is equal to 99 when the object is clicked, does not mean you are clicking on object 99. So if you just need to access the current object, and don&#039;t care about outputting the value of &quot;i&quot;, then you can just use the &lt;code&gt;this&lt;/code&gt; keyword instead:

&lt;pre lang=&quot;javascript&quot;&gt;
var myElements = [ /* DOM Collection */ ];
 
for (var i = 0; i &lt; 100; ++i) {
    myElements[i].onclick = function() {
        alert( &#039;You clicked on: &#039; this.id );
    };
}
&lt;/pre&gt;

So, with the above script, if you had set your elements to have incremental IDs in the HTML, then the alerted value would be equal to the ID of the object clicked. Using &lt;code&gt;this&lt;/code&gt; can also be used to access a number of properties of the current object like &lt;code&gt;this.style&lt;/code&gt; or &lt;code&gt;this.className&lt;/code&gt;, etc. Only if you&#039;re dealing with the variable value itself are you required to use a closure.

Anyhow, thanks for a nice topic of discussion.</description>
		<content:encoded><![CDATA[<p>Nice article, James.</p>
<p>I just wanted to point out, so as not to confuse beginners, that a closure is not always necessray in the initial example you gave. The closure is only required if you want to access the value of &#8220;i&#8221; directly. Also, just because &#8220;i&#8221; is equal to 99 when the object is clicked, does not mean you are clicking on object 99. So if you just need to access the current object, and don&#8217;t care about outputting the value of &#8220;i&#8221;, then you can just use the <code>this</code> keyword instead:</p>

<div class="wp_syntax"><div class="code"><pre class="javascript" style="font-family:monospace;"><span class="kw2">var</span> myElements <span class="sy0">=</span> <span class="br0">&#91;</span> <span class="co2">/* DOM Collection */</span> <span class="br0">&#93;</span><span class="sy0">;</span>
&nbsp;
<span class="kw1">for</span> <span class="br0">&#40;</span><span class="kw2">var</span> i <span class="sy0">=</span> <span class="nu0">0</span><span class="sy0">;</span> i <span class="sy0">&amp;</span>lt<span class="sy0">;</span> <span class="nu0">100</span><span class="sy0">;</span> <span class="sy0">++</span>i<span class="br0">&#41;</span> <span class="br0">&#123;</span>
    myElements<span class="br0">&#91;</span>i<span class="br0">&#93;</span>.<span class="me1">onclick</span> <span class="sy0">=</span> <span class="kw2">function</span><span class="br0">&#40;</span><span class="br0">&#41;</span> <span class="br0">&#123;</span>
        <span class="kw3">alert</span><span class="br0">&#40;</span> <span class="st0">'You clicked on: '</span> <span class="kw1">this</span>.<span class="me1">id</span> <span class="br0">&#41;</span><span class="sy0">;</span>
    <span class="br0">&#125;</span><span class="sy0">;</span>
<span class="br0">&#125;</span></pre></div></div>

<p>So, with the above script, if you had set your elements to have incremental IDs in the HTML, then the alerted value would be equal to the ID of the object clicked. Using <code>this</code> can also be used to access a number of properties of the current object like <code>this.style</code> or <code>this.className</code>, etc. Only if you&#8217;re dealing with the variable value itself are you required to use a closure.</p>
<p>Anyhow, thanks for a nice topic of discussion.</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: zorg</title>
		<link>http://james.padolsey.com/javascript/closures-in-javascript/comment-page-1/#comment-16785</link>
		<dc:creator>zorg</dc:creator>
		<pubDate>Mon, 20 Jul 2009 13:17:05 +0000</pubDate>
		<guid isPermaLink="false">http://james.padolsey.com/?p=1017#comment-16785</guid>
		<description>@James 

&gt; &quot;This behaviour can be used in a number of different ways and has become a useful remedy for quite a few 
&gt; JavaScript gotchas; one of the most common being the “looping problem”.

the Javascript gotcha you mention happens the onclick anonymous function closes over a binding to a mutable variable, it is non intuitive but not incorrect! Intuitively, one expects a new binding to a fresh variable at every iteration, that&#039;s what would happen in a language where immutability is the default.

See http://math.andrej.com/2009/04/09/pythons-lambda-is-broken/ 
and http://lambda-the-ultimate.org/node/2648</description>
		<content:encoded><![CDATA[<p>@James </p>
<p>&gt; &#8220;This behaviour can be used in a number of different ways and has become a useful remedy for quite a few<br />
&gt; JavaScript gotchas; one of the most common being the “looping problem”.</p>
<p>the Javascript gotcha you mention happens the onclick anonymous function closes over a binding to a mutable variable, it is non intuitive but not incorrect! Intuitively, one expects a new binding to a fresh variable at every iteration, that&#8217;s what would happen in a language where immutability is the default.</p>
<p>See <a href="http://math.andrej.com/2009/04/09/pythons-lambda-is-broken/">http://math.andrej.com/2009/04/09/pythons-lambda-is-broken/</a><br />
and <a href="http://lambda-the-ultimate.org/node/2648">http://lambda-the-ultimate.org/node/2648</a></p>
]]></content:encoded>
	</item>
	<item>
		<title>By: zorg</title>
		<link>http://james.padolsey.com/javascript/closures-in-javascript/comment-page-1/#comment-16782</link>
		<dc:creator>zorg</dc:creator>
		<pubDate>Mon, 20 Jul 2009 12:05:57 +0000</pubDate>
		<guid isPermaLink="false">http://james.padolsey.com/?p=1017#comment-16782</guid>
		<description>@Dusan

&gt; I also do use (a lot) javascript and after 15+ years of “real” languages porffesional usage, I came to a 
&gt; conclusion that javascript has its appeal *only* inside HTML. It allows for very quick development of 
&gt; “awesome stuff”, although not always relevant. And of course browser made this combination realy 
&gt; ubiquitous.

Seriously, don&#039;t people ever tire of that &quot;real professional languages&quot; rant ?
Javascript is a cool mix of Scheme and Self (two languages way cooler than most &quot;real&quot; languages.
Javascript is the web lingua franca.
Javascript implementation have upped their games tremendously in the past year, V8 is faster than PHP, CPython, MRI, etc...
Javscript is indeed making inroads outside the browser (http://en.wikipedia.org/wiki/Server-side_JavaScript), coding your whole web app in one language does makes sense, goodbye impedance mismatch</description>
		<content:encoded><![CDATA[<p>@Dusan</p>
<p>&gt; I also do use (a lot) javascript and after 15+ years of “real” languages porffesional usage, I came to a<br />
&gt; conclusion that javascript has its appeal *only* inside HTML. It allows for very quick development of<br />
&gt; “awesome stuff”, although not always relevant. And of course browser made this combination realy<br />
&gt; ubiquitous.</p>
<p>Seriously, don&#8217;t people ever tire of that &#8220;real professional languages&#8221; rant ?<br />
Javascript is a cool mix of Scheme and Self (two languages way cooler than most &#8220;real&#8221; languages.<br />
Javascript is the web lingua franca.<br />
Javascript implementation have upped their games tremendously in the past year, V8 is faster than PHP, CPython, MRI, etc&#8230;<br />
Javscript is indeed making inroads outside the browser (<a href="http://en.wikipedia.org/wiki/Server-side_JavaScript">http://en.wikipedia.org/wiki/Server-side_JavaScript</a>), coding your whole web app in one language does makes sense, goodbye impedance mismatch</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Jani Hartikainen</title>
		<link>http://james.padolsey.com/javascript/closures-in-javascript/comment-page-1/#comment-16755</link>
		<dc:creator>Jani Hartikainen</dc:creator>
		<pubDate>Sun, 19 Jul 2009 23:34:23 +0000</pubDate>
		<guid isPermaLink="false">http://james.padolsey.com/?p=1017#comment-16755</guid>
		<description>I&#039;m not 100% sure but the looping example with the anonymous function may end up leaking memory. The array has references to the elements, and the closure is going to keep a reference to the array, thus creating a circular reference when you assign the function as a handler in an element.

@Dusan why are you reading this blog if you think it&#039;s pointless? :P</description>
		<content:encoded><![CDATA[<p>I&#8217;m not 100% sure but the looping example with the anonymous function may end up leaking memory. The array has references to the elements, and the closure is going to keep a reference to the array, thus creating a circular reference when you assign the function as a handler in an element.</p>
<p>@Dusan why are you reading this blog if you think it&#8217;s pointless? <img src='http://james.padolsey.com/wp-includes/images/smilies/icon_razz.gif' alt=':P' class='wp-smiley' /> </p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Bjoern Wibben</title>
		<link>http://james.padolsey.com/javascript/closures-in-javascript/comment-page-1/#comment-16672</link>
		<dc:creator>Bjoern Wibben</dc:creator>
		<pubDate>Sat, 18 Jul 2009 11:15:38 +0000</pubDate>
		<guid isPermaLink="false">http://james.padolsey.com/?p=1017#comment-16672</guid>
		<description>Hi James,

your blog is great, keep up the good work.

I&#039;m using closures for iteration (simplified example):

&lt;pre lang=&quot;javascript&quot;&gt;
var myarr = [&quot;a&quot;, &quot;b&quot;, &quot;c&quot;];

function iterator(arr) {
	var idx = 0;
	return function () {
		return arr[idx++];
	}
}

var getNext = iterator(myarr);

getNext(); // a
getNext(); // b
getNext(); // c
&lt;/pre&gt;

Best Regards,

Björn</description>
		<content:encoded><![CDATA[<p>Hi James,</p>
<p>your blog is great, keep up the good work.</p>
<p>I&#8217;m using closures for iteration (simplified example):</p>

<div class="wp_syntax"><div class="code"><pre class="javascript" style="font-family:monospace;"><span class="kw2">var</span> myarr <span class="sy0">=</span> <span class="br0">&#91;</span><span class="st0">&quot;a&quot;</span><span class="sy0">,</span> <span class="st0">&quot;b&quot;</span><span class="sy0">,</span> <span class="st0">&quot;c&quot;</span><span class="br0">&#93;</span><span class="sy0">;</span>
&nbsp;
<span class="kw2">function</span> iterator<span class="br0">&#40;</span>arr<span class="br0">&#41;</span> <span class="br0">&#123;</span>
	<span class="kw2">var</span> idx <span class="sy0">=</span> <span class="nu0">0</span><span class="sy0">;</span>
	<span class="kw1">return</span> <span class="kw2">function</span> <span class="br0">&#40;</span><span class="br0">&#41;</span> <span class="br0">&#123;</span>
		<span class="kw1">return</span> arr<span class="br0">&#91;</span>idx<span class="sy0">++</span><span class="br0">&#93;</span><span class="sy0">;</span>
	<span class="br0">&#125;</span>
<span class="br0">&#125;</span>
&nbsp;
<span class="kw2">var</span> getNext <span class="sy0">=</span> iterator<span class="br0">&#40;</span>myarr<span class="br0">&#41;</span><span class="sy0">;</span>
&nbsp;
getNext<span class="br0">&#40;</span><span class="br0">&#41;</span><span class="sy0">;</span> <span class="co1">// a</span>
getNext<span class="br0">&#40;</span><span class="br0">&#41;</span><span class="sy0">;</span> <span class="co1">// b</span>
getNext<span class="br0">&#40;</span><span class="br0">&#41;</span><span class="sy0">;</span> <span class="co1">// c</span></pre></div></div>

<p>Best Regards,</p>
<p>Björn</p>
]]></content:encoded>
	</item>
</channel>
</rss>
