<?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: Explaining JavaScript scope and closures</title>
	<atom:link href="http://robertnyman.com/2008/10/09/explaining-javascript-scope-and-closures/feed/" rel="self" type="application/rss+xml" />
	<link>http://robertnyman.com/2008/10/09/explaining-javascript-scope-and-closures/</link>
	<description>Web development and Internet trends</description>
	<lastBuildDate>Sat, 20 Mar 2010 05:17:00 +0000</lastBuildDate>
	<generator>http://wordpress.org/?v=2.9.2</generator>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
		<item>
		<title>By: 6 Advanced JavaScript Techniques You Should Know &#171; HUE Designer</title>
		<link>http://robertnyman.com/2008/10/09/explaining-javascript-scope-and-closures/#comment-627584</link>
		<dc:creator>6 Advanced JavaScript Techniques You Should Know &#171; HUE Designer</dc:creator>
		<pubDate>Wed, 17 Mar 2010 12:50:46 +0000</pubDate>
		<guid isPermaLink="false">http://www.robertnyman.com/?p=881#comment-627584</guid>
		<description>[...] Explaining JavaScript scope and closures (Robert Nyman) [...]</description>
		<content:encoded><![CDATA[<p>[...] Explaining JavaScript scope and closures (Robert Nyman) [...]</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Robert Nyman</title>
		<link>http://robertnyman.com/2008/10/09/explaining-javascript-scope-and-closures/#comment-627540</link>
		<dc:creator>Robert Nyman</dc:creator>
		<pubDate>Wed, 17 Mar 2010 09:54:44 +0000</pubDate>
		<guid isPermaLink="false">http://www.robertnyman.com/?p=881#comment-627540</guid>
		<description>Jonathan,

Glad you liked it!</description>
		<content:encoded><![CDATA[<p>Jonathan,</p>
<p>Glad you liked it!</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Jonathan Nieto</title>
		<link>http://robertnyman.com/2008/10/09/explaining-javascript-scope-and-closures/#comment-627423</link>
		<dc:creator>Jonathan Nieto</dc:creator>
		<pubDate>Wed, 17 Mar 2010 00:42:42 +0000</pubDate>
		<guid isPermaLink="false">http://www.robertnyman.com/?p=881#comment-627423</guid>
		<description>Hey! this post was really helpful!!!

Thx :D</description>
		<content:encoded><![CDATA[<p>Hey! this post was really helpful!!!</p>
<p>Thx <img src='http://robertnyman.com/wp-includes/images/smilies/icon_biggrin.gif' alt=':D' class='wp-smiley' /> </p>
]]></content:encoded>
	</item>
	<item>
		<title>By: JavaScript: Tools, Coding Standard and Guidelines &#187; select *</title>
		<link>http://robertnyman.com/2008/10/09/explaining-javascript-scope-and-closures/#comment-621834</link>
		<dc:creator>JavaScript: Tools, Coding Standard and Guidelines &#187; select *</dc:creator>
		<pubDate>Wed, 10 Feb 2010 06:18:21 +0000</pubDate>
		<guid isPermaLink="false">http://www.robertnyman.com/?p=881#comment-621834</guid>
		<description>[...] Closures : Working with closures and Explaining JavaScript scope and closures. [...]</description>
		<content:encoded><![CDATA[<p>[...] Closures : Working with closures and Explaining JavaScript scope and closures. [...]</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Robert Nyman</title>
		<link>http://robertnyman.com/2008/10/09/explaining-javascript-scope-and-closures/#comment-619493</link>
		<dc:creator>Robert Nyman</dc:creator>
		<pubDate>Mon, 25 Jan 2010 07:23:37 +0000</pubDate>
		<guid isPermaLink="false">http://www.robertnyman.com/?p=881#comment-619493</guid>
		<description>John,

Great to hear!</description>
		<content:encoded><![CDATA[<p>John,</p>
<p>Great to hear!</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: John</title>
		<link>http://robertnyman.com/2008/10/09/explaining-javascript-scope-and-closures/#comment-619205</link>
		<dc:creator>John</dc:creator>
		<pubDate>Fri, 22 Jan 2010 20:27:30 +0000</pubDate>
		<guid isPermaLink="false">http://www.robertnyman.com/?p=881#comment-619205</guid>
		<description>I know this post is a year old, but the section on the loop problem really helped me today.  Thanks!</description>
		<content:encoded><![CDATA[<p>I know this post is a year old, but the section on the loop problem really helped me today.  Thanks!</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: 6 excellent Advanced JavaScript Techniques &#171; EWS</title>
		<link>http://robertnyman.com/2008/10/09/explaining-javascript-scope-and-closures/#comment-618551</link>
		<dc:creator>6 excellent Advanced JavaScript Techniques &#171; EWS</dc:creator>
		<pubDate>Tue, 19 Jan 2010 00:05:31 +0000</pubDate>
		<guid isPermaLink="false">http://www.robertnyman.com/?p=881#comment-618551</guid>
		<description>[...] Explaining JavaScript scope and closures (Robert Nyman) [...]</description>
		<content:encoded><![CDATA[<p>[...] Explaining JavaScript scope and closures (Robert Nyman) [...]</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Robert Nyman</title>
		<link>http://robertnyman.com/2008/10/09/explaining-javascript-scope-and-closures/#comment-612371</link>
		<dc:creator>Robert Nyman</dc:creator>
		<pubDate>Mon, 30 Nov 2009 17:21:48 +0000</pubDate>
		<guid isPermaLink="false">http://www.robertnyman.com/?p=881#comment-612371</guid>
		<description>Mikay,

Glad that you liked it! :-)</description>
		<content:encoded><![CDATA[<p>Mikay,</p>
<p>Glad that you liked it! <img src='http://robertnyman.com/wp-includes/images/smilies/icon_smile.gif' alt=':-)' class='wp-smiley' /> </p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Mikay</title>
		<link>http://robertnyman.com/2008/10/09/explaining-javascript-scope-and-closures/#comment-612359</link>
		<dc:creator>Mikay</dc:creator>
		<pubDate>Mon, 30 Nov 2009 16:16:34 +0000</pubDate>
		<guid isPermaLink="false">http://www.robertnyman.com/?p=881#comment-612359</guid>
		<description>Thanks,Robert.Great article.You help me completely understand the Closure,and this is the first article I read about Self-invoking functions,
though I have learned it from the source code of Prototype.</description>
		<content:encoded><![CDATA[<p>Thanks,Robert.Great article.You help me completely understand the Closure,and this is the first article I read about Self-invoking functions,<br />
though I have learned it from the source code of Prototype.</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: karalamalar &#187; Blog Ar?ivi &#187; JavaScript kapsam ve kaplamlar? anlamak</title>
		<link>http://robertnyman.com/2008/10/09/explaining-javascript-scope-and-closures/#comment-610780</link>
		<dc:creator>karalamalar &#187; Blog Ar?ivi &#187; JavaScript kapsam ve kaplamlar? anlamak</dc:creator>
		<pubDate>Thu, 19 Nov 2009 12:49:12 +0000</pubDate>
		<guid isPermaLink="false">http://www.robertnyman.com/?p=881#comment-610780</guid>
		<description>[...] kapsam ve kaplamlar? anlamak  Bu makalenin orijinali (Explaining JavaScript scope and closures) Robert Nyman taraf?ndan kendi blogunda yaz?lm??t?r. Türkçe çevirisi için kendisinden izin [...]</description>
		<content:encoded><![CDATA[<p>[...] kapsam ve kaplamlar? anlamak  Bu makalenin orijinali (Explaining JavaScript scope and closures) Robert Nyman taraf?ndan kendi blogunda yaz?lm??t?r. Türkçe çevirisi için kendisinden izin [...]</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Robert Nyman</title>
		<link>http://robertnyman.com/2008/10/09/explaining-javascript-scope-and-closures/#comment-605979</link>
		<dc:creator>Robert Nyman</dc:creator>
		<pubDate>Sun, 18 Oct 2009 10:16:13 +0000</pubDate>
		<guid isPermaLink="false">http://www.robertnyman.com/?p=881#comment-605979</guid>
		<description>Julie,

Great! And don&#039;t worry about &lt;code&gt;onload&lt;/code&gt;: one smal  step at a time. :-)</description>
		<content:encoded><![CDATA[<p>Julie,</p>
<p>Great! And don&#8217;t worry about <code>onload</code>: one smal  step at a time. <img src='http://robertnyman.com/wp-includes/images/smilies/icon_smile.gif' alt=':-)' class='wp-smiley' /> </p>
]]></content:encoded>
	</item>
	<item>
		<title>By: 6 Advanced JavaScript Techniques You Should Know &#124; My Blog</title>
		<link>http://robertnyman.com/2008/10/09/explaining-javascript-scope-and-closures/#comment-605869</link>
		<dc:creator>6 Advanced JavaScript Techniques You Should Know &#124; My Blog</dc:creator>
		<pubDate>Sat, 17 Oct 2009 06:36:37 +0000</pubDate>
		<guid isPermaLink="false">http://www.robertnyman.com/?p=881#comment-605869</guid>
		<description>[...] Explaining JavaScript scope and closures (Robert Nyman) [...]</description>
		<content:encoded><![CDATA[<p>[...] Explaining JavaScript scope and closures (Robert Nyman) [...]</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Julie</title>
		<link>http://robertnyman.com/2008/10/09/explaining-javascript-scope-and-closures/#comment-605835</link>
		<dc:creator>Julie</dc:creator>
		<pubDate>Fri, 16 Oct 2009 19:35:21 +0000</pubDate>
		<guid isPermaLink="false">http://www.robertnyman.com/?p=881#comment-605835</guid>
		<description>Hey Robert,

Thanks for all the tips and and your full explanation about closures. I really get it now! I&#039;ll definitely check out the script fixes. Closures certaiinly look the way to go, especially as you explained above they&#039;re very handy for OOP and encapsulation.

On an aside about onload, good job you pointed that out. I must admit I tend just to pile everything into init and have one onload, not the best way if you have a team working on a project I suppose, but never have any problem with it. I suppose one day I&#039;ll have to catch up with the modern approach... ;)</description>
		<content:encoded><![CDATA[<p>Hey Robert,</p>
<p>Thanks for all the tips and and your full explanation about closures. I really get it now! I&#8217;ll definitely check out the script fixes. Closures certaiinly look the way to go, especially as you explained above they&#8217;re very handy for OOP and encapsulation.</p>
<p>On an aside about onload, good job you pointed that out. I must admit I tend just to pile everything into init and have one onload, not the best way if you have a team working on a project I suppose, but never have any problem with it. I suppose one day I&#8217;ll have to catch up with the modern approach&#8230; <img src='http://robertnyman.com/wp-includes/images/smilies/icon_wink.gif' alt=';)' class='wp-smiley' /> </p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Robert Nyman</title>
		<link>http://robertnyman.com/2008/10/09/explaining-javascript-scope-and-closures/#comment-605822</link>
		<dc:creator>Robert Nyman</dc:creator>
		<pubDate>Fri, 16 Oct 2009 16:59:02 +0000</pubDate>
		<guid isPermaLink="false">http://www.robertnyman.com/?p=881#comment-605822</guid>
		<description>Julie,

Nice! However, you are overwriting something on a global object, so it will not allow you to have multiple &lt;code&gt;window.onload&lt;/code&gt; - just thought I&#039;d mention that. :-)

And sure, it&#039;s a closure. Whatever you put in there, i.e. that doesn&#039;t explicitly overwrite something that exists outside of it, is only valid and reachable from within that code. If you want to return something to the outside, though, you can use a &lt;code&gt;return&lt;/code&gt; statement in that anonymous function, which will be returned globally.

Also: no closures are bad; IE is terrible. :-)
But, to my knowledge, Microsoft released some separate script fixes as well. More info can be found in &lt;a href=&quot;http://ajaxian.com/archives/ie-memory-leaks-be-gone&quot; rel=&quot;nofollow&quot;&gt;IE Memory Leaks Be-gone&lt;/a&gt; and &lt;a href=&quot;http://ajaxian.com/archives/ies-memory-leak-fix-greatly-exaggerated&quot; rel=&quot;nofollow&quot;&gt;IE’s Memory Leak Fix Greatly Exaggerated&lt;/a&gt;.

However, really, don&#039;t worry about this. All major JavaScript libraries, such as jQuery and others, completely rely on closures as well. So, closures is a de-facto approach to efficient JavaScript programming.</description>
		<content:encoded><![CDATA[<p>Julie,</p>
<p>Nice! However, you are overwriting something on a global object, so it will not allow you to have multiple <code>window.onload</code> &#8211; just thought I&#8217;d mention that. <img src='http://robertnyman.com/wp-includes/images/smilies/icon_smile.gif' alt=':-)' class='wp-smiley' /> </p>
<p>And sure, it&#8217;s a closure. Whatever you put in there, i.e. that doesn&#8217;t explicitly overwrite something that exists outside of it, is only valid and reachable from within that code. If you want to return something to the outside, though, you can use a <code>return</code> statement in that anonymous function, which will be returned globally.</p>
<p>Also: no closures are bad; IE is terrible. <img src='http://robertnyman.com/wp-includes/images/smilies/icon_smile.gif' alt=':-)' class='wp-smiley' /><br />
But, to my knowledge, Microsoft released some separate script fixes as well. More info can be found in <a href="http://ajaxian.com/archives/ie-memory-leaks-be-gone" rel="nofollow">IE Memory Leaks Be-gone</a> and <a href="http://ajaxian.com/archives/ies-memory-leak-fix-greatly-exaggerated" rel="nofollow">IE’s Memory Leak Fix Greatly Exaggerated</a>.</p>
<p>However, really, don&#8217;t worry about this. All major JavaScript libraries, such as jQuery and others, completely rely on closures as well. So, closures is a de-facto approach to efficient JavaScript programming.</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Julie</title>
		<link>http://robertnyman.com/2008/10/09/explaining-javascript-scope-and-closures/#comment-605803</link>
		<dc:creator>Julie</dc:creator>
		<pubDate>Fri, 16 Oct 2009 13:42:27 +0000</pubDate>
		<guid isPermaLink="false">http://www.robertnyman.com/?p=881#comment-605803</guid>
		<description>Hi Robert,

Many thanks for the quick reply!

I&#039;ve been playing around with self-invoking functions since your article, and came up with a simple way to &quot;encapsulate&quot; onload, thought you might like to see it :)

(function()
{
window.onload=init;
})();

This is just like your &quot;dog&quot; function above. But would it technically be a closure though?

Looks like closures give memory leaks to IE before version 8 (good ref is  http://msdn.microsoft.com/en-us/library/dd361842(VS.85).aspx ), so they would still effect a substantial number of users right now. But are all closures bad??</description>
		<content:encoded><![CDATA[<p>Hi Robert,</p>
<p>Many thanks for the quick reply!</p>
<p>I&#8217;ve been playing around with self-invoking functions since your article, and came up with a simple way to &#8220;encapsulate&#8221; onload, thought you might like to see it <img src='http://robertnyman.com/wp-includes/images/smilies/icon_smile.gif' alt=':)' class='wp-smiley' /> </p>
<p>(function()<br />
{<br />
window.onload=init;<br />
})();</p>
<p>This is just like your &#8220;dog&#8221; function above. But would it technically be a closure though?</p>
<p>Looks like closures give memory leaks to IE before version 8 (good ref is  <a href="http://msdn.microsoft.com/en-us/library/dd361842(VS.85).aspx" rel="nofollow">http://msdn.microsoft.com/en-us/library/dd361842(VS.85).aspx</a> ), so they would still effect a substantial number of users right now. But are all closures bad??</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Robert Nyman</title>
		<link>http://robertnyman.com/2008/10/09/explaining-javascript-scope-and-closures/#comment-605779</link>
		<dc:creator>Robert Nyman</dc:creator>
		<pubDate>Fri, 16 Oct 2009 08:26:17 +0000</pubDate>
		<guid isPermaLink="false">http://www.robertnyman.com/?p=881#comment-605779</guid>
		<description>Julie,

Glad you liked it!
About &lt;code&gt;addLinks&lt;/code&gt;: it is rather that within each iteration of the loop, an inner function is called immediately to get a reference to the value of &lt;code&gt;i&lt;/code&gt; at that specific time, and then assign that value to the &lt;code&gt;onclick&lt;/code&gt; event of the link.

&gt; Can you have a self-invoking function without a closure?
&gt; If so, would there be any benefit to doing this?

Well, a self-invoking rather creates a closure immediately. Then it is up to you if you want to take advantage of what&#039;s offered with that or not.

&gt; If you use closures, what about memory leeks?
I believe older versions of Internet Explorer had some memory issues with this, but that they should be sorted out by long. Or at least so I hope! :-)</description>
		<content:encoded><![CDATA[<p>Julie,</p>
<p>Glad you liked it!<br />
About <code>addLinks</code>: it is rather that within each iteration of the loop, an inner function is called immediately to get a reference to the value of <code>i</code> at that specific time, and then assign that value to the <code>onclick</code> event of the link.</p>
<p>> Can you have a self-invoking function without a closure?<br />
> If so, would there be any benefit to doing this?</p>
<p>Well, a self-invoking rather creates a closure immediately. Then it is up to you if you want to take advantage of what&#8217;s offered with that or not.</p>
<p>> If you use closures, what about memory leeks?<br />
I believe older versions of Internet Explorer had some memory issues with this, but that they should be sorted out by long. Or at least so I hope! <img src='http://robertnyman.com/wp-includes/images/smilies/icon_smile.gif' alt=':-)' class='wp-smiley' /> </p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Julie</title>
		<link>http://robertnyman.com/2008/10/09/explaining-javascript-scope-and-closures/#comment-605679</link>
		<dc:creator>Julie</dc:creator>
		<pubDate>Thu, 15 Oct 2009 13:42:48 +0000</pubDate>
		<guid isPermaLink="false">http://www.robertnyman.com/?p=881#comment-605679</guid>
		<description>Hi Robert,

Many thanks for such a succinct article! I&#039;m rather new to all this and it took a bit of time to get my head around self-invoking functions. 

If anyone is still struggling, correct me if I&#039;m wrong but for your &#039;infamous loop&#039; problem, if for the sake of argument you call the addLinks function declaration (all the loop and stuff) &#039;splurge&#039;, then am I right in thinking that the function call is done immediately by the declaration &#039;splurge&#039;(i); ?

I&#039;m curious about several other points as well:
Can you have a self-invoking function without a closure?
If so, would there be any benefit to doing this?
If you use closures, what about memory leeks?</description>
		<content:encoded><![CDATA[<p>Hi Robert,</p>
<p>Many thanks for such a succinct article! I&#8217;m rather new to all this and it took a bit of time to get my head around self-invoking functions. </p>
<p>If anyone is still struggling, correct me if I&#8217;m wrong but for your &#8216;infamous loop&#8217; problem, if for the sake of argument you call the addLinks function declaration (all the loop and stuff) &#8217;splurge&#8217;, then am I right in thinking that the function call is done immediately by the declaration &#8217;splurge&#8217;(i); ?</p>
<p>I&#8217;m curious about several other points as well:<br />
Can you have a self-invoking function without a closure?<br />
If so, would there be any benefit to doing this?<br />
If you use closures, what about memory leeks?</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: 6 Advanced JavaScript Techniques You Should Know &#124; Programming Blog</title>
		<link>http://robertnyman.com/2008/10/09/explaining-javascript-scope-and-closures/#comment-604853</link>
		<dc:creator>6 Advanced JavaScript Techniques You Should Know &#124; Programming Blog</dc:creator>
		<pubDate>Thu, 08 Oct 2009 21:31:05 +0000</pubDate>
		<guid isPermaLink="false">http://www.robertnyman.com/?p=881#comment-604853</guid>
		<description>[...] Explaining JavaScript scope and closures (Robert Nyman) [...]</description>
		<content:encoded><![CDATA[<p>[...] Explaining JavaScript scope and closures (Robert Nyman) [...]</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: 6 Advanced JavaScript Techniques You Should Know</title>
		<link>http://robertnyman.com/2008/10/09/explaining-javascript-scope-and-closures/#comment-604829</link>
		<dc:creator>6 Advanced JavaScript Techniques You Should Know</dc:creator>
		<pubDate>Thu, 08 Oct 2009 15:59:46 +0000</pubDate>
		<guid isPermaLink="false">http://www.robertnyman.com/?p=881#comment-604829</guid>
		<description>[...] Explaining JavaScript scope and closures (Robert Nyman) [...]</description>
		<content:encoded><![CDATA[<p>[...] Explaining JavaScript scope and closures (Robert Nyman) [...]</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Robert Nyman</title>
		<link>http://robertnyman.com/2008/10/09/explaining-javascript-scope-and-closures/#comment-599671</link>
		<dc:creator>Robert Nyman</dc:creator>
		<pubDate>Sun, 30 Aug 2009 20:02:36 +0000</pubDate>
		<guid isPermaLink="false">http://www.robertnyman.com/?p=881#comment-599671</guid>
		<description>Alan,

No problem, glad you understood what I meant! :-)</description>
		<content:encoded><![CDATA[<p>Alan,</p>
<p>No problem, glad you understood what I meant! <img src='http://robertnyman.com/wp-includes/images/smilies/icon_smile.gif' alt=':-)' class='wp-smiley' /> </p>
]]></content:encoded>
	</item>
</channel>
</rss>
