<?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; javascript</title>
	<atom:link href="http://kill-0.com/duplo/tag/javascript/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>
		<item>
		<title>JavaScript Conditionals from ERb</title>
		<link>http://kill-0.com/duplo/2008/05/01/javascript-conditionals-from-erb/</link>
		<comments>http://kill-0.com/duplo/2008/05/01/javascript-conditionals-from-erb/#comments</comments>
		<pubDate>Fri, 02 May 2008 04:57:45 +0000</pubDate>
		<dc:creator>ericw</dc:creator>
				<category><![CDATA[Rails]]></category>
		<category><![CDATA[javascript]]></category>

		<guid isPermaLink="false">http://kill-0.com/duplo/2008/05/01/javascript-conditionals-from-erb/</guid>
		<description><![CDATA[Rails&#8217;s JavaScript generator provides some great functionality. But unfortunately, one of the things it can&#8217;t do for you, is generate JavaScript conditionally, based on the page&#8217;s content. The reason is simply that the page&#8217;s content doesn&#8217;t exist at the time the conditional in your code was executed. I wanted to do something like the following: [...]]]></description>
			<content:encoded><![CDATA[<p>Rails&#8217;s <a href="http://api.rubyonrails.com/classes/ActionView/Helpers/PrototypeHelper/JavaScriptGenerator/GeneratorMethods.html">JavaScript generator</a> provides some great functionality.  But unfortunately, one of the things it can&#8217;t do for you, is generate JavaScript conditionally, based on the page&#8217;s content.  The reason is simply that the page&#8217;s content doesn&#8217;t exist at the time the conditional in your code was executed.  I wanted to do something like the following:</p>
<pre><code>if page["album_1_rating"]
  page["album_1_rating"].visual_effect(:highlight)
end</code></pre>
<p>As I said, at the time the <tt>if</tt> statement above is executed, there is no HTML file yet, so the generator can&#8217;t possibly know if the element referenced will exist in the file or not.  In fact, <tt>page["album_1_rating"]</tt> is a <tt>ActionView::Helpers::JavaScriptElementProxy</tt> object, which is not false, so the <tt>if</tt> statement will always execute.  This wasn&#8217;t what I wanted.  So I came up with something that would work. I added the following method to <tt>lib/prototype_helper_hacks.rb</tt>, then required it in <tt>config/environment.rb</tt>.</p>
<pre><code>def if_element(id, &amp;block)
  self &lt;&lt; "if ($(\"#{id}\")) {"
  block.call(self[id])
  self &lt;&lt; "}"
end</code></pre>
<p>This lets me do things like this in my views:</p>
<pre><code>page.if_element("album_1_rating") do |element|
  element.visual_effect(:highlight)
end</code></pre>
<p>That made me happy.</p>
<p>In thinking more about the situation, I&#8217;m not sure this is really such a great method.  It works, but I can&#8217;t help but feel there&#8217;s some better way of going about this.  I could simply use the page append method, to do something like:</p>
<pre><code>page &lt;&lt; "if ($(id)) {"
  page["album_1_rating"].visual_effect(:highlight)
page &lt;&lt; "}"</code></pre>
<p>But at least if nothing else, my <tt>if_element</tt> method makes this a lot cleaner looking.  So really this is just some syntactic sugar, and I wish at least I could make it more of a general purpose <tt>if</tt> statement, but since I don&#8217;t need that sort of funcationality at this point, <acronym title="You're not going to need it.">YAGNI</acronym> tells me to leave that hurdle for another day.</p>
]]></content:encoded>
			<wfw:commentRss>http://kill-0.com/duplo/2008/05/01/javascript-conditionals-from-erb/feed/</wfw:commentRss>
		<slash:comments>1</slash:comments>
		</item>
	</channel>
</rss>
