<?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"
	>
<channel>
	<title>Comments on: Explaining JavaScript scope and closures</title>
	<atom:link href="http://www.robertnyman.com/2008/10/09/explaining-javascript-scope-and-closures/feed/" rel="self" type="application/rss+xml" />
	<link>http://www.robertnyman.com/2008/10/09/explaining-javascript-scope-and-closures/</link>
	<description>Web development and Internet trends</description>
	<pubDate>Tue, 06 Jan 2009 08:21:22 +0000</pubDate>
	<generator>http://wordpress.org/?v=2.6.1</generator>
		<item>
		<title>By: JavaScript timers - using functions and scope &#124; How2Pc</title>
		<link>http://www.robertnyman.com/2008/10/09/explaining-javascript-scope-and-closures/#comment-491380</link>
		<dc:creator>JavaScript timers - using functions and scope &#124; How2Pc</dc:creator>
		<pubDate>Sat, 27 Dec 2008 20:44:38 +0000</pubDate>
		<guid isPermaLink="false">http://www.robertnyman.com/?p=881#comment-491380</guid>
		<description>[...] Explaining JavaScript scope and closures [...]</description>
		<content:encoded><![CDATA[<p>[...] Explaining JavaScript scope and closures [...]</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Robert Nyman</title>
		<link>http://www.robertnyman.com/2008/10/09/explaining-javascript-scope-and-closures/#comment-490131</link>
		<dc:creator>Robert Nyman</dc:creator>
		<pubDate>Thu, 25 Dec 2008 22:33:51 +0000</pubDate>
		<guid isPermaLink="false">http://www.robertnyman.com/?p=881#comment-490131</guid>
		<description>Internet Marketing, Jeff,

Glad that it helped!</description>
		<content:encoded><![CDATA[<p>Internet Marketing, Jeff,</p>
<p>Glad that it helped!</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Jeff</title>
		<link>http://www.robertnyman.com/2008/10/09/explaining-javascript-scope-and-closures/#comment-489892</link>
		<dc:creator>Jeff</dc:creator>
		<pubDate>Thu, 25 Dec 2008 04:59:28 +0000</pubDate>
		<guid isPermaLink="false">http://www.robertnyman.com/?p=881#comment-489892</guid>
		<description>Thanks for this article,  i'm working on a javascript project full of scope issues and closures, very usefull page to keep mind clear ;-)</description>
		<content:encoded><![CDATA[<p>Thanks for this article,  i&#8217;m working on a javascript project full of scope issues and closures, very usefull page to keep mind clear <img src='http://www.robertnyman.com/wp-includes/images/smilies/icon_wink.gif' alt=';-)' class='wp-smiley' /></p>
]]></content:encoded>
	</item>
	<item>
		<title>By: JavaScript timers - using functions and scope - Robert&#8217;s talk - Web development and Internet trends</title>
		<link>http://www.robertnyman.com/2008/10/09/explaining-javascript-scope-and-closures/#comment-486296</link>
		<dc:creator>JavaScript timers - using functions and scope - Robert&#8217;s talk - Web development and Internet trends</dc:creator>
		<pubDate>Tue, 16 Dec 2008 12:07:25 +0000</pubDate>
		<guid isPermaLink="false">http://www.robertnyman.com/?p=881#comment-486296</guid>
		<description>[...] Explaining JavaScript scope and closures   Posted in Developing, JavaScript, Technology &#124; Share your thoughts [...]</description>
		<content:encoded><![CDATA[<p>[...] Explaining JavaScript scope and closures   Posted in Developing, JavaScript, Technology | Share your thoughts [...]</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Internet Marketing</title>
		<link>http://www.robertnyman.com/2008/10/09/explaining-javascript-scope-and-closures/#comment-482069</link>
		<dc:creator>Internet Marketing</dc:creator>
		<pubDate>Thu, 04 Dec 2008 16:09:29 +0000</pubDate>
		<guid isPermaLink="false">http://www.robertnyman.com/?p=881#comment-482069</guid>
		<description>Cool. I am working on a projec requiring clear mind of how JS module works. It helps a lot. 

Thanks</description>
		<content:encoded><![CDATA[<p>Cool. I am working on a projec requiring clear mind of how JS module works. It helps a lot. </p>
<p>Thanks</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: JavaScript inheritance - experimenting with syntax alternatives and private variables - Robert&#8217;s talk - Web development and Internet trends</title>
		<link>http://www.robertnyman.com/2008/10/09/explaining-javascript-scope-and-closures/#comment-466844</link>
		<dc:creator>JavaScript inheritance - experimenting with syntax alternatives and private variables - Robert&#8217;s talk - Web development and Internet trends</dc:creator>
		<pubDate>Wed, 22 Oct 2008 14:17:44 +0000</pubDate>
		<guid isPermaLink="false">http://www.robertnyman.com/?p=881#comment-466844</guid>
		<description>[...] reading this article, I strongly recommend reading JavaScript inheritance - how and why and Explaining JavaScript scope and closures first, since many phenomenon below will have their explanation [...]</description>
		<content:encoded><![CDATA[<p>[...] reading this article, I strongly recommend reading JavaScript inheritance - how and why and Explaining JavaScript scope and closures first, since many phenomenon below will have their explanation [...]</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: JavaScript: how to get private, privileged, public and static members (properties and methods) - Robert&#8217;s talk - Web development and Internet trends</title>
		<link>http://www.robertnyman.com/2008/10/09/explaining-javascript-scope-and-closures/#comment-461715</link>
		<dc:creator>JavaScript: how to get private, privileged, public and static members (properties and methods) - Robert&#8217;s talk - Web development and Internet trends</dc:creator>
		<pubDate>Tue, 14 Oct 2008 11:29:11 +0000</pubDate>
		<guid isPermaLink="false">http://www.robertnyman.com/?p=881#comment-461715</guid>
		<description>[...] reading JavaScript inheritance - how and why and Explaining JavaScript scope and closures, I thought we&#8217;d combine the knowledge gained to talk about private, privileged, public and [...]</description>
		<content:encoded><![CDATA[<p>[...] reading JavaScript inheritance - how and why and Explaining JavaScript scope and closures, I thought we&#8217;d combine the knowledge gained to talk about private, privileged, public and [...]</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Robert Nyman</title>
		<link>http://www.robertnyman.com/2008/10/09/explaining-javascript-scope-and-closures/#comment-460801</link>
		<dc:creator>Robert Nyman</dc:creator>
		<pubDate>Mon, 13 Oct 2008 18:39:42 +0000</pubDate>
		<guid isPermaLink="false">http://www.robertnyman.com/?p=881#comment-460801</guid>
		<description>Bleyder,

Great!

Andrew,

Thank you! And absolutely, I can relate how developers coming from C++ or similar can have a hard time with some concepts, so I hope this is helpful for those as well!</description>
		<content:encoded><![CDATA[<p>Bleyder,</p>
<p>Great!</p>
<p>Andrew,</p>
<p>Thank you! And absolutely, I can relate how developers coming from C++ or similar can have a hard time with some concepts, so I hope this is helpful for those as well!</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Andrew Noyes</title>
		<link>http://www.robertnyman.com/2008/10/09/explaining-javascript-scope-and-closures/#comment-460450</link>
		<dc:creator>Andrew Noyes</dc:creator>
		<pubDate>Mon, 13 Oct 2008 13:17:54 +0000</pubDate>
		<guid isPermaLink="false">http://www.robertnyman.com/?p=881#comment-460450</guid>
		<description>Great article! This would have saved my ass on a project just a couple of months ago. The closure bit has caused numerous problems for me and most of the solutions I found on the internet were either half-assed or explained nothing, which left me with a viable solution but defenseless for the next time I ran into that problem. Thanks!

Ironically, there are many C or Java programmers that are well educated but use poor form. My C++ professor in college taught us all kinds of bad practices, like using global variables and &lt;code&gt;system("PAUSE");&lt;/code&gt; instead of &lt;code&gt;cin.ignore();&lt;/code&gt; or some other less intrusive object. JavaScript is so much easier to work with if you coming from a strong C++ background where you are mindful of C++'s built-in precautions against scope problems that are essential to OOP, because JavaScript lacks any such thing, being a loosely typed language. It's easy to fall into sinkholes when beginning with JavaScript.</description>
		<content:encoded><![CDATA[<p>Great article! This would have saved my ass on a project just a couple of months ago. The closure bit has caused numerous problems for me and most of the solutions I found on the internet were either half-assed or explained nothing, which left me with a viable solution but defenseless for the next time I ran into that problem. Thanks!</p>
<p>Ironically, there are many C or Java programmers that are well educated but use poor form. My C++ professor in college taught us all kinds of bad practices, like using global variables and <code>system("PAUSE");</code> instead of <code>cin.ignore();</code> or some other less intrusive object. JavaScript is so much easier to work with if you coming from a strong C++ background where you are mindful of C++&#8217;s built-in precautions against scope problems that are essential to OOP, because JavaScript lacks any such thing, being a loosely typed language. It&#8217;s easy to fall into sinkholes when beginning with JavaScript.</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Bleyder</title>
		<link>http://www.robertnyman.com/2008/10/09/explaining-javascript-scope-and-closures/#comment-460327</link>
		<dc:creator>Bleyder</dc:creator>
		<pubDate>Mon, 13 Oct 2008 11:16:54 +0000</pubDate>
		<guid isPermaLink="false">http://www.robertnyman.com/?p=881#comment-460327</guid>
		<description>Thanks for this article.

I'm begining to undertand a little more how works JavaScript.</description>
		<content:encoded><![CDATA[<p>Thanks for this article.</p>
<p>I&#8217;m begining to undertand a little more how works JavaScript.</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Weekly Links #22 &#124; GrantPalin.com</title>
		<link>http://www.robertnyman.com/2008/10/09/explaining-javascript-scope-and-closures/#comment-459514</link>
		<dc:creator>Weekly Links #22 &#124; GrantPalin.com</dc:creator>
		<pubDate>Sun, 12 Oct 2008 19:38:22 +0000</pubDate>
		<guid isPermaLink="false">http://www.robertnyman.com/?p=881#comment-459514</guid>
		<description>[...] Explaining JavaScript scope and closures JavaScript handles scope somewhat differently from other common programming and scripting languages, and it also has an interesting capability for creating and using closures. Robert Nyman explains the concepts clearly. [...]</description>
		<content:encoded><![CDATA[<p>[...] Explaining JavaScript scope and closures JavaScript handles scope somewhat differently from other common programming and scripting languages, and it also has an interesting capability for creating and using closures. Robert Nyman explains the concepts clearly. [...]</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Robert Nyman</title>
		<link>http://www.robertnyman.com/2008/10/09/explaining-javascript-scope-and-closures/#comment-457635</link>
		<dc:creator>Robert Nyman</dc:creator>
		<pubDate>Fri, 10 Oct 2008 17:59:15 +0000</pubDate>
		<guid isPermaLink="false">http://www.robertnyman.com/?p=881#comment-457635</guid>
		<description>Jeff,

Thank you!
Glad that it helped you as well!</description>
		<content:encoded><![CDATA[<p>Jeff,</p>
<p>Thank you!<br />
Glad that it helped you as well!</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Jeff L</title>
		<link>http://www.robertnyman.com/2008/10/09/explaining-javascript-scope-and-closures/#comment-457617</link>
		<dc:creator>Jeff L</dc:creator>
		<pubDate>Fri, 10 Oct 2008 17:43:49 +0000</pubDate>
		<guid isPermaLink="false">http://www.robertnyman.com/?p=881#comment-457617</guid>
		<description>Thanks for a great article Robert. I also missed the (i) at the end of the onclick function and was wondering how num = i &#8212;makes sense now that you pointed it out, though. 

I also appreciate you using an example that most of us have problem run into (where i was not what we expected, but instead was the last known value). I think that makes a big difference in helping folks to understand.</description>
		<content:encoded><![CDATA[<p>Thanks for a great article Robert. I also missed the (i) at the end of the onclick function and was wondering how num = i &mdash;makes sense now that you pointed it out, though. </p>
<p>I also appreciate you using an example that most of us have problem run into (where i was not what we expected, but instead was the last known value). I think that makes a big difference in helping folks to understand.</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Robert Nyman</title>
		<link>http://www.robertnyman.com/2008/10/09/explaining-javascript-scope-and-closures/#comment-457471</link>
		<dc:creator>Robert Nyman</dc:creator>
		<pubDate>Fri, 10 Oct 2008 15:30:21 +0000</pubDate>
		<guid isPermaLink="false">http://www.robertnyman.com/?p=881#comment-457471</guid>
		<description>Chris,

Thanks! :-)
I'm glad that you understood it!</description>
		<content:encoded><![CDATA[<p>Chris,</p>
<p>Thanks! <img src='http://www.robertnyman.com/wp-includes/images/smilies/icon_smile.gif' alt=':-)' class='wp-smiley' /><br />
I&#8217;m glad that you understood it!</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Chris</title>
		<link>http://www.robertnyman.com/2008/10/09/explaining-javascript-scope-and-closures/#comment-457402</link>
		<dc:creator>Chris</dc:creator>
		<pubDate>Fri, 10 Oct 2008 14:11:20 +0000</pubDate>
		<guid isPermaLink="false">http://www.robertnyman.com/?p=881#comment-457402</guid>
		<description>It does clarify indeed! What I oversaw was just the parantheses in the end:
&lt;code&gt;}&lt;strong&gt;(i)&lt;/strong&gt;;&lt;/code&gt;

Thanks for explaining and for writing so much good stuff!</description>
		<content:encoded><![CDATA[<p>It does clarify indeed! What I oversaw was just the parantheses in the end:<br />
<code>}<strong>(i)</strong>;</code></p>
<p>Thanks for explaining and for writing so much good stuff!</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Robert Nyman</title>
		<link>http://www.robertnyman.com/2008/10/09/explaining-javascript-scope-and-closures/#comment-457391</link>
		<dc:creator>Robert Nyman</dc:creator>
		<pubDate>Fri, 10 Oct 2008 13:58:02 +0000</pubDate>
		<guid isPermaLink="false">http://www.robertnyman.com/?p=881#comment-457391</guid>
		<description>Andreas,

Thanks!

Chris,

Thank you!
The key is that the function is self-invoked, i.e. gets executed right away, and sends in the value &lt;code&gt;i&lt;/code&gt; to be the value of &lt;code&gt;num&lt;/code&gt;.

If you examine that part of the code more closely:

&lt;code&gt;link.onclick = function (num) {
			return function () {
				alert(num);
			};
		}(i);&lt;/code&gt;

you can see that it is not just assigned, but actually gets called immediately by having two parentheses at the end, with &lt;code&gt;i&lt;/code&gt; in them.

And since inner functions will always have access to their outer/containing functions' variables (i.e. a closure is created), it will always know that &lt;code&gt;num&lt;/code&gt; is the value of &lt;code&gt;i&lt;/code&gt; when it was initially called.

I hope it's clearer now! :-)</description>
		<content:encoded><![CDATA[<p>Andreas,</p>
<p>Thanks!</p>
<p>Chris,</p>
<p>Thank you!<br />
The key is that the function is self-invoked, i.e. gets executed right away, and sends in the value <code>i</code> to be the value of <code>num</code>.</p>
<p>If you examine that part of the code more closely:</p>
<p><code>link.onclick = function (num) {<br />
			return function () {<br />
				alert(num);<br />
			};<br />
		}(i);</code></p>
<p>you can see that it is not just assigned, but actually gets called immediately by having two parentheses at the end, with <code>i</code> in them.</p>
<p>And since inner functions will always have access to their outer/containing functions&#8217; variables (i.e. a closure is created), it will always know that <code>num</code> is the value of <code>i</code> when it was initially called.</p>
<p>I hope it&#8217;s clearer now! <img src='http://www.robertnyman.com/wp-includes/images/smilies/icon_smile.gif' alt=':-)' class='wp-smiley' /></p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Chris</title>
		<link>http://www.robertnyman.com/2008/10/09/explaining-javascript-scope-and-closures/#comment-457363</link>
		<dc:creator>Chris</dc:creator>
		<pubDate>Fri, 10 Oct 2008 13:25:36 +0000</pubDate>
		<guid isPermaLink="false">http://www.robertnyman.com/?p=881#comment-457363</guid>
		<description>Thank you for that article. Made some things clear. But I have one question:
In your addLinks-example, why does link.onclick know that num gets the
value of i and not something else?
It's not the first time I see this and don't understand ?-)</description>
		<content:encoded><![CDATA[<p>Thank you for that article. Made some things clear. But I have one question:<br />
In your addLinks-example, why does link.onclick know that num gets the<br />
value of i and not something else?<br />
It&#8217;s not the first time I see this and don&#8217;t understand ?-)</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Andreas Rydberg</title>
		<link>http://www.robertnyman.com/2008/10/09/explaining-javascript-scope-and-closures/#comment-457265</link>
		<dc:creator>Andreas Rydberg</dc:creator>
		<pubDate>Fri, 10 Oct 2008 11:48:05 +0000</pubDate>
		<guid isPermaLink="false">http://www.robertnyman.com/?p=881#comment-457265</guid>
		<description>Thanks Robert for a great article! I really need to read up on closures.</description>
		<content:encoded><![CDATA[<p>Thanks Robert for a great article! I really need to read up on closures.</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Robert Nyman</title>
		<link>http://www.robertnyman.com/2008/10/09/explaining-javascript-scope-and-closures/#comment-457153</link>
		<dc:creator>Robert Nyman</dc:creator>
		<pubDate>Fri, 10 Oct 2008 09:47:28 +0000</pubDate>
		<guid isPermaLink="false">http://www.robertnyman.com/?p=881#comment-457153</guid>
		<description>Thanks guys, I'm glad that you liked it!

Andrea,

Really nice! :-)
I choose my approach partly for easier readability and understanding for everyone, but also honestly because I didn't think as far as you did.

I think it's a perfect example of making closures portable! Thanks!</description>
		<content:encoded><![CDATA[<p>Thanks guys, I&#8217;m glad that you liked it!</p>
<p>Andrea,</p>
<p>Really nice! <img src='http://www.robertnyman.com/wp-includes/images/smilies/icon_smile.gif' alt=':-)' class='wp-smiley' /><br />
I choose my approach partly for easier readability and understanding for everyone, but also honestly because I didn&#8217;t think as far as you did.</p>
<p>I think it&#8217;s a perfect example of making closures portable! Thanks!</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Andrea Giammarchi</title>
		<link>http://www.robertnyman.com/2008/10/09/explaining-javascript-scope-and-closures/#comment-457140</link>
		<dc:creator>Andrea Giammarchi</dc:creator>
		<pubDate>Fri, 10 Oct 2008 09:22:41 +0000</pubDate>
		<guid isPermaLink="false">http://www.robertnyman.com/?p=881#comment-457140</guid>
		<description>Hi Robert,
I would suggest a better addLinks example, since there is no reason to create two functions, one for the closure, and another for the current link onclick event.

function addLinks () {
	for (var onclick = function(num){return function(){alert(num)}}, i=0, link; i&#60;5; i++) {
		link = document.createElement("a");
		link.innerHTML = "Link " + i;
		link.onclick = onclick(i);
		document.body.appendChild(link);
	}
};
onload = addLinks;

This is more about performances (maniacs) but it is good to understand closure portability, isn't it? :-)</description>
		<content:encoded><![CDATA[<p>Hi Robert,<br />
I would suggest a better addLinks example, since there is no reason to create two functions, one for the closure, and another for the current link onclick event.</p>
<p>function addLinks () {<br />
	for (var onclick = function(num){return function(){alert(num)}}, i=0, link; i&lt;5; i++) {<br />
		link = document.createElement(&#8221;a&#8221;);<br />
		link.innerHTML = &#8220;Link &#8221; + i;<br />
		link.onclick = onclick(i);<br />
		document.body.appendChild(link);<br />
	}<br />
};<br />
onload = addLinks;</p>
<p>This is more about performances (maniacs) but it is good to understand closure portability, isn&#8217;t it? <img src='http://www.robertnyman.com/wp-includes/images/smilies/icon_smile.gif' alt=':-)' class='wp-smiley' /></p>
]]></content:encoded>
	</item>
</channel>
</rss>
