<?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>Duplo &#187; scope</title>
	<atom:link href="http://kill-0.com/duplo/tag/scope/feed/" rel="self" type="application/rss+xml" />
	<link>http://kill-0.com/duplo</link>
	<description>Building Blocks &#038; Learning Experiences</description>
	<lastBuildDate>Fri, 02 Jul 2010 16:03:14 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.0.1</generator>
		<item>
		<title>JavaScript doesn&#8217;t have block scope</title>
		<link>http://kill-0.com/duplo/2009/11/12/javascript-doesnt-have-block-scope/</link>
		<comments>http://kill-0.com/duplo/2009/11/12/javascript-doesnt-have-block-scope/#comments</comments>
		<pubDate>Thu, 12 Nov 2009 21:39:25 +0000</pubDate>
		<dc:creator>ericw</dc:creator>
				<category><![CDATA[Software]]></category>
		<category><![CDATA[javascript]]></category>
		<category><![CDATA[scope]]></category>

		<guid isPermaLink="false">http://kill-0.com/duplo/?p=21</guid>
		<description><![CDATA[&#8230; it has function scope.  Okay, I get that, but apparently I hadn&#8217;t totally wrapped my head around just what that means.  In essence, it means that all variables that have a declaration in the function, are declared, regardless of whether or not the line of code which declares them would be executed.  Given the [...]]]></description>
			<content:encoded><![CDATA[<p>&#8230; it has function scope.  Okay, I get that, but apparently I hadn&#8217;t totally wrapped my head around just what that means.  In essence, it means that all variables that have a declaration in the function, are declared, <em>regardless of whether or not the line of code which declares them would be executed</em>.  Given the following code:<br />
<code> </code></p>
<pre>var foo = "bar";

function test() {
  console.debug("foo is (1st time)" + foo);
  if (false) {
    var foo = "baz";
  }
  console.debug("foo is (2nd time)" + foo);
}</pre>
<p>Your console will show something like this:</p>
<pre>foo is (1st time) undefined
foo is (2nd time) undefined</pre>
<p>Which is a little weird since the local declaration of <tt>foo</tt> was never executed.  A quick search didn&#8217;t turn up any promising hits, but I imagine that there must be a preprocessing step that executes all variable declarations before the function is run.  The very similar function below (diffs in bold):</p>
<pre>var foo = "bar";

function test() {
  console.debug("foo is (1st time)" + foo);
  if (<strong>"undefined" == typeof(foo)</strong>) {
    var foo = "baz";
  }
  console.debug("foo is (2nd time)" + foo);
}</pre>
<p>Yields:</p>
<pre>foo is (1st time) undefined
foo is (2nd time) baz</pre>
<p>A more intentional use is probably (diffs also in bold):</p>
<pre>var foo = "bar";

function test() {
  console.debug("foo is (1st time)" + foo);
  if (<strong>"undefined" == typeof(foo)</strong>) {
    <strong>foo = "baz";</strong>
  }
  console.debug("foo is (2nd time)" + foo);
}</pre>
<p>Which has output:</p>
<pre>foo is (1st time) bar
foo is (2nd time) bar</pre>
<p>So this isn&#8217;t anything groundbreaking, or even weird.  It&#8217;s clearly defined<sup>1</sup>, and discussed<sup>2</sup>, but the consequences eluded me for about an hour last night.  Hopefully by writing this article, I&#8217;ll remember for next time.</p>
<ol class="footnotes"><li id="footnote_0_21" class="footnote">http://docstore.mik.ua/orelly/webprog/jscript/ch04_03.htm#jscript4-CHP-4-SECT-3.1</li><li id="footnote_1_21" class="footnote">http://stackoverflow.com/questions/1711173/declaration-for-variable-in-while-condition-in-javascript</li></ol>]]></content:encoded>
			<wfw:commentRss>http://kill-0.com/duplo/2009/11/12/javascript-doesnt-have-block-scope/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
	</channel>
</rss>
