<?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: The power of closures in javascript</title>
	<atom:link href="http://www.javascriptkata.com/2007/04/10/the-power-of-closures-in-javascript/feed/" rel="self" type="application/rss+xml" />
	<link>http://www.javascriptkata.com/2007/04/10/the-power-of-closures-in-javascript/</link>
	<description>Advanced katas for javascripters</description>
	<lastBuildDate>Fri, 30 Jul 2010 18:10:52 +0000</lastBuildDate>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.0</generator>
	<item>
		<title>By: How to de-anonymize your anonymous functions &#187; Javascript Kata</title>
		<link>http://www.javascriptkata.com/2007/04/10/the-power-of-closures-in-javascript/comment-page-1/#comment-910</link>
		<dc:creator>How to de-anonymize your anonymous functions &#187; Javascript Kata</dc:creator>
		<pubDate>Wed, 19 May 2010 17:31:57 +0000</pubDate>
		<guid isPermaLink="false">http://javascriptkata.timmyontime.com/?p=23#comment-910</guid>
		<description>[...] It works, but it may not be the best thing to do. You can learn more about closure on that previous post and I&#8217;ll write more about it.   Comment (RSS) &#160;&#124;&#160;&#160;Trackback         blog [...]</description>
		<content:encoded><![CDATA[<p>[...] It works, but it may not be the best thing to do. You can learn more about closure on that previous post and I&#8217;ll write more about it.   Comment (RSS) &nbsp;|&nbsp;&nbsp;Trackback         blog [...]</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: How to create an object with private variables and methods &#124; Javascript Kata</title>
		<link>http://www.javascriptkata.com/2007/04/10/the-power-of-closures-in-javascript/comment-page-1/#comment-782</link>
		<dc:creator>How to create an object with private variables and methods &#124; Javascript Kata</dc:creator>
		<pubDate>Wed, 23 Sep 2009 12:50:55 +0000</pubDate>
		<guid isPermaLink="false">http://javascriptkata.timmyontime.com/?p=23#comment-782</guid>
		<description>[...] The magic lies in creating a different scope at the end of the class definition that does not include private variables. Then, private members are available in this scope and not outside of it, thanks to the power of closures. [...]</description>
		<content:encoded><![CDATA[<p>[...] The magic lies in creating a different scope at the end of the class definition that does not include private variables. Then, private members are available in this scope and not outside of it, thanks to the power of closures. [...]</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: BK</title>
		<link>http://www.javascriptkata.com/2007/04/10/the-power-of-closures-in-javascript/comment-page-1/#comment-82</link>
		<dc:creator>BK</dc:creator>
		<pubDate>Mon, 16 Apr 2007 14:38:53 +0000</pubDate>
		<guid isPermaLink="false">http://javascriptkata.timmyontime.com/?p=23#comment-82</guid>
		<description>Dan was speaking about memory leaking when using closures. Here is a great article about that (except for the anti-MS statements but what can we do, there will always be people spitting on MS :P)
http://www.bazon.net/mishoo/articles.epl?art_id=824

Also, a way of creating inner functions that won&#039;t keep reference to the inner values is to use the Function constructor instead of the function keyword. Again, it is up to developers to choose when to use which.

Example:
function foo(e){
  var i=0
  return new Function(&quot;alert(&quot; + i + &quot; &quot; + e + &quot;)&quot;);
}</description>
		<content:encoded><![CDATA[<p>Dan was speaking about memory leaking when using closures. Here is a great article about that (except for the anti-MS statements but what can we do, there will always be people spitting on MS <img src='http://www.javascriptkata.com/wp-includes/images/smilies/icon_razz.gif' alt=':P' class='wp-smiley' /> )<br />
<a href="http://www.bazon.net/mishoo/articles.epl?art_id=824" rel="nofollow">http://www.bazon.net/mishoo/articles.epl?art_id=824</a></p>
<p>Also, a way of creating inner functions that won&#8217;t keep reference to the inner values is to use the Function constructor instead of the function keyword. Again, it is up to developers to choose when to use which.</p>
<p>Example:<br />
function foo(e){<br />
  var i=0<br />
  return new Function(&#8220;alert(&#8221; + i + &#8221; &#8221; + e + &#8220;)&#8221;);<br />
}</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Dan</title>
		<link>http://www.javascriptkata.com/2007/04/10/the-power-of-closures-in-javascript/comment-page-1/#comment-81</link>
		<dc:creator>Dan</dc:creator>
		<pubDate>Thu, 12 Apr 2007 03:36:38 +0000</pubDate>
		<guid isPermaLink="false">http://javascriptkata.timmyontime.com/?p=23#comment-81</guid>
		<description>Chris, I know that many languages support closures (that&#039;s why I wrote &quot;specific&quot; with quote). But most developers never used them. I heard that even Java has closures...</description>
		<content:encoded><![CDATA[<p>Chris, I know that many languages support closures (that&#8217;s why I wrote &#8220;specific&#8221; with quote). But most developers never used them. I heard that even Java has closures&#8230;</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Chris</title>
		<link>http://www.javascriptkata.com/2007/04/10/the-power-of-closures-in-javascript/comment-page-1/#comment-80</link>
		<dc:creator>Chris</dc:creator>
		<pubDate>Wed, 11 Apr 2007 23:09:05 +0000</pubDate>
		<guid isPermaLink="false">http://javascriptkata.timmyontime.com/?p=23#comment-80</guid>
		<description>&gt; Because closures are â€œspecificâ€ to javascript, youâ€™ll have to try it for yourself.

This statement is incorrect.  Many languages have support for closures. Many have had it before javascript was created.</description>
		<content:encoded><![CDATA[<p>&gt; Because closures are â€œspecificâ€ to javascript, youâ€™ll have to try it for yourself.</p>
<p>This statement is incorrect.  Many languages have support for closures. Many have had it before javascript was created.</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Dan</title>
		<link>http://www.javascriptkata.com/2007/04/10/the-power-of-closures-in-javascript/comment-page-1/#comment-79</link>
		<dc:creator>Dan</dc:creator>
		<pubDate>Wed, 11 Apr 2007 20:41:35 +0000</pubDate>
		<guid isPermaLink="false">http://javascriptkata.timmyontime.com/?p=23#comment-79</guid>
		<description>I really don&#039;t want to say to developers WHEN they should use it. They&#039;ll have to find on their own. I prefer it that way ;)

But still, I take your suggestion and will think about a good way to say it.</description>
		<content:encoded><![CDATA[<p>I really don&#8217;t want to say to developers WHEN they should use it. They&#8217;ll have to find on their own. I prefer it that way <img src='http://www.javascriptkata.com/wp-includes/images/smilies/icon_wink.gif' alt=';)' class='wp-smiley' /> </p>
<p>But still, I take your suggestion and will think about a good way to say it.</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Jim Plush</title>
		<link>http://www.javascriptkata.com/2007/04/10/the-power-of-closures-in-javascript/comment-page-1/#comment-78</link>
		<dc:creator>Jim Plush</dc:creator>
		<pubDate>Wed, 11 Apr 2007 15:20:02 +0000</pubDate>
		<guid isPermaLink="false">http://javascriptkata.timmyontime.com/?p=23#comment-78</guid>
		<description>hmmmm, rather introductory article. It doesn&#039;t really lend itself to why you&#039;d want to use closures or some of the other applications they&#039;re quite handy for.</description>
		<content:encoded><![CDATA[<p>hmmmm, rather introductory article. It doesn&#8217;t really lend itself to why you&#8217;d want to use closures or some of the other applications they&#8217;re quite handy for.</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Aaron Faanes</title>
		<link>http://www.javascriptkata.com/2007/04/10/the-power-of-closures-in-javascript/comment-page-1/#comment-77</link>
		<dc:creator>Aaron Faanes</dc:creator>
		<pubDate>Wed, 11 Apr 2007 01:24:23 +0000</pubDate>
		<guid isPermaLink="false">http://javascriptkata.timmyontime.com/?p=23#comment-77</guid>
		<description>Muahaha, closures are awesome. You can even take your timeout example a step further and offer a way to remove it. The code would look roughly like:

function createTimer(doSomethingLater, duration) {
// You also don&#039;t need &quot;window&quot; - window just defaults
// to the global namespace. top, parent, content, and
// a few others do this too.
var id = setTimeout(doSomethingLater, duration);
return function removeTimer() {
clearTimeout(id);
}
}

This example is a bit contrived since removing a timeout is easy, but you can see how the logic behind the function is hidden, so you can just call the function instead of capturing the id and calling clearTimeout yourself.

The removal-example becomes alot more relevant when you&#039;re dealing with stuff that&#039;s complicated to remove since you abstract the removal, and if you don&#039;t have any need to remove it, just ignore the return value, and it will be happily gc&#039;d.

You can usually tell when you should be using closures if you&#039;re finding yourself having a object pool somewhere to keep track of data you&#039;d need to do something. When you do find this, closures may very well be your cleaner solution.

Another nifty thing that you showed, but didnt mention, is that variables are scoped by function, instead of the usual way of by code block. That is to say this code is valid:

function foo() {
var arr = [1,2,3];
console.log(i);
for(var i = 0; i</description>
		<content:encoded><![CDATA[<p>Muahaha, closures are awesome. You can even take your timeout example a step further and offer a way to remove it. The code would look roughly like:</p>
<p>function createTimer(doSomethingLater, duration) {<br />
// You also don&#8217;t need &#8220;window&#8221; &#8211; window just defaults<br />
// to the global namespace. top, parent, content, and<br />
// a few others do this too.<br />
var id = setTimeout(doSomethingLater, duration);<br />
return function removeTimer() {<br />
clearTimeout(id);<br />
}<br />
}</p>
<p>This example is a bit contrived since removing a timeout is easy, but you can see how the logic behind the function is hidden, so you can just call the function instead of capturing the id and calling clearTimeout yourself.</p>
<p>The removal-example becomes alot more relevant when you&#8217;re dealing with stuff that&#8217;s complicated to remove since you abstract the removal, and if you don&#8217;t have any need to remove it, just ignore the return value, and it will be happily gc&#8217;d.</p>
<p>You can usually tell when you should be using closures if you&#8217;re finding yourself having a object pool somewhere to keep track of data you&#8217;d need to do something. When you do find this, closures may very well be your cleaner solution.</p>
<p>Another nifty thing that you showed, but didnt mention, is that variables are scoped by function, instead of the usual way of by code block. That is to say this code is valid:</p>
<p>function foo() {<br />
var arr = [1,2,3];<br />
console.log(i);<br />
for(var i = 0; i</p>
]]></content:encoded>
	</item>
</channel>
</rss>
