<?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: A common problem with the powerful javascript closures</title>
	<atom:link href="http://www.javascriptkata.com/2007/04/11/a-common-problem-with-the-powerful-javascript-closures/feed/" rel="self" type="application/rss+xml" />
	<link>http://www.javascriptkata.com/2007/04/11/a-common-problem-with-the-powerful-javascript-closures/</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: Gilbert</title>
		<link>http://www.javascriptkata.com/2007/04/11/a-common-problem-with-the-powerful-javascript-closures/comment-page-1/#comment-93</link>
		<dc:creator>Gilbert</dc:creator>
		<pubDate>Thu, 14 Jun 2007 11:46:19 +0000</pubDate>
		<guid isPermaLink="false">http://javascriptkata.timmyontime.com/?p=24#comment-93</guid>
		<description>function test(){
var i = 10;
var f = new Function(&#039;alert(&#039; + i + &#039;);&#039;);
f();
}
test();

This works</description>
		<content:encoded><![CDATA[<p>function test(){<br />
var i = 10;<br />
var f = new Function(&#8216;alert(&#8216; + i + &#8216;);&#8217;);<br />
f();<br />
}<br />
test();</p>
<p>This works</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Dan</title>
		<link>http://www.javascriptkata.com/2007/04/11/a-common-problem-with-the-powerful-javascript-closures/comment-page-1/#comment-92</link>
		<dc:creator>Dan</dc:creator>
		<pubDate>Tue, 17 Apr 2007 17:57:31 +0000</pubDate>
		<guid isPermaLink="false">http://javascriptkata.timmyontime.com/?p=24#comment-92</guid>
		<description>Dammit! You&#039;re right. Thanks!</description>
		<content:encoded><![CDATA[<p>Dammit! You&#8217;re right. Thanks!</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: BK</title>
		<link>http://www.javascriptkata.com/2007/04/11/a-common-problem-with-the-powerful-javascript-closures/comment-page-1/#comment-91</link>
		<dc:creator>BK</dc:creator>
		<pubDate>Tue, 17 Apr 2007 17:46:38 +0000</pubDate>
		<guid isPermaLink="false">http://javascriptkata.timmyontime.com/?p=24#comment-91</guid>
		<description>this works if you put it global.
Now, try this:
function test(){
  var i = 10;
  var f = new Function(&quot;alert(i);&quot;);
  f();
}
test();

Result: i is undefined in both FF and IE -&gt; No Closure</description>
		<content:encoded><![CDATA[<p>this works if you put it global.<br />
Now, try this:<br />
function test(){<br />
  var i = 10;<br />
  var f = new Function(&#8220;alert(i);&#8221;);<br />
  f();<br />
}<br />
test();</p>
<p>Result: i is undefined in both FF and IE -&gt; No Closure</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Dan</title>
		<link>http://www.javascriptkata.com/2007/04/11/a-common-problem-with-the-powerful-javascript-closures/comment-page-1/#comment-90</link>
		<dc:creator>Dan</dc:creator>
		<pubDate>Tue, 17 Apr 2007 17:41:51 +0000</pubDate>
		<guid isPermaLink="false">http://javascriptkata.timmyontime.com/?p=24#comment-90</guid>
		<description>BK, closures are everywhere! Even with Function... try that code.

var i = 10;
var f = new Function(&quot;alert(i);&quot;);
f();

&lt;em&gt;Update : I am wrong! Look at the next comment from BK&lt;/em&gt;</description>
		<content:encoded><![CDATA[<p>BK, closures are everywhere! Even with Function&#8230; try that code.</p>
<p>var i = 10;<br />
var f = new Function(&#8220;alert(i);&#8221;);<br />
f();</p>
<p><em>Update : I am wrong! Look at the next comment from BK</em></p>
]]></content:encoded>
	</item>
	<item>
		<title>By: BK</title>
		<link>http://www.javascriptkata.com/2007/04/11/a-common-problem-with-the-powerful-javascript-closures/comment-page-1/#comment-89</link>
		<dc:creator>BK</dc:creator>
		<pubDate>Tue, 17 Apr 2007 16:50:54 +0000</pubDate>
		<guid isPermaLink="false">http://javascriptkata.timmyontime.com/?p=24#comment-89</guid>
		<description>I agree that they&#039;re Evil to write and maintain but still the only way (I know) of creating a function in a function without closure and for an application that would need to run in a very limited memory space, you could have no choice (except maybe to create the function outside of the function)</description>
		<content:encoded><![CDATA[<p>I agree that they&#8217;re Evil to write and maintain but still the only way (I know) of creating a function in a function without closure and for an application that would need to run in a very limited memory space, you could have no choice (except maybe to create the function outside of the function)</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Dan</title>
		<link>http://www.javascriptkata.com/2007/04/11/a-common-problem-with-the-powerful-javascript-closures/comment-page-1/#comment-88</link>
		<dc:creator>Dan</dc:creator>
		<pubDate>Mon, 16 Apr 2007 22:44:51 +0000</pubDate>
		<guid isPermaLink="false">http://javascriptkata.timmyontime.com/?p=24#comment-88</guid>
		<description>@jsn : I didn&#039;t redefine alertFunction, it&#039;s just that I have alertFunction and alertFunctions (with a &quot;s&quot;).

@BK : Like Aaron said, Function is a bitch. It would be a big problem if the function would be just a bit more complex...</description>
		<content:encoded><![CDATA[<p>@jsn : I didn&#8217;t redefine alertFunction, it&#8217;s just that I have alertFunction and alertFunctions (with a &#8220;s&#8221;).</p>
<p>@BK : Like Aaron said, Function is a bitch. It would be a big problem if the function would be just a bit more complex&#8230;</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Aaron Faanes</title>
		<link>http://www.javascriptkata.com/2007/04/11/a-common-problem-with-the-powerful-javascript-closures/comment-page-1/#comment-87</link>
		<dc:creator>Aaron Faanes</dc:creator>
		<pubDate>Mon, 16 Apr 2007 21:48:22 +0000</pubDate>
		<guid isPermaLink="false">http://javascriptkata.timmyontime.com/?p=24#comment-87</guid>
		<description>BK: While that would work, new Function is just a synonym for eval(), and uses of eval() are problematic at best since &quot;complicated &#039;string &quot; + concate + &quot;&#039; nation is a bitch to &quot; + write + (and * debug).

For that example, it&#039;s probably not that important either way, since there&#039;s little security, performance, or clarity issues at all with doing it any way, but I figured it&#039;s worth mentioning my dislike for it in general.

jimbojw: That ends up using a closure itself anyway, hidden behind a method call.

Function.prototype.bindAsEventListener = function(/* some args */) {
var that = this;
var args = arguments;
return function() { return that.call.apply(that, args); }
}

I&#039;m not saying it won&#039;t work, I&#039;m just saying that Prototype isn&#039;t beyond closures. :P</description>
		<content:encoded><![CDATA[<p>BK: While that would work, new Function is just a synonym for eval(), and uses of eval() are problematic at best since &#8220;complicated &#8216;string &#8221; + concate + &#8220;&#8216; nation is a bitch to &#8221; + write + (and * debug).</p>
<p>For that example, it&#8217;s probably not that important either way, since there&#8217;s little security, performance, or clarity issues at all with doing it any way, but I figured it&#8217;s worth mentioning my dislike for it in general.</p>
<p>jimbojw: That ends up using a closure itself anyway, hidden behind a method call.</p>
<p>Function.prototype.bindAsEventListener = function(/* some args */) {<br />
var that = this;<br />
var args = arguments;<br />
return function() { return that.call.apply(that, args); }<br />
}</p>
<p>I&#8217;m not saying it won&#8217;t work, I&#8217;m just saying that Prototype isn&#8217;t beyond closures. <img src='http://www.javascriptkata.com/wp-includes/images/smilies/icon_razz.gif' alt=':P' class='wp-smiley' /> </p>
]]></content:encoded>
	</item>
	<item>
		<title>By: BK</title>
		<link>http://www.javascriptkata.com/2007/04/11/a-common-problem-with-the-powerful-javascript-closures/comment-page-1/#comment-86</link>
		<dc:creator>BK</dc:creator>
		<pubDate>Mon, 16 Apr 2007 14:43:58 +0000</pubDate>
		<guid isPermaLink="false">http://javascriptkata.timmyontime.com/?p=24#comment-86</guid>
		<description>Again, the use of the Function constructor also does the trick here:
alertFunction=new Function(&quot;alert(&#039;Number is &quot; + iNumber + &quot;);&quot;);
alertFunctions.push(alertFunction);</description>
		<content:encoded><![CDATA[<p>Again, the use of the Function constructor also does the trick here:<br />
alertFunction=new Function(&#8220;alert(&#8216;Number is &#8221; + iNumber + &#8220;);&#8221;);<br />
alertFunctions.push(alertFunction);</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: jimbojw</title>
		<link>http://www.javascriptkata.com/2007/04/11/a-common-problem-with-the-powerful-javascript-closures/comment-page-1/#comment-85</link>
		<dc:creator>jimbojw</dc:creator>
		<pubDate>Thu, 12 Apr 2007 21:16:57 +0000</pubDate>
		<guid isPermaLink="false">http://javascriptkata.timmyontime.com/?p=24#comment-85</guid>
		<description>If you have access to the Prototype library, the bindAsEventListener can method can help with this.

http://wiki.script.aculo.us/scriptaculous/show/Function.prototype.bindAsEventListener


alertFunctions.push(
  (function() {
    alert(this);
  }).bindAsEventListener(iNumber)
);</description>
		<content:encoded><![CDATA[<p>If you have access to the Prototype library, the bindAsEventListener can method can help with this.</p>
<p><a href="http://wiki.script.aculo.us/scriptaculous/show/Function.prototype.bindAsEventListener" rel="nofollow">http://wiki.script.aculo.us/scriptaculous/show/Function.prototype.bindAsEventListener</a></p>
<p>alertFunctions.push(<br />
  (function() {<br />
    alert(this);<br />
  }).bindAsEventListener(iNumber)<br />
);</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: jsn</title>
		<link>http://www.javascriptkata.com/2007/04/11/a-common-problem-with-the-powerful-javascript-closures/comment-page-1/#comment-84</link>
		<dc:creator>jsn</dc:creator>
		<pubDate>Thu, 12 Apr 2007 21:07:08 +0000</pubDate>
		<guid isPermaLink="false">http://javascriptkata.timmyontime.com/?p=24#comment-84</guid>
		<description>no need to define alertFunction() multiple times.

&lt;code&gt;
function alertFunction(x) {
    return function() { alert(x) }
}

for (var iNumber = 0; iNumber</description>
		<content:encoded><![CDATA[<p>no need to define alertFunction() multiple times.</p>
<p><code><br />
function alertFunction(x) {<br />
    return function() { alert(x) }<br />
}</p>
<p>for (var iNumber = 0; iNumber</code></p>
]]></content:encoded>
	</item>
</channel>
</rss>
