<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:wfw="http://wellformedweb.org/CommentAPI/"
	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/"
	xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
	>

<channel>
	<title>Software Warlock &#187; variable</title>
	<atom:link href="http://softwareblog.morlok.net/tag/variable/feed/" rel="self" type="application/rss+xml" />
	<link>http://softwareblog.morlok.net</link>
	<description></description>
	<lastBuildDate>Sat, 24 Dec 2011 16:06:59 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.1</generator>
		<item>
		<title>Fun with JavaScript Closures + Scoping</title>
		<link>http://softwareblog.morlok.net/2010/07/10/fun-with-javascript-closures-scoping/</link>
		<comments>http://softwareblog.morlok.net/2010/07/10/fun-with-javascript-closures-scoping/#comments</comments>
		<pubDate>Sat, 10 Jul 2010 14:55:44 +0000</pubDate>
		<dc:creator>Warlock</dc:creator>
				<category><![CDATA[Javascript]]></category>
		<category><![CDATA[closure]]></category>
		<category><![CDATA[scoping]]></category>
		<category><![CDATA[variable]]></category>

		<guid isPermaLink="false">http://softwareblog.morlok.net/?p=396</guid>
		<description><![CDATA[It&#8217;s easy to forget that variable scoping in JavaScript is at the function level and not a the block level like what you get with C/C++/C#/Java/etc. This can have some counter intuitive implications when you&#8217;re dealing with closures. Consider the &#8230;<p class="read-more"><a href="http://softwareblog.morlok.net/2010/07/10/fun-with-javascript-closures-scoping/">Read more &#187;</a></p>]]></description>
			<content:encoded><![CDATA[<p>It&#8217;s easy to forget that variable scoping in JavaScript is at the function level and not a the block level like what you get with C/C++/C#/Java/etc.  This can have some counter intuitive implications when you&#8217;re dealing with closures.  Consider the following example:</p>
<pre name="code" class="javascript">
var closure;

for(var i = 0; i <= 5; i++) {
  var x = i * i;
  if( i == 1 ) {
    closure = function() { alert(x); };
  }
}

closure();
</pre>
<p><script type="text/javascript">
function runme() {
var closure;
for(var i = 0; i <= 5; i++) {
  var x = i * i;
  if( i == 1 ) {
    closure = function() { alert(x); };
  }
}
closure();
}
</script></p>
<p> What do you think the output of the alert box will be?  At first blush it's easy to think it will be 1 because the variable x is declared within the <tt>for</tt> loop.</p>
<p><a href="javascript:runme()">Run Code</a></p>
<p>In reality, though, the output is 25 because despite the variable declaration in the <tt>for</tt> loop, the code is functionally equivalent to this:</p>
<pre name="code" class="javascript">
var closure;
var x;

for(var i = 0; i <= 5; i++) {
  x = i * i;
  if( i == 1 ) {
    closure = function() { alert(x); };
  }
}

closure();
</pre>
<p>This is important to remember when working with closures in loops.</p>
<p>So how do solve this problem? Manually create a new scope by creating and invoking an anonymous function.</p>
<pre name="code" class="javascript">
var closure;

for(var i = 0; i <= 5; i++) {
  var x = i * i;
  if( i == 1 ) {
    (function(x) {
      closure = function() { alert(x); };
    })(x);
  }
}

closure();
</pre>
<p><script type="text/javascript">
function runmefixed() {
var closure;
for(var i = 0; i <= 5; i++) {
  var x = i * i;
  if( i == 1 ) {
    (function(x) {
      closure = function() { alert(x); };
    })(x);
  }
}
closure();
}
</script></p>
<p><a href="javascript:runmefixed()">Run Code</a></p>
<p>With the newly introduce lexical scope, the program returns 1 as one would expect.</p>
]]></content:encoded>
			<wfw:commentRss>http://softwareblog.morlok.net/2010/07/10/fun-with-javascript-closures-scoping/feed/</wfw:commentRss>
		<slash:comments>1</slash:comments>
		</item>
	</channel>
</rss>

