<?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>Mark Needham &#187; Javascript</title>
	<atom:link href="http://www.markhneedham.com/blog/category/javascript/feed/" rel="self" type="application/rss+xml" />
	<link>http://www.markhneedham.com/blog</link>
	<description>Thoughts on Software Development</description>
	<lastBuildDate>Mon, 13 Feb 2012 21:25:48 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.3.1</generator>
		<item>
		<title>jQuery: Collecting the results from a collection of asynchronous requests</title>
		<link>http://www.markhneedham.com/blog/2011/09/25/jquery-collecting-the-results-from-a-collection-of-asynchronous-requests/</link>
		<comments>http://www.markhneedham.com/blog/2011/09/25/jquery-collecting-the-results-from-a-collection-of-asynchronous-requests/#comments</comments>
		<pubDate>Sun, 25 Sep 2011 09:26:19 +0000</pubDate>
		<dc:creator>Mark Needham</dc:creator>
				<category><![CDATA[Javascript]]></category>
		<category><![CDATA[jQuery]]></category>

		<guid isPermaLink="false">http://www.markhneedham.com/blog/?p=3732</guid>
		<description><![CDATA[Liz and I recently spent some time building a pair stair to show how long ago people had paired with each other and one of the things we had to do was make AJAX requests to get the pairing data for each person and then collate it all to build the stair. The original attempt [...]]]></description>
			<content:encoded><![CDATA[<p><a href="http://lizdouglass.wordpress.com/">Liz</a> and I recently spent some time building a pair stair to show how long ago people had paired with each other and one of the things we had to do was make AJAX requests to get the pairing data for each person and then collate it all to build the stair.</p>
<div align="center">
<img src="http://www.markhneedham.com/blog/wp-content/uploads/2011/09/pair-stair.gif" alt="Pair stair" title="pair-stair.gif" border="0" width="545" height="341" />
</div>
<p>The original attempt to do this looked a bit like this:</p>

<div class="wp_syntax"><div class="code"><pre class="javascript" style="font-family:monospace;"><span style="color: #003366; font-weight: bold;">var</span> people <span style="color: #339933;">=</span> <span style="color: #009900;">&#91;</span><span style="color: #3366CC;">&quot;Marc&quot;</span><span style="color: #339933;">,</span> <span style="color: #3366CC;">&quot;Liz&quot;</span><span style="color: #339933;">,</span> <span style="color: #3366CC;">&quot;Ken&quot;</span><span style="color: #339933;">,</span> <span style="color: #3366CC;">&quot;Duncan&quot;</span><span style="color: #339933;">,</span> <span style="color: #3366CC;">&quot;Uday&quot;</span><span style="color: #339933;">,</span> <span style="color: #3366CC;">&quot;Mark&quot;</span><span style="color: #339933;">,</span> <span style="color: #3366CC;">&quot;Charles&quot;</span><span style="color: #009900;">&#93;</span><span style="color: #339933;">;</span>
&nbsp;
<span style="color: #003366; font-weight: bold;">var</span> grid <span style="color: #339933;">=</span> <span style="color: #009900;">&#91;</span><span style="color: #009900;">&#93;</span><span style="color: #339933;">;</span>
$.<span style="color: #660066;">each</span><span style="color: #009900;">&#40;</span>people<span style="color: #339933;">,</span> <span style="color: #003366; font-weight: bold;">function</span><span style="color: #009900;">&#40;</span>index<span style="color: #339933;">,</span> person<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
  $.<span style="color: #660066;">getJSON</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">'/git/pairs/'</span> <span style="color: #339933;">+</span> person<span style="color: #339933;">,</span> <span style="color: #003366; font-weight: bold;">function</span><span style="color: #009900;">&#40;</span>data<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
    <span style="color: #006600; font-style: italic;">// parse data and create somethingCool</span>
    grid.<span style="color: #660066;">push</span><span style="color: #009900;">&#40;</span>somethingCool<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
  <span style="color: #009900;">&#125;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>  
<span style="color: #009900;">&#125;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
<span style="color: #006600; font-style: italic;">// do something with grid</span></pre></div></div>

<p>When we try to do something with <cite>grid</cite> it is of course empty because we&#8217;ve attempted to access it before all of the callbacks (which populate it) have returned.</p>
<p>Pedro Teixeira has <a href="http://metaduck.com/post/2675027550/asynchronous-iteration-patterns-in-node-js">a nice blog post which explains how to solve this problem in node.js</a> and we can use the same pattern here.</p>
<p>We need to write our own looping mechanism which is able to determine when the last callback has returned.</p>
<p>This is done by creating a copy of the <cite>people</cite> array and then manually iterating through it using <cite>shift</cite>.</p>

<div class="wp_syntax"><div class="code"><pre class="javascript" style="font-family:monospace;"><span style="color: #003366; font-weight: bold;">var</span> people <span style="color: #339933;">=</span> <span style="color: #009900;">&#91;</span><span style="color: #3366CC;">&quot;Marc&quot;</span><span style="color: #339933;">,</span> <span style="color: #3366CC;">&quot;Liz&quot;</span><span style="color: #339933;">,</span> <span style="color: #3366CC;">&quot;Ken&quot;</span><span style="color: #339933;">,</span> <span style="color: #3366CC;">&quot;Duncan&quot;</span><span style="color: #339933;">,</span> <span style="color: #3366CC;">&quot;Uday&quot;</span><span style="color: #339933;">,</span> <span style="color: #3366CC;">&quot;Mark&quot;</span><span style="color: #339933;">,</span> <span style="color: #3366CC;">&quot;Charles&quot;</span><span style="color: #009900;">&#93;</span><span style="color: #339933;">;</span>
<span style="color: #003366; font-weight: bold;">var</span> peopleCopy <span style="color: #339933;">=</span> people.<span style="color: #660066;">slice</span><span style="color: #009900;">&#40;</span><span style="color: #CC0000;">0</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">,</span> grid <span style="color: #339933;">=</span> <span style="color: #009900;">&#91;</span><span style="color: #009900;">&#93;</span><span style="color: #339933;">;</span>
<span style="color: #009900;">&#40;</span><span style="color: #003366; font-weight: bold;">function</span> getPairs<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
  <span style="color: #003366; font-weight: bold;">var</span> person <span style="color: #339933;">=</span> peopleCopy.<span style="color: #660066;">shift</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
  <span style="color: #000066; font-weight: bold;">if</span><span style="color: #009900;">&#40;</span>peopleCopy.<span style="color: #660066;">length</span> <span style="color: #339933;">==</span> <span style="color: #CC0000;">0</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
    <span style="color: #006600; font-style: italic;">// do something with grid</span>
  <span style="color: #009900;">&#125;</span> <span style="color: #000066; font-weight: bold;">else</span> <span style="color: #009900;">&#123;</span>
    $.<span style="color: #660066;">getJSON</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">&quot;/git/pairs&quot;</span> <span style="color: #339933;">+</span> person<span style="color: #339933;">,</span> <span style="color: #003366; font-weight: bold;">function</span><span style="color: #009900;">&#40;</span>data<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
      <span style="color: #006600; font-style: italic;">// parse data and create somethingCool</span>
      grid.<span style="color: #660066;">push</span><span style="color: #009900;">&#40;</span>somethingCool<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
      getPairs<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>		
    <span style="color: #009900;">&#125;</span><span style="color: #009900;">&#41;</span>						
  <span style="color: #009900;">&#125;</span>
<span style="color: #009900;">&#125;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span></pre></div></div>

<p>I tried to extract the asynchronous looping and ended up with the following function:</p>

<div class="wp_syntax"><div class="code"><pre class="javascript" style="font-family:monospace;"><span style="color: #003366; font-weight: bold;">function</span> asyncLoop<span style="color: #009900;">&#40;</span>collection<span style="color: #339933;">,</span> seedResult<span style="color: #339933;">,</span> loopFn<span style="color: #339933;">,</span> completionFn<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
  <span style="color: #003366; font-weight: bold;">var</span> copy <span style="color: #339933;">=</span> collection.<span style="color: #660066;">slice</span><span style="color: #009900;">&#40;</span><span style="color: #CC0000;">0</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
  <span style="color: #009900;">&#40;</span><span style="color: #003366; font-weight: bold;">function</span> loop<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
    <span style="color: #003366; font-weight: bold;">var</span> <span style="color: #000066; font-weight: bold;">item</span> <span style="color: #339933;">=</span> copy.<span style="color: #660066;">shift</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
    <span style="color: #000066; font-weight: bold;">if</span><span style="color: #009900;">&#40;</span>copy.<span style="color: #660066;">length</span> <span style="color: #339933;">==</span> <span style="color: #CC0000;">0</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
      completionFn<span style="color: #009900;">&#40;</span>seedResult<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
    <span style="color: #009900;">&#125;</span> <span style="color: #000066; font-weight: bold;">else</span> <span style="color: #009900;">&#123;</span>
      loopFn<span style="color: #009900;">&#40;</span><span style="color: #000066; font-weight: bold;">item</span><span style="color: #339933;">,</span> seedResult<span style="color: #339933;">,</span> loop<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
    <span style="color: #009900;">&#125;</span>	
  <span style="color: #009900;">&#125;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>	
<span style="color: #009900;">&#125;</span></pre></div></div>

<p>Which could be called like this:</p>

<div class="wp_syntax"><div class="code"><pre class="javascript" style="font-family:monospace;"><span style="color: #003366; font-weight: bold;">var</span> people <span style="color: #339933;">=</span> <span style="color: #009900;">&#91;</span><span style="color: #3366CC;">&quot;Marc&quot;</span><span style="color: #339933;">,</span> <span style="color: #3366CC;">&quot;Liz&quot;</span><span style="color: #339933;">,</span> <span style="color: #3366CC;">&quot;Ken&quot;</span><span style="color: #339933;">,</span> <span style="color: #3366CC;">&quot;Duncan&quot;</span><span style="color: #339933;">,</span> <span style="color: #3366CC;">&quot;Uday&quot;</span><span style="color: #339933;">,</span> <span style="color: #3366CC;">&quot;Mark&quot;</span><span style="color: #339933;">,</span> <span style="color: #3366CC;">&quot;Charles&quot;</span><span style="color: #009900;">&#93;</span><span style="color: #339933;">;</span>
asyncLoop<span style="color: #009900;">&#40;</span>people<span style="color: #339933;">,</span> <span style="color: #009900;">&#91;</span><span style="color: #009900;">&#93;</span><span style="color: #339933;">,</span> <span style="color: #003366; font-weight: bold;">function</span><span style="color: #009900;">&#40;</span><span style="color: #000066;">name</span><span style="color: #339933;">,</span> grid<span style="color: #339933;">,</span> callBackFn<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
  <span style="color: #006600; font-style: italic;">// parse data and create something cool</span>
  grid.<span style="color: #660066;">push</span><span style="color: #009900;">&#40;</span>somethingCool<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
  callBackFn<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #009900;">&#125;</span><span style="color: #339933;">,</span> <span style="color: #003366; font-weight: bold;">function</span><span style="color: #009900;">&#40;</span>grid<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
  <span style="color: #006600; font-style: italic;">// do something with grid</span>
<span style="color: #009900;">&#125;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span></pre></div></div>

<p>I&#8217;m not sure that it reads that much clearer but it does push some of the boiler plate code away.</p>
]]></content:encoded>
			<wfw:commentRss>http://www.markhneedham.com/blog/2011/09/25/jquery-collecting-the-results-from-a-collection-of-asynchronous-requests/feed/</wfw:commentRss>
		<slash:comments>5</slash:comments>
		</item>
		<item>
		<title>node.js: child_process.exec not returning all results</title>
		<link>http://www.markhneedham.com/blog/2011/09/22/node-js-child_process-exec-not-returning-all-results/</link>
		<comments>http://www.markhneedham.com/blog/2011/09/22/node-js-child_process-exec-not-returning-all-results/#comments</comments>
		<pubDate>Thu, 22 Sep 2011 19:55:45 +0000</pubDate>
		<dc:creator>Mark Needham</dc:creator>
				<category><![CDATA[Javascript]]></category>
		<category><![CDATA[node.js]]></category>

		<guid isPermaLink="false">http://www.markhneedham.com/blog/?p=3727</guid>
		<description><![CDATA[I&#8217;ve been playing around with some node.js code to get each of the commits from our git repository but noticed that it didn&#8217;t seem to be returning me all the results. I had the following code: var exec = require&#40;'child_process'&#41;.exec; &#160; var gitRepository = '/some/local/path'; &#160; exec&#40;'cd ' + gitRepository + ' &#38;&#38; git log [...]]]></description>
			<content:encoded><![CDATA[<p>I&#8217;ve been playing around with some node.js code to get each of the commits from our git repository but noticed that it didn&#8217;t seem to be returning me all the results.</p>
<p>I had the following code:</p>

<div class="wp_syntax"><div class="code"><pre class="javascript" style="font-family:monospace;"><span style="color: #003366; font-weight: bold;">var</span> exec <span style="color: #339933;">=</span> require<span style="color: #009900;">&#40;</span><span style="color: #3366CC;">'child_process'</span><span style="color: #009900;">&#41;</span>.<span style="color: #660066;">exec</span><span style="color: #339933;">;</span>
&nbsp;
<span style="color: #003366; font-weight: bold;">var</span> gitRepository <span style="color: #339933;">=</span> <span style="color: #3366CC;">'/some/local/path'</span><span style="color: #339933;">;</span>
&nbsp;
exec<span style="color: #009900;">&#40;</span><span style="color: #3366CC;">'cd '</span> <span style="color: #339933;">+</span> gitRepository <span style="color: #339933;">+</span> <span style="color: #3366CC;">' &amp;&amp; git log --pretty=format:&quot;%H | %ad | %s%d&quot; --date=raw '</span><span style="color: #339933;">,</span> <span style="color: #003366; font-weight: bold;">function</span><span style="color: #009900;">&#40;</span>error<span style="color: #339933;">,</span> stdout<span style="color: #339933;">,</span> stderror<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
  <span style="color: #003366; font-weight: bold;">var</span> commits <span style="color: #339933;">=</span> stdout.<span style="color: #660066;">split</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">&quot;<span style="color: #000099; font-weight: bold;">\n</span>&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
  <span style="color: #006600; font-style: italic;">// do some stuff with commits</span>
<span style="color: #009900;">&#125;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span></pre></div></div>

<p>We have around 2000 commits in the repository but I was only getting back 1600 of them when I checked the <cite>length</cite> of <cite>commits</cite>.</p>
<p>Eventually I decided to print out what was in <cite>error</cite> and got the following message:</p>

<div class="wp_syntax"><div class="code"><pre class="text" style="font-family:monospace;">error: Error: maxBuffer exceeded.</pre></div></div>

<p>Going back to <a href="http://nodejs.org/docs/v0.4.8/api/all.html#child_process.exec">the documentation</a> revealed my mistake:</p>
<blockquote><p>
maxBuffer specifies the largest amount of data allowed on stdout or stderr &#8211; if this value is exceeded then the child process is killed.</p>
<p>The default options are</p>
<p>{ encoding: &#8216;utf8&#8242;,<br />
  timeout: 0,<br />
  maxBuffer: 200*1024,<br />
  killSignal: &#8216;SIGTERM&#8217;,<br />
  cwd: null,<br />
  env: null }</p>
</blockquote>
<p>The limit is 2048000 which is around about the number of bytes being returned when I get to 1600 commits. </p>
<p>Changing the code to increase the buffer sorts it out:</p>

<div class="wp_syntax"><div class="code"><pre class="javascript" style="font-family:monospace;"><span style="color: #003366; font-weight: bold;">var</span> exec <span style="color: #339933;">=</span> require<span style="color: #009900;">&#40;</span><span style="color: #3366CC;">'child_process'</span><span style="color: #009900;">&#41;</span>.<span style="color: #660066;">exec</span><span style="color: #339933;">;</span>
&nbsp;
<span style="color: #003366; font-weight: bold;">var</span> gitRepository <span style="color: #339933;">=</span> <span style="color: #3366CC;">'/some/local/path'</span><span style="color: #339933;">;</span>
&nbsp;
exec<span style="color: #009900;">&#40;</span><span style="color: #3366CC;">'cd '</span> <span style="color: #339933;">+</span> gitRepository <span style="color: #339933;">+</span> <span style="color: #3366CC;">' &amp;&amp; git log --pretty=format:&quot;%H | %ad | %s%d&quot; --date=raw '</span><span style="color: #339933;">,</span> <span style="color: #009900;">&#123;</span>maxBuffer<span style="color: #339933;">:</span> <span style="color: #CC0000;">500</span><span style="color: #339933;">*</span><span style="color: #CC0000;">1024</span><span style="color: #009900;">&#125;</span><span style="color: #339933;">,</span> <span style="color: #003366; font-weight: bold;">function</span><span style="color: #009900;">&#40;</span>error<span style="color: #339933;">,</span> stdout<span style="color: #339933;">,</span> stderror<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
  <span style="color: #003366; font-weight: bold;">var</span> commits <span style="color: #339933;">=</span> stdout.<span style="color: #660066;">split</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">&quot;<span style="color: #000099; font-weight: bold;">\n</span>&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
  <span style="color: #006600; font-style: italic;">// do some stuff with commits</span>
<span style="color: #009900;">&#125;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span></pre></div></div>

]]></content:encoded>
			<wfw:commentRss>http://www.markhneedham.com/blog/2011/09/22/node-js-child_process-exec-not-returning-all-results/feed/</wfw:commentRss>
		<slash:comments>1</slash:comments>
		</item>
		<item>
		<title>Javascript: Internet Explorer 8 &#8211; trim() leads to &#8216;Object doesn&#8217;t support this property or method&#8217; error</title>
		<link>http://www.markhneedham.com/blog/2011/09/13/javascript-internet-explorer-8-trim-leads-to-object-doesnt-support-this-property-or-method-error/</link>
		<comments>http://www.markhneedham.com/blog/2011/09/13/javascript-internet-explorer-8-trim-leads-to-object-doesnt-support-this-property-or-method-error/#comments</comments>
		<pubDate>Tue, 13 Sep 2011 13:33:43 +0000</pubDate>
		<dc:creator>Mark Needham</dc:creator>
				<category><![CDATA[Javascript]]></category>

		<guid isPermaLink="false">http://www.markhneedham.com/blog/?p=3720</guid>
		<description><![CDATA[We make use of the Javascript trim() function in our application but didn&#8217;t realise that it isn&#8217;t implemented by Internet Explorer until version 9. This led to the following error on IE8 when we used it: Message: Object doesn&#8217;t support this property or method Line: 18 Char: 13 Code: 0 URI: http://our.app/file.js There&#8217;s a stackoverflow [...]]]></description>
			<content:encoded><![CDATA[<p>We make use of the Javascript <cite><a href="https://developer.mozilla.org/en/JavaScript/Reference/Global_Objects/String/trim">trim()</a></cite> function in our application but didn&#8217;t realise that it isn&#8217;t implemented by Internet Explorer until version 9.</p>
<p>This led to the following error on IE8 when we used it:</p>
<blockquote><p>
Message: Object doesn&#8217;t support this property or method<br />
Line: 18<br />
Char: 13<br />
Code: 0<br />
URI: http://our.app/file.js
</p></blockquote>
<p>There&#8217;s a <a href="http://stackoverflow.com/questions/2308134/trim-in-javascript-not-working-in-ie">stackoverflow thread</a> suggesting some different ways of implementing your own &#8216;trim()&#8217; method but since we&#8217;re using jQuery already we decided to just use the &#8216;$.trim()&#8217; function from there.</p>
<p>Therefore:</p>

<div class="wp_syntax"><div class="code"><pre class="javascript" style="font-family:monospace;"><span style="color: #003366; font-weight: bold;">var</span> cleaned <span style="color: #339933;">=</span> ourString.<span style="color: #660066;">trim</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span></pre></div></div>

<p>becomes:</p>

<div class="wp_syntax"><div class="code"><pre class="javascript" style="font-family:monospace;"><span style="color: #003366; font-weight: bold;">var</span> cleaned <span style="color: #339933;">=</span> $.<span style="color: #660066;">trim</span><span style="color: #009900;">&#40;</span>ourString<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span></pre></div></div>

<p>I&#8217;m sure I must have come across this before but I don&#8217;t remember when!</p>
]]></content:encoded>
			<wfw:commentRss>http://www.markhneedham.com/blog/2011/09/13/javascript-internet-explorer-8-trim-leads-to-object-doesnt-support-this-property-or-method-error/feed/</wfw:commentRss>
		<slash:comments>2</slash:comments>
		</item>
		<item>
		<title>Learning node.js: Step</title>
		<link>http://www.markhneedham.com/blog/2011/09/11/learning-node-js-step/</link>
		<comments>http://www.markhneedham.com/blog/2011/09/11/learning-node-js-step/#comments</comments>
		<pubDate>Sun, 11 Sep 2011 22:37:15 +0000</pubDate>
		<dc:creator>Mark Needham</dc:creator>
				<category><![CDATA[Javascript]]></category>
		<category><![CDATA[node.js]]></category>

		<guid isPermaLink="false">http://www.markhneedham.com/blog/?p=3716</guid>
		<description><![CDATA[I&#8217;ve been playing around with node.js to generate some graphs from our git repository which effectively meant chaining together a bunch of shell commands to give me the repository data in the format I wanted. I was able to do this by making use of child_process which comes with the core library. The first version [...]]]></description>
			<content:encoded><![CDATA[<p>I&#8217;ve been playing around with node.js to generate some graphs from our git repository which effectively meant chaining together a bunch of shell commands to give me the repository data in the format I wanted.</p>
<p>I was able to do this by making use of <cite><a href="http://nodejs.org/docs/v0.4.8/api/all.html#child_process.exec">child_process</a></cite> which comes with the core library.</p>
<p>The first version looked like this:</p>

<div class="wp_syntax"><div class="code"><pre class="javascript" style="font-family:monospace;"><span style="color: #003366; font-weight: bold;">var</span> exec <span style="color: #339933;">=</span> require<span style="color: #009900;">&#40;</span><span style="color: #3366CC;">'child_process'</span><span style="color: #009900;">&#41;</span>.<span style="color: #660066;">exec</span><span style="color: #339933;">,</span> _ <span style="color: #339933;">=</span> require<span style="color: #009900;">&#40;</span><span style="color: #3366CC;">&quot;underscore&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
...
<span style="color: #003366; font-weight: bold;">function</span> parseCommitsFromRepository<span style="color: #009900;">&#40;</span>fn<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
  <span style="color: #003366; font-weight: bold;">var</span> gitRepository <span style="color: #339933;">=</span> <span style="color: #3366CC;">&quot;/tmp/core&quot;</span><span style="color: #339933;">;</span>
  <span style="color: #003366; font-weight: bold;">var</span> gitPlayArea <span style="color: #339933;">=</span> <span style="color: #3366CC;">&quot;/tmp/&quot;</span> <span style="color: #339933;">+</span> <span style="color: #003366; font-weight: bold;">new</span> Date<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span>.<span style="color: #660066;">getTime</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
  exec<span style="color: #009900;">&#40;</span><span style="color: #3366CC;">'cd '</span> <span style="color: #339933;">+</span> gitRepository <span style="color: #339933;">+</span> <span style="color: #3366CC;">' &amp;&amp; git reset HEAD'</span><span style="color: #339933;">,</span> <span style="color: #003366; font-weight: bold;">function</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
    exec<span style="color: #009900;">&#40;</span><span style="color: #3366CC;">'git clone '</span> <span style="color: #339933;">+</span> gitRepository <span style="color: #339933;">+</span> <span style="color: #3366CC;">' '</span> <span style="color: #339933;">+</span> gitPlayArea<span style="color: #339933;">,</span> <span style="color: #003366; font-weight: bold;">function</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
      exec<span style="color: #009900;">&#40;</span><span style="color: #3366CC;">'cd '</span> <span style="color: #339933;">+</span> gitPlayArea <span style="color: #339933;">+</span> <span style="color: #3366CC;">' &amp;&amp; git log --pretty=format:&quot;%H | %ad | %s%d&quot; --date=raw'</span><span style="color: #339933;">,</span> <span style="color: #003366; font-weight: bold;">function</span><span style="color: #009900;">&#40;</span>blank<span style="color: #339933;">,</span> gitEntries<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
        <span style="color: #003366; font-weight: bold;">var</span> commits <span style="color: #339933;">=</span> _<span style="color: #009900;">&#40;</span>gitEntries.<span style="color: #660066;">split</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">&quot;<span style="color: #000099; font-weight: bold;">\n</span>&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span>.<span style="color: #660066;">chain</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span>
                        .<span style="color: #660066;">filter</span><span style="color: #009900;">&#40;</span><span style="color: #003366; font-weight: bold;">function</span><span style="color: #009900;">&#40;</span><span style="color: #000066; font-weight: bold;">item</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span> <span style="color: #000066; font-weight: bold;">return</span> <span style="color: #000066; font-weight: bold;">item</span> <span style="color: #339933;">!=</span> <span style="color: #3366CC;">&quot;&quot;</span><span style="color: #339933;">;</span> <span style="color: #009900;">&#125;</span><span style="color: #009900;">&#41;</span>
                        .<span style="color: #660066;">map</span><span style="color: #009900;">&#40;</span><span style="color: #003366; font-weight: bold;">function</span><span style="color: #009900;">&#40;</span><span style="color: #000066; font-weight: bold;">item</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span> <span style="color: #000066; font-weight: bold;">return</span> <span style="color: #000066; font-weight: bold;">item</span>.<span style="color: #660066;">split</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">&quot;|&quot;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#125;</span><span style="color: #009900;">&#41;</span>
                        .<span style="color: #660066;">filter</span><span style="color: #009900;">&#40;</span><span style="color: #003366; font-weight: bold;">function</span><span style="color: #009900;">&#40;</span>theSplit<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span> <span style="color: #000066; font-weight: bold;">return</span> theSplit <span style="color: #339933;">!==</span> undefined <span style="color: #339933;">&amp;&amp;</span> theSplit<span style="color: #009900;">&#91;</span><span style="color: #CC0000;">1</span><span style="color: #009900;">&#93;</span> <span style="color: #339933;">!==</span> undefined <span style="color: #339933;">&amp;&amp;</span> theSplit<span style="color: #009900;">&#91;</span><span style="color: #CC0000;">2</span><span style="color: #009900;">&#93;</span> <span style="color: #339933;">!==</span> undefined<span style="color: #339933;">;</span> <span style="color: #009900;">&#125;</span><span style="color: #009900;">&#41;</span>
                        .<span style="color: #660066;">map</span><span style="color: #009900;">&#40;</span><span style="color: #003366; font-weight: bold;">function</span><span style="color: #009900;">&#40;</span>theSplit<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>  
                          <span style="color: #003366; font-weight: bold;">var</span> date <span style="color: #339933;">=</span> <span style="color: #003366; font-weight: bold;">new</span> Date<span style="color: #009900;">&#40;</span>theSplit<span style="color: #009900;">&#91;</span><span style="color: #CC0000;">1</span><span style="color: #009900;">&#93;</span>.<span style="color: #660066;">trim</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span>.<span style="color: #660066;">split</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">&quot; &quot;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#91;</span><span style="color: #CC0000;">0</span><span style="color: #009900;">&#93;</span><span style="color: #339933;">*</span><span style="color: #CC0000;">1000</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
                          <span style="color: #000066; font-weight: bold;">return</span> <span style="color: #009900;">&#123;</span>message<span style="color: #339933;">:</span> theSplit<span style="color: #009900;">&#91;</span><span style="color: #CC0000;">2</span><span style="color: #009900;">&#93;</span>.<span style="color: #660066;">trim</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">,</span> date<span style="color: #339933;">:</span> date.<span style="color: #660066;">toDateString</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">,</span> time <span style="color: #339933;">:</span> date.<span style="color: #660066;">toTimeString</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#125;</span><span style="color: #339933;">;</span> <span style="color: #009900;">&#125;</span><span style="color: #009900;">&#41;</span>
                        .<span style="color: #660066;">value</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>			
        fn<span style="color: #009900;">&#40;</span>commits<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
      <span style="color: #009900;">&#125;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>		
    <span style="color: #009900;">&#125;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
  <span style="color: #009900;">&#125;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #009900;">&#125;</span></pre></div></div>

<p>node.js has an asynchronous programming model so the majority of the time we have to pass callbacks to other functions which get called when the asynchronous computation has completed.</p>
<p>In this case there&#8217;s an order dependency in the <cite>parseCommitsFromRepository</cite> function such that we need to nest the second call to <cite>exec</cite>  inside the callback from the first call.</p>
<p>i.e. we don&#8217;t want to get the log of the repository before we&#8217;ve cloned the repository to the location that we&#8217;re trying to get that log from.</p>
<p>As you create more and more order dependencies between asynchronous functions the nesting becomes greater and the code moves more and more to the right hand side of the screen.</p>
<p>I came across the <cite><a href="https://github.com/creationix/step">Step</a></cite> library which allows you to stack up functions and have the results from each one get passed on to the next.</p>
<p>I decided to try it in my code and it ended up looking like this:</p>

<div class="wp_syntax"><div class="code"><pre class="javascript" style="font-family:monospace;"><span style="color: #003366; font-weight: bold;">function</span> parseCommitsFromRepository<span style="color: #009900;">&#40;</span>fn<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>	
  <span style="color: #003366; font-weight: bold;">var</span> gitRepository <span style="color: #339933;">=</span> <span style="color: #3366CC;">&quot;/tmp/core&quot;</span><span style="color: #339933;">;</span>
  <span style="color: #003366; font-weight: bold;">var</span> gitPlayArea <span style="color: #339933;">=</span> <span style="color: #3366CC;">&quot;/tmp/&quot;</span> <span style="color: #339933;">+</span> <span style="color: #003366; font-weight: bold;">new</span> Date<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span>.<span style="color: #660066;">getTime</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>	
  Step<span style="color: #009900;">&#40;</span>
    <span style="color: #003366; font-weight: bold;">function</span> getRepositoryUpToDate<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span> exec<span style="color: #009900;">&#40;</span><span style="color: #3366CC;">'cd '</span> <span style="color: #339933;">+</span> gitRepository <span style="color: #339933;">+</span> <span style="color: #3366CC;">' &amp;&amp; git reset HEAD'</span><span style="color: #339933;">,</span> <span style="color: #000066; font-weight: bold;">this</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span> <span style="color: #009900;">&#125;</span><span style="color: #339933;">,</span>
    <span style="color: #003366; font-weight: bold;">function</span> cloneRepository<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span>       <span style="color: #009900;">&#123;</span> exec<span style="color: #009900;">&#40;</span><span style="color: #3366CC;">'git clone '</span> <span style="color: #339933;">+</span> gitRepository <span style="color: #339933;">+</span> <span style="color: #3366CC;">' '</span> <span style="color: #339933;">+</span> gitPlayArea<span style="color: #339933;">,</span> <span style="color: #000066; font-weight: bold;">this</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span> <span style="color: #009900;">&#125;</span><span style="color: #339933;">,</span>
    <span style="color: #003366; font-weight: bold;">function</span> getGitEntries<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span>         <span style="color: #009900;">&#123;</span> exec<span style="color: #009900;">&#40;</span><span style="color: #3366CC;">'cd '</span> <span style="color: #339933;">+</span> gitPlayArea <span style="color: #339933;">+</span> <span style="color: #3366CC;">' &amp;&amp; git log --pretty=format:&quot;%H | %ad | %s%d&quot; --date=raw'</span><span style="color: #339933;">,</span> <span style="color: #000066; font-weight: bold;">this</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span> <span style="color: #009900;">&#125;</span><span style="color: #339933;">,</span>
    <span style="color: #003366; font-weight: bold;">function</span> handleResponse<span style="color: #009900;">&#40;</span>blank<span style="color: #339933;">,</span> gitEntries<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
      <span style="color: #003366; font-weight: bold;">var</span> commits <span style="color: #339933;">=</span> _<span style="color: #009900;">&#40;</span>gitEntries.<span style="color: #660066;">split</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">&quot;<span style="color: #000099; font-weight: bold;">\n</span>&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span>.<span style="color: #660066;">chain</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span>
                      .<span style="color: #660066;">filter</span><span style="color: #009900;">&#40;</span><span style="color: #003366; font-weight: bold;">function</span><span style="color: #009900;">&#40;</span><span style="color: #000066; font-weight: bold;">item</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span> <span style="color: #000066; font-weight: bold;">return</span> <span style="color: #000066; font-weight: bold;">item</span> <span style="color: #339933;">!=</span> <span style="color: #3366CC;">&quot;&quot;</span><span style="color: #339933;">;</span> <span style="color: #009900;">&#125;</span><span style="color: #009900;">&#41;</span>
                      .<span style="color: #660066;">map</span><span style="color: #009900;">&#40;</span><span style="color: #003366; font-weight: bold;">function</span><span style="color: #009900;">&#40;</span><span style="color: #000066; font-weight: bold;">item</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span> <span style="color: #000066; font-weight: bold;">return</span> <span style="color: #000066; font-weight: bold;">item</span>.<span style="color: #660066;">split</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">&quot;|&quot;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#125;</span><span style="color: #009900;">&#41;</span>
                      .<span style="color: #660066;">filter</span><span style="color: #009900;">&#40;</span><span style="color: #003366; font-weight: bold;">function</span><span style="color: #009900;">&#40;</span>theSplit<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span> <span style="color: #000066; font-weight: bold;">return</span> theSplit <span style="color: #339933;">!==</span> undefined <span style="color: #339933;">&amp;&amp;</span> theSplit<span style="color: #009900;">&#91;</span><span style="color: #CC0000;">1</span><span style="color: #009900;">&#93;</span> <span style="color: #339933;">!==</span> undefined <span style="color: #339933;">&amp;&amp;</span> theSplit<span style="color: #009900;">&#91;</span><span style="color: #CC0000;">2</span><span style="color: #009900;">&#93;</span> <span style="color: #339933;">!==</span> undefined<span style="color: #339933;">;</span> <span style="color: #009900;">&#125;</span><span style="color: #009900;">&#41;</span>
                      .<span style="color: #660066;">map</span><span style="color: #009900;">&#40;</span><span style="color: #003366; font-weight: bold;">function</span><span style="color: #009900;">&#40;</span>theSplit<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>  
                        <span style="color: #003366; font-weight: bold;">var</span> date <span style="color: #339933;">=</span> <span style="color: #003366; font-weight: bold;">new</span> Date<span style="color: #009900;">&#40;</span>theSplit<span style="color: #009900;">&#91;</span><span style="color: #CC0000;">1</span><span style="color: #009900;">&#93;</span>.<span style="color: #660066;">trim</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span>.<span style="color: #660066;">split</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">&quot; &quot;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#91;</span><span style="color: #CC0000;">0</span><span style="color: #009900;">&#93;</span><span style="color: #339933;">*</span><span style="color: #CC0000;">1000</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
                        <span style="color: #000066; font-weight: bold;">return</span> <span style="color: #009900;">&#123;</span>message<span style="color: #339933;">:</span> theSplit<span style="color: #009900;">&#91;</span><span style="color: #CC0000;">2</span><span style="color: #009900;">&#93;</span>.<span style="color: #660066;">trim</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">,</span> date<span style="color: #339933;">:</span> date.<span style="color: #660066;">toDateString</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">,</span> time <span style="color: #339933;">:</span> date.<span style="color: #660066;">toTimeString</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#125;</span><span style="color: #339933;">;</span> <span style="color: #009900;">&#125;</span><span style="color: #009900;">&#41;</span>
                      .<span style="color: #660066;">value</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>			
      fn<span style="color: #009900;">&#40;</span>commits<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
    <span style="color: #009900;">&#125;</span>
  <span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>	
<span style="color: #009900;">&#125;</span></pre></div></div>

<p>An interesting side effect of using this approach is that we can describe what each <cite>exec</cite> call is doing in the name of the function that executes it.</p>
<p>Another neat thing about this library is that I can easily wrap those functions inside a logging function if I want to see on the console where the process has got up to:</p>

<div class="wp_syntax"><div class="code"><pre class="javascript" style="font-family:monospace;"><span style="color: #003366; font-weight: bold;">function</span> log<span style="color: #009900;">&#40;</span>message<span style="color: #339933;">,</span> fn<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
  <span style="color: #000066; font-weight: bold;">return</span> <span style="color: #003366; font-weight: bold;">function</span> logMe<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
    console.<span style="color: #660066;">log</span><span style="color: #009900;">&#40;</span><span style="color: #003366; font-weight: bold;">new</span> Date<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span>.<span style="color: #660066;">toString</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #339933;">+</span> <span style="color: #3366CC;">&quot;: &quot;</span> <span style="color: #339933;">+</span> message<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
     fn.<span style="color: #660066;">apply</span><span style="color: #009900;">&#40;</span><span style="color: #000066; font-weight: bold;">this</span><span style="color: #339933;">,</span> arguments<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
  <span style="color: #009900;">&#125;</span>
<span style="color: #009900;">&#125;</span></pre></div></div>


<div class="wp_syntax"><div class="code"><pre class="javascript" style="font-family:monospace;"><span style="color: #003366; font-weight: bold;">function</span> parseCommitsFromRepository<span style="color: #009900;">&#40;</span>fn<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>	
  <span style="color: #003366; font-weight: bold;">var</span> gitRepository <span style="color: #339933;">=</span> <span style="color: #3366CC;">&quot;/tmp/core&quot;</span><span style="color: #339933;">;</span>
  <span style="color: #003366; font-weight: bold;">var</span> gitPlayArea <span style="color: #339933;">=</span> <span style="color: #3366CC;">&quot;/tmp/&quot;</span> <span style="color: #339933;">+</span> <span style="color: #003366; font-weight: bold;">new</span> Date<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span>.<span style="color: #660066;">getTime</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>	
  Step<span style="color: #009900;">&#40;</span>
    log<span style="color: #009900;">&#40;</span><span style="color: #3366CC;">&quot;Resetting repository&quot;</span><span style="color: #339933;">,</span> <span style="color: #003366; font-weight: bold;">function</span> getRepositoryUpToDate<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span> exec<span style="color: #009900;">&#40;</span><span style="color: #3366CC;">'cd '</span> <span style="color: #339933;">+</span> gitRepository <span style="color: #339933;">+</span> <span style="color: #3366CC;">' &amp;&amp; git reset HEAD'</span><span style="color: #339933;">,</span> <span style="color: #000066; font-weight: bold;">this</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span> <span style="color: #009900;">&#125;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">,</span>
    log<span style="color: #009900;">&#40;</span><span style="color: #3366CC;">&quot;Cloning repository&quot;</span><span style="color: #339933;">,</span> <span style="color: #003366; font-weight: bold;">function</span> cloneRepository<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span>         <span style="color: #009900;">&#123;</span> exec<span style="color: #009900;">&#40;</span><span style="color: #3366CC;">'git clone '</span> <span style="color: #339933;">+</span> gitRepository <span style="color: #339933;">+</span> <span style="color: #3366CC;">' '</span> <span style="color: #339933;">+</span> gitPlayArea<span style="color: #339933;">,</span> <span style="color: #000066; font-weight: bold;">this</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span> <span style="color: #009900;">&#125;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">,</span>
    log<span style="color: #009900;">&#40;</span><span style="color: #3366CC;">&quot;Getting log&quot;</span><span style="color: #339933;">,</span> <span style="color: #003366; font-weight: bold;">function</span> getGitEntries<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span>                  <span style="color: #009900;">&#123;</span> exec<span style="color: #009900;">&#40;</span><span style="color: #3366CC;">'cd '</span> <span style="color: #339933;">+</span> gitPlayArea <span style="color: #339933;">+</span> <span style="color: #3366CC;">' &amp;&amp; git log --pretty=format:&quot;%H | %ad | %s%d&quot; --date=raw'</span><span style="color: #339933;">,</span> <span style="color: #000066; font-weight: bold;">this</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span> <span style="color: #009900;">&#125;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">,</span>
    log<span style="color: #009900;">&#40;</span><span style="color: #3366CC;">&quot;Processing log&quot;</span><span style="color: #339933;">,</span> <span style="color: #003366; font-weight: bold;">function</span> handleResponse<span style="color: #009900;">&#40;</span>blank<span style="color: #339933;">,</span> gitEntries<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
      <span style="color: #003366; font-weight: bold;">var</span> commits <span style="color: #339933;">=</span> _<span style="color: #009900;">&#40;</span>gitEntries.<span style="color: #660066;">split</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">&quot;<span style="color: #000099; font-weight: bold;">\n</span>&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span>.<span style="color: #660066;">chain</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span>
                      .<span style="color: #660066;">filter</span><span style="color: #009900;">&#40;</span><span style="color: #003366; font-weight: bold;">function</span><span style="color: #009900;">&#40;</span><span style="color: #000066; font-weight: bold;">item</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span> <span style="color: #000066; font-weight: bold;">return</span> <span style="color: #000066; font-weight: bold;">item</span> <span style="color: #339933;">!=</span> <span style="color: #3366CC;">&quot;&quot;</span><span style="color: #339933;">;</span> <span style="color: #009900;">&#125;</span><span style="color: #009900;">&#41;</span>
                      .<span style="color: #660066;">map</span><span style="color: #009900;">&#40;</span><span style="color: #003366; font-weight: bold;">function</span><span style="color: #009900;">&#40;</span><span style="color: #000066; font-weight: bold;">item</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span> <span style="color: #000066; font-weight: bold;">return</span> <span style="color: #000066; font-weight: bold;">item</span>.<span style="color: #660066;">split</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">&quot;|&quot;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#125;</span><span style="color: #009900;">&#41;</span>
                      .<span style="color: #660066;">filter</span><span style="color: #009900;">&#40;</span><span style="color: #003366; font-weight: bold;">function</span><span style="color: #009900;">&#40;</span>theSplit<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span> <span style="color: #000066; font-weight: bold;">return</span> theSplit <span style="color: #339933;">!==</span> undefined <span style="color: #339933;">&amp;&amp;</span> theSplit<span style="color: #009900;">&#91;</span><span style="color: #CC0000;">1</span><span style="color: #009900;">&#93;</span> <span style="color: #339933;">!==</span> undefined <span style="color: #339933;">&amp;&amp;</span> theSplit<span style="color: #009900;">&#91;</span><span style="color: #CC0000;">2</span><span style="color: #009900;">&#93;</span> <span style="color: #339933;">!==</span> undefined<span style="color: #339933;">;</span> <span style="color: #009900;">&#125;</span><span style="color: #009900;">&#41;</span>
                      .<span style="color: #660066;">map</span><span style="color: #009900;">&#40;</span><span style="color: #003366; font-weight: bold;">function</span><span style="color: #009900;">&#40;</span>theSplit<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>  
                        <span style="color: #003366; font-weight: bold;">var</span> date <span style="color: #339933;">=</span> <span style="color: #003366; font-weight: bold;">new</span> Date<span style="color: #009900;">&#40;</span>theSplit<span style="color: #009900;">&#91;</span><span style="color: #CC0000;">1</span><span style="color: #009900;">&#93;</span>.<span style="color: #660066;">trim</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span>.<span style="color: #660066;">split</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">&quot; &quot;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#91;</span><span style="color: #CC0000;">0</span><span style="color: #009900;">&#93;</span><span style="color: #339933;">*</span><span style="color: #CC0000;">1000</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
                        <span style="color: #000066; font-weight: bold;">return</span> <span style="color: #009900;">&#123;</span>message<span style="color: #339933;">:</span> theSplit<span style="color: #009900;">&#91;</span><span style="color: #CC0000;">2</span><span style="color: #009900;">&#93;</span>.<span style="color: #660066;">trim</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">,</span> date<span style="color: #339933;">:</span> date.<span style="color: #660066;">toDateString</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">,</span> time <span style="color: #339933;">:</span> date.<span style="color: #660066;">toTimeString</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#125;</span><span style="color: #339933;">;</span> <span style="color: #009900;">&#125;</span><span style="color: #009900;">&#41;</span>
                      .<span style="color: #660066;">value</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>			
      fn<span style="color: #009900;">&#40;</span>commits<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
    <span style="color: #009900;">&#125;</span><span style="color: #009900;">&#41;</span>
  <span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>	
<span style="color: #009900;">&#125;</span></pre></div></div>

<p>I then get this output when executing the function:</p>

<div class="wp_syntax"><div class="code"><pre class="text" style="font-family:monospace;">Sun Sep 11 2011 23:33:09 GMT+0100 (BST): Resetting repository
Sun Sep 11 2011 23:33:11 GMT+0100 (BST): Cloning repository
Sun Sep 11 2011 23:33:24 GMT+0100 (BST): Getting log
Sun Sep 11 2011 23:33:24 GMT+0100 (BST): Processing log</pre></div></div>

<p>There are more cool ways to use the Step library on the <a href="https://github.com/creationix/step">github page</a> &#8211; what I&#8217;ve described here is only a very simple use case.</p>
]]></content:encoded>
			<wfw:commentRss>http://www.markhneedham.com/blog/2011/09/11/learning-node-js-step/feed/</wfw:commentRss>
		<slash:comments>2</slash:comments>
		</item>
		<item>
		<title>node.js: Building a graph of build times using the Go API</title>
		<link>http://www.markhneedham.com/blog/2011/08/13/node-js-building-a-graph-of-build-times-using-the-go-api/</link>
		<comments>http://www.markhneedham.com/blog/2011/08/13/node-js-building-a-graph-of-build-times-using-the-go-api/#comments</comments>
		<pubDate>Sat, 13 Aug 2011 14:52:25 +0000</pubDate>
		<dc:creator>Mark Needham</dc:creator>
				<category><![CDATA[Javascript]]></category>
		<category><![CDATA[node.js]]></category>

		<guid isPermaLink="false">http://www.markhneedham.com/blog/?p=3700</guid>
		<description><![CDATA[I&#8217;ve been playing around with node.js again and one thing that I wanted to do was take a CSV file generated by the Go API and extract the build times so that we could display it on a graph. Since I don&#8217;t have a Go instance on my machine I created a URL in my [...]]]></description>
			<content:encoded><![CDATA[<p>I&#8217;ve been playing around with node.js again and one thing that I wanted to do was take a CSV file generated by the <a href="http://www.thoughtworks-studios.com/go/2.1/help/Properties_API.html">Go API</a> and extract the build times so that we could display it on a graph.</p>
<p>Since I don&#8217;t have a Go instance on my machine I created a URL in my node application which would mimic the API and return a CSV file.</p>
<p>I&#8217;m using the <a href="http://expressjs.com/">express web framework</a> to take care of some of the plumbing:</p>
<p><em>dashboard.js</em></p>

<div class="wp_syntax"><div class="code"><pre class="javascript" style="font-family:monospace;"><span style="color: #003366; font-weight: bold;">var</span> express <span style="color: #339933;">=</span> require<span style="color: #009900;">&#40;</span><span style="color: #3366CC;">'express'</span><span style="color: #009900;">&#41;</span>
<span style="color: #003366; font-weight: bold;">var</span> app <span style="color: #339933;">=</span> express.<span style="color: #660066;">createServer</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
app.<span style="color: #660066;">get</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">'/fake-go'</span><span style="color: #339933;">,</span> <span style="color: #003366; font-weight: bold;">function</span><span style="color: #009900;">&#40;</span>req<span style="color: #339933;">,</span> res<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
  fs.<span style="color: #660066;">readFile</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">'go.txt'</span><span style="color: #339933;">,</span> <span style="color: #003366; font-weight: bold;">function</span><span style="color: #009900;">&#40;</span>err<span style="color: #339933;">,</span> data<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
    res.<span style="color: #660066;">attachment</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">&quot;data.csv&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
    res.<span style="color: #660066;">end</span><span style="color: #009900;">&#40;</span>data<span style="color: #339933;">,</span> <span style="color: #3366CC;">'UTF-8'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>		
  <span style="color: #009900;">&#125;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #009900;">&#125;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span></pre></div></div>

<p><cite>go.txt</cite> is just in my home directory and looks like this:</p>

<div class="wp_syntax"><div class="code"><pre class="text" style="font-family:monospace;">cruise_agent,cruise_job_duration,cruise_job_id,cruise_job_result,cruise_pipeline_counter,cruise_pipeline_label,cruise_stage_counter,cruise_timestamp_01_scheduled,cruise_timestamp_02_assigned,cruise_timestamp_03_preparing,cruise_timestamp_04_building,cruise_timestamp_05_completing,cruise_timestamp_06_completed,tests_failed_count,tests_ignored_count,tests_total_count,tests_total_duration
TheOriginalAndTheBest,275,1812,Passed,647,0.647,1,2011-08-02T14:48:33+01:00,2011-08-02T14:48:45+01:00,2011-08-02T14:48:56+01:00,2011-08-02T14:48:57+01:00,2011-08-02T14:53:11+01:00,2011-08-02T14:53:32+01:00,0,0,375,0.076
TheOriginalAndTheBest,20,1815,Cancelled,648,0.648,1,2011-08-02T15:09:32+01:00,2011-08-02T15:09:46+01:00,2011-08-02T15:09:56+01:00,2011-08-02T15:09:56+01:00,,2011-08-02T15:10:17+01:00,,,,
TheOriginalAndTheBest,268,1817,Passed,649,0.649,1,2011-08-02T15:14:20+01:00,2011-08-02T15:14:30+01:00,2011-08-02T15:14:40+01:00,2011-08-02T15:14:41+01:00,2011-08-02T15:18:49+01:00,2011-08-02T15:19:09+01:00,0,0,368,0.074
TheOriginalAndTheBest,272,1822,Passed,650,0.650,2,2011-08-02T15:30:31+01:00,2011-08-02T15:30:41+01:00,2011-08-02T15:30:51+01:00,2011-08-02T15:30:52+01:00,2011-08-02T15:35:05+01:00,2011-08-02T15:35:24+01:00,0,0,368,0.083
TheOriginalAndTheBest,271,1825,Passed,651,0.651,1,2011-08-02T15:38:33+01:00,2011-08-02T15:38:44+01:00,2011-08-02T15:38:54+01:00,2011-08-02T15:38:54+01:00,2011-08-02T15:43:06+01:00,2011-08-02T15:43:26+01:00,0,0,368,0.093</pre></div></div>

<p>I wanted to create an end point which I could call and get back a JSON representation of all the different builds.</p>

<div class="wp_syntax"><div class="code"><pre class="javascript" style="font-family:monospace;">app.<span style="color: #660066;">get</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">'/go/show'</span><span style="color: #339933;">,</span> <span style="color: #003366; font-weight: bold;">function</span><span style="color: #009900;">&#40;</span>req<span style="color: #339933;">,</span> res<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
  <span style="color: #003366; font-weight: bold;">var</span> site <span style="color: #339933;">=</span> http.<span style="color: #660066;">createClient</span><span style="color: #009900;">&#40;</span><span style="color: #CC0000;">3000</span><span style="color: #339933;">,</span> <span style="color: #3366CC;">&quot;localhost&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span> 
  <span style="color: #003366; font-weight: bold;">var</span> request <span style="color: #339933;">=</span> site.<span style="color: #660066;">request</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">&quot;GET&quot;</span><span style="color: #339933;">,</span> <span style="color: #3366CC;">&quot;/fake-go&quot;</span><span style="color: #339933;">,</span> <span style="color: #009900;">&#123;</span><span style="color: #3366CC;">'host'</span> <span style="color: #339933;">:</span> <span style="color: #3366CC;">&quot;localhost&quot;</span><span style="color: #009900;">&#125;</span><span style="color: #009900;">&#41;</span>
  request.<span style="color: #660066;">end</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
  request.<span style="color: #660066;">on</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">'response'</span><span style="color: #339933;">,</span> <span style="color: #003366; font-weight: bold;">function</span><span style="color: #009900;">&#40;</span>response<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
    <span style="color: #003366; font-weight: bold;">var</span> data <span style="color: #339933;">=</span> <span style="color: #3366CC;">&quot;&quot;</span><span style="color: #339933;">;</span>
    response.<span style="color: #660066;">setEncoding</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">'utf8'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
    response.<span style="color: #660066;">on</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">'data'</span><span style="color: #339933;">,</span> <span style="color: #003366; font-weight: bold;">function</span><span style="color: #009900;">&#40;</span>chunk<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
      data <span style="color: #339933;">+=</span> chunk<span style="color: #339933;">;</span>
    <span style="color: #009900;">&#125;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
    response.<span style="color: #660066;">on</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">'end'</span><span style="color: #339933;">,</span> <span style="color: #003366; font-weight: bold;">function</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
      <span style="color: #003366; font-weight: bold;">var</span> lines <span style="color: #339933;">=</span> data.<span style="color: #660066;">split</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">&quot;<span style="color: #000099; font-weight: bold;">\n</span>&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">,</span> buildTimes <span style="color: #339933;">=</span> <span style="color: #009900;">&#91;</span><span style="color: #009900;">&#93;</span><span style="color: #339933;">;</span>
      lines.<span style="color: #660066;">forEach</span><span style="color: #009900;">&#40;</span><span style="color: #003366; font-weight: bold;">function</span><span style="color: #009900;">&#40;</span>line<span style="color: #339933;">,</span> index<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
        <span style="color: #003366; font-weight: bold;">var</span> columns <span style="color: #339933;">=</span> line.<span style="color: #660066;">split</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">&quot;,&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
        <span style="color: #000066; font-weight: bold;">if</span><span style="color: #009900;">&#40;</span>index <span style="color: #339933;">!=</span> <span style="color: #CC0000;">0</span> <span style="color: #339933;">&amp;&amp;</span> nonEmpty<span style="color: #009900;">&#40;</span>columns<span style="color: #009900;">&#91;</span><span style="color: #CC0000;">9</span><span style="color: #009900;">&#93;</span><span style="color: #009900;">&#41;</span> <span style="color: #339933;">&amp;&amp;</span> nonEmpty<span style="color: #009900;">&#40;</span>columns<span style="color: #009900;">&#91;</span><span style="color: #CC0000;">11</span><span style="color: #009900;">&#93;</span><span style="color: #009900;">&#41;</span> <span style="color: #339933;">&amp;&amp;</span> columns<span style="color: #009900;">&#91;</span><span style="color: #CC0000;">3</span><span style="color: #009900;">&#93;</span> <span style="color: #339933;">==</span> <span style="color: #3366CC;">&quot;Passed&quot;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
          buildTimes.<span style="color: #660066;">push</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#123;</span> start <span style="color: #339933;">:</span>  columns<span style="color: #009900;">&#91;</span><span style="color: #CC0000;">9</span><span style="color: #009900;">&#93;</span><span style="color: #339933;">,</span> end <span style="color: #339933;">:</span> columns<span style="color: #009900;">&#91;</span><span style="color: #CC0000;">11</span><span style="color: #009900;">&#93;</span><span style="color: #009900;">&#125;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
        <span style="color: #009900;">&#125;</span>
      <span style="color: #009900;">&#125;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
      res.<span style="color: #660066;">contentType</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">'application/json'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
      res.<span style="color: #660066;">send</span><span style="color: #009900;">&#40;</span>JSON.<span style="color: #660066;">stringify</span><span style="color: #009900;">&#40;</span>buildTimes<span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>			
    <span style="color: #009900;">&#125;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
  <span style="color: #009900;">&#125;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>	
<span style="color: #009900;">&#125;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
<span style="color: #003366; font-weight: bold;">function</span> isEmpty<span style="color: #009900;">&#40;</span>column<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
  <span style="color: #000066; font-weight: bold;">return</span> column <span style="color: #339933;">!==</span> <span style="color: #3366CC;">&quot;&quot;</span> <span style="color: #339933;">&amp;&amp;</span> column <span style="color: #339933;">!==</span> undefined
<span style="color: #009900;">&#125;</span></pre></div></div>

<p>I should probably use <a href="http://documentcloud.github.com/underscore/">underscore.js</a> for some of that code but I didn&#8217;t want to shave that yak just yet!</p>
<p>I have a default route setup so that I can just go to localhost:3000 and see the graphs:</p>

<div class="wp_syntax"><div class="code"><pre class="javascript" style="font-family:monospace;">app.<span style="color: #660066;">get</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">'/'</span><span style="color: #339933;">,</span> <span style="color: #003366; font-weight: bold;">function</span><span style="color: #009900;">&#40;</span>req<span style="color: #339933;">,</span> res<span style="color: #009900;">&#41;</span><span style="color: #009900;">&#123;</span>
  res.<span style="color: #660066;">render</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">'index.jade'</span><span style="color: #339933;">,</span> <span style="color: #009900;">&#123;</span> title<span style="color: #339933;">:</span> <span style="color: #3366CC;">'Dashboard'</span> <span style="color: #009900;">&#125;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #009900;">&#125;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span></pre></div></div>

<p>On the client side we can then create a graph using the <a href="http://www.rgraph.net/">RGraph</a> API:</p>
<p><em>index.jade</em></p>

<div class="wp_syntax"><div class="code"><pre class="html" style="font-family:monospace;">h2(align=&quot;center&quot;) Project Dashboard
script
  function drawGoGraph(buildTimes) {		
    var go = new RGraph.Line('go', _(buildTimes).map(function(buildTime) { return (new Date(buildTime.end) - new Date(buildTime.start)) / 1000 }).filter(function(diff) { return diff &gt; 0; }));
    go.Set('chart.title', 'Build Times');		
    go.Set('chart.gutter.top', 45);
    go.Set('chart.gutter.bottom', 125);
    go.Set('chart.gutter.left', 50);
    go.Set('chart.text.angle', 90);
    go.Set('chart.shadow', true);
    go.Set('chart.linewidth', 1);
&nbsp;
    go.Draw();		
  }
&nbsp;
  $(document).ready(function() {
    $.getJSON('/go/show', function(data) {
	  drawGoGraph(data);
    });
  });
&nbsp;
div(align=&quot;center&quot;)
  canvas(id=&quot;go&quot;, width=&quot;500&quot;, height=&quot;400&quot;)
    [Please wait...]</pre></div></div>

<p>We just do some simple subtraction between the start and end build times and then filter out any results which have an end time before the start time. I&#8217;m not entirely sure why we end up with entries like that but having those in the graph totally ruins it!</p>
<p>We include all the .js files in the <cite>layout.jade</cite> file.</p>
<p><em>layout.jade</em></p>

<div class="wp_syntax"><div class="code"><pre class="html" style="font-family:monospace;">!!! 5
html(lang=&quot;en&quot;)
  head
    title Project Dashboard
    script(src='RGraph/libraries/RGraph.common.core.js')
    script(src=&quot;RGraph/libraries/RGraph.common.context.js&quot;)
    script(src=&quot;RGraph/libraries/RGraph.common.annotate.js&quot;)
    script(src=&quot;RGraph/libraries/RGraph.common.tooltips.js&quot;)
    script(src=&quot;RGraph/libraries/RGraph.common.zoom.js&quot;)
    script(src=&quot;RGraph/libraries/RGraph.common.resizing.js&quot;)
    script(src=&quot;RGraph/libraries/RGraph.line.js&quot;)
    script(src=&quot;jquery-1.6.2.min.js &quot;)
    script(src=&quot;underscore-min.js&quot;)</pre></div></div>

<p>Et voila:</p>
<div align="center">
<img src="http://www.markhneedham.com/blog/wp-content/uploads/2011/08/build-graph.jpg" alt="Build graph" title="build-graph.jpg" border="0" width="471" height="286" />
</div>
]]></content:encoded>
			<wfw:commentRss>http://www.markhneedham.com/blog/2011/08/13/node-js-building-a-graph-of-build-times-using-the-go-api/feed/</wfw:commentRss>
		<slash:comments>3</slash:comments>
		</item>
		<item>
		<title>node.js: A little application with Twitter &amp; CouchDB</title>
		<link>http://www.markhneedham.com/blog/2010/03/21/node-js-a-little-application-with-twitter-couchdb/</link>
		<comments>http://www.markhneedham.com/blog/2010/03/21/node-js-a-little-application-with-twitter-couchdb/#comments</comments>
		<pubDate>Sun, 21 Mar 2010 22:13:27 +0000</pubDate>
		<dc:creator>Mark Needham</dc:creator>
				<category><![CDATA[Javascript]]></category>
		<category><![CDATA[node.js]]></category>

		<guid isPermaLink="false">http://www.markhneedham.com/blog/?p=2281</guid>
		<description><![CDATA[I&#8217;ve been continuing to play around with node.js and I thought it would be interesting to write a little application to poll Twitter every minute and save any new Tweets into a CouchDB database. I first played around with CouchDB in May last year and initially spent a lot of time trying to work out [...]]]></description>
			<content:encoded><![CDATA[<p>I&#8217;ve been continuing to play around with <a href="http://nodejs.org/api.html">node.js</a> and I thought it would be interesting to write a little application to poll Twitter every minute and save any new Tweets into a <a href="http://wiki.apache.org/couchdb/">CouchDB</a> database.</p>
<p>I first played around with CouchDB in May last year and initially spent a lot of time trying to work out how to install it before coming across <a href="http://janl.github.com/couchdbx/">CouchDBX</a> which gives you one click installation for Mac OS X.</p>
<p>I&#8217;m using sixtus&#8217; <a href="http://github.com/sixtus/node-couch">node-couch</a> library to communicate with CouchDB and I&#8217;ve written a little bit of code that allows me to call the Twitter API.</p>
<h4>What did I learn?</h4>
<ul>
<li>I&#8217;ve been reading through Brian Guthrie&#8217;s slides from his &#8216;<a href="http://www.slideshare.net/btguthrie/advanced-ruby-idioms-so-clean-you-can-eat-off-of-them">Advanced Ruby Idioms So Clean You Can Eat Off Of Them</a>&#8216; talk from <a href="http://rubyconfindia.org/">RubyConfIndia</a> and one of the suggestions he makes is that in Ruby there are only 6 acceptable types of signatures for functions:
<ul>
<li>0 parameters</li>
<li>1 parameter</li>
<li>2 parameters </li>
<li>A hash</li>
<li>1 parameter and a hash</li>
<li>A variable number of arguments passed in as an array</li>
</ul>
<p>It seems to me that the same guidelines would be applicable in JavaScript as well except <strong>instead of a Hash we can pass in an object with properties and values to serve the same purpose</strong>. A lot of the jQuery libraries I&#8217;ve used actually do this anyway so it&#8217;s an idiom that&#8217;s in both languages.</p>
<p>I originally wrote my twitter function it so that it would take in several of the arguments individually:</p>

<div class="wp_syntax"><div class="code"><pre class="javascript" style="font-family:monospace;"><span style="color: #003366; font-weight: bold;">export</span>.<span style="color: #660066;">query</span> <span style="color: #339933;">=</span> <span style="color: #003366; font-weight: bold;">function</span><span style="color: #009900;">&#40;</span>username<span style="color: #339933;">,</span> password<span style="color: #339933;">,</span> method<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span> ... <span style="color: #009900;">&#125;</span></pre></div></div>

<p>After reading Brian&#8217;s slides I realised that this was quickly going to become a mess so I&#8217;ve changed the signature to only take in the most important parameter (&#8216;method&#8217;) on its own with the other parameters passed in an &#8216;options&#8217; object:</p>

<div class="wp_syntax"><div class="code"><pre class="javascript" style="font-family:monospace;"><span style="color: #003366; font-weight: bold;">export</span>.<span style="color: #660066;">query</span> <span style="color: #339933;">=</span> <span style="color: #003366; font-weight: bold;">function</span><span style="color: #009900;">&#40;</span>method<span style="color: #339933;">,</span> options<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span> ... <span style="color: #009900;">&#125;</span></pre></div></div>

<p>I&#8217;ve not written functions that take in parameters like this before but I really like it so far. It <strong>really helps simplify signatures</strong> while allowing you to pass in extra values if necessary.</p>
</li>
<li><strong>I find myself porting higher order functions from C#/F# into JavaScript</strong> whenever I can&#8217;t find a function to do what I want &#8211; it&#8217;s fun writing them but I&#8217;m not sure how idiomatic the code I&#8217;m writing is.
<p>For example I wanted to write a function to take query parameters out of an options object and create a query string out of them. I adapted the code from node-couch and ended up with the following:</p>

<div class="wp_syntax"><div class="code"><pre class="javascript" style="font-family:monospace;">Object.<span style="color: #660066;">prototype</span>.<span style="color: #660066;">filter</span> <span style="color: #339933;">=</span> <span style="color: #003366; font-weight: bold;">function</span><span style="color: #009900;">&#40;</span>fn<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
    <span style="color: #003366; font-weight: bold;">var</span> result <span style="color: #339933;">=</span> <span style="color: #009900;">&#123;</span><span style="color: #009900;">&#125;</span><span style="color: #339933;">;</span>
    <span style="color: #000066; font-weight: bold;">for</span> <span style="color: #009900;">&#40;</span><span style="color: #003366; font-weight: bold;">var</span> <span style="color: #000066;">name</span> <span style="color: #000066; font-weight: bold;">in</span> <span style="color: #000066; font-weight: bold;">this</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
        <span style="color: #000066; font-weight: bold;">if</span> <span style="color: #009900;">&#40;</span><span style="color: #000066; font-weight: bold;">this</span>.<span style="color: #660066;">hasOwnProperty</span><span style="color: #009900;">&#40;</span><span style="color: #000066;">name</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
            <span style="color: #000066; font-weight: bold;">if</span> <span style="color: #009900;">&#40;</span>fn.<span style="color: #660066;">call</span><span style="color: #009900;">&#40;</span><span style="color: #000066; font-weight: bold;">this</span><span style="color: #009900;">&#91;</span><span style="color: #000066;">name</span><span style="color: #009900;">&#93;</span><span style="color: #339933;">,</span> <span style="color: #000066;">name</span><span style="color: #339933;">,</span> <span style="color: #000066; font-weight: bold;">this</span><span style="color: #009900;">&#91;</span><span style="color: #000066;">name</span><span style="color: #009900;">&#93;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
                result<span style="color: #009900;">&#91;</span><span style="color: #000066;">name</span><span style="color: #009900;">&#93;</span> <span style="color: #339933;">=</span> <span style="color: #000066; font-weight: bold;">this</span><span style="color: #009900;">&#91;</span><span style="color: #000066;">name</span><span style="color: #009900;">&#93;</span><span style="color: #339933;">;</span>
            <span style="color: #009900;">&#125;</span>
        <span style="color: #009900;">&#125;</span>
    <span style="color: #009900;">&#125;</span>
    <span style="color: #000066; font-weight: bold;">return</span> result<span style="color: #339933;">;</span>
<span style="color: #009900;">&#125;</span><span style="color: #339933;">;</span>
&nbsp;
Object.<span style="color: #660066;">prototype</span>.<span style="color: #660066;">into</span> <span style="color: #339933;">=</span> <span style="color: #003366; font-weight: bold;">function</span><span style="color: #009900;">&#40;</span>theArray<span style="color: #339933;">,</span> fn<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
    <span style="color: #000066; font-weight: bold;">for</span> <span style="color: #009900;">&#40;</span><span style="color: #003366; font-weight: bold;">var</span> <span style="color: #000066;">name</span> <span style="color: #000066; font-weight: bold;">in</span> <span style="color: #000066; font-weight: bold;">this</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
        <span style="color: #000066; font-weight: bold;">if</span> <span style="color: #009900;">&#40;</span><span style="color: #000066; font-weight: bold;">this</span>.<span style="color: #660066;">hasOwnProperty</span><span style="color: #009900;">&#40;</span><span style="color: #000066;">name</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
            theArray.<span style="color: #660066;">push</span><span style="color: #009900;">&#40;</span>fn.<span style="color: #660066;">call</span><span style="color: #009900;">&#40;</span><span style="color: #000066; font-weight: bold;">this</span><span style="color: #009900;">&#91;</span><span style="color: #000066;">name</span><span style="color: #009900;">&#93;</span><span style="color: #339933;">,</span> <span style="color: #000066;">name</span><span style="color: #339933;">,</span> <span style="color: #000066; font-weight: bold;">this</span><span style="color: #009900;">&#91;</span><span style="color: #000066;">name</span><span style="color: #009900;">&#93;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
        <span style="color: #009900;">&#125;</span>
    <span style="color: #009900;">&#125;</span>
    <span style="color: #000066; font-weight: bold;">return</span> theArray<span style="color: #339933;">;</span>
<span style="color: #009900;">&#125;</span><span style="color: #339933;">;</span>
&nbsp;
<span style="color: #003366; font-weight: bold;">function</span> encodeOptions<span style="color: #009900;">&#40;</span>options<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
    <span style="color: #003366; font-weight: bold;">var</span> parameters <span style="color: #339933;">=</span> <span style="color: #009900;">&#91;</span><span style="color: #009900;">&#93;</span><span style="color: #339933;">;</span>
    <span style="color: #000066; font-weight: bold;">if</span> <span style="color: #009900;">&#40;</span><span style="color: #000066; font-weight: bold;">typeof</span><span style="color: #009900;">&#40;</span>options<span style="color: #009900;">&#41;</span> <span style="color: #339933;">===</span> <span style="color: #3366CC;">&quot;object&quot;</span> <span style="color: #339933;">&amp;&amp;</span> options <span style="color: #339933;">!==</span> <span style="color: #003366; font-weight: bold;">null</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
        parameters <span style="color: #339933;">=</span> options
                        .<span style="color: #660066;">filter</span><span style="color: #009900;">&#40;</span><span style="color: #003366; font-weight: bold;">function</span><span style="color: #009900;">&#40;</span><span style="color: #000066;">name</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
                            <span style="color: #000066; font-weight: bold;">return</span> <span style="color: #339933;">!</span><span style="color: #009900;">&#40;</span><span style="color: #000066;">name</span> <span style="color: #339933;">===</span> <span style="color: #3366CC;">&quot;username&quot;</span> <span style="color: #339933;">||</span> <span style="color: #000066;">name</span> <span style="color: #339933;">===</span> <span style="color: #3366CC;">&quot;password&quot;</span> <span style="color: #339933;">||</span> <span style="color: #000066;">name</span> <span style="color: #339933;">===</span> <span style="color: #3366CC;">&quot;callback&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><span style="color: #009900;">&#125;</span><span style="color: #009900;">&#41;</span>
                        .<span style="color: #660066;">into</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#91;</span><span style="color: #009900;">&#93;</span><span style="color: #339933;">,</span> <span style="color: #003366; font-weight: bold;">function</span><span style="color: #009900;">&#40;</span><span style="color: #000066;">name</span><span style="color: #339933;">,</span> value<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
                            <span style="color: #000066; font-weight: bold;">return</span> encodeURIComponent<span style="color: #009900;">&#40;</span><span style="color: #000066;">name</span><span style="color: #009900;">&#41;</span> <span style="color: #339933;">+</span> <span style="color: #3366CC;">&quot;=&quot;</span> <span style="color: #339933;">+</span> encodeURIComponent<span style="color: #009900;">&#40;</span>value<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span> <span style="color: #009900;">&#125;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
    <span style="color: #009900;">&#125;</span>
    <span style="color: #000066; font-weight: bold;">return</span> parameters.<span style="color: #660066;">length</span> <span style="color: #339933;">?</span> <span style="color: #009900;">&#40;</span><span style="color: #3366CC;">&quot;?&quot;</span> <span style="color: #339933;">+</span> parameters.<span style="color: #660066;">join</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">&quot;&amp;&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span> <span style="color: #339933;">:</span> <span style="color: #3366CC;">&quot;&quot;</span><span style="color: #339933;">;</span>
<span style="color: #009900;">&#125;</span></pre></div></div>

<p>I&#8217;m not sure how wise it is adding these functions to the object prototype &#8211; I haven&#8217;t had any problems so far but I guess if other libraries I&#8217;m using changed the prototype of these built in types in the same way as I am then I might get unexpected behaviour. </p>
<p>Would the typical way to defend against this be to check if a function is defined before trying to define one and throwing an exception if so? Or is adding to the prototype just a dangerous thing to do altogether?</p>
<p>Either way I&#8217;m not altogether convinced that the code with these higher order functions actually reads better than it would without them.
</li>
<li>I&#8217;m finding it quite interesting that a lot of the code I write around node.js depends on callbacks which means that if you have 3 operations that depend on each other then you end up with nested callbacks which almost reads like code written in a <a href="http://www.markhneedham.com/blog/2010/03/19/functional-c-continuation-passing-style/">continuation passing style</a>.
<p>For example I have some code which needs to do the following:</p>
<ul>
<li>Query CouchDB to get the ID of the most recently saved tweet</li>
<li>Query Twitter with that ID to get any tweets since that one</li>
<li>Save those tweets to CouchDB</li>
</ul>

<div class="wp_syntax"><div class="code"><pre class="javascript" style="font-family:monospace;"><span style="color: #003366; font-weight: bold;">var</span> server <span style="color: #339933;">=</span> http.<span style="color: #660066;">createServer</span><span style="color: #009900;">&#40;</span><span style="color: #003366; font-weight: bold;">function</span> <span style="color: #009900;">&#40;</span>req<span style="color: #339933;">,</span> res<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
    couchDB.<span style="color: #660066;">view</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">&quot;application/sort-by-id&quot;</span><span style="color: #339933;">,</span> <span style="color: #009900;">&#123;</span>
        descending <span style="color: #339933;">:</span> <span style="color: #003366; font-weight: bold;">true</span><span style="color: #339933;">,</span>
        success <span style="color: #339933;">:</span> <span style="color: #003366; font-weight: bold;">function</span><span style="color: #009900;">&#40;</span>response<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
            twitter.<span style="color: #660066;">query</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">&quot;friends_timeline&quot;</span><span style="color: #339933;">,</span> <span style="color: #009900;">&#123;</span>
                ...
                <span style="color: #660066;">since_id</span> <span style="color: #339933;">:</span> response.<span style="color: #660066;">rows</span><span style="color: #009900;">&#91;</span><span style="color: #CC0000;">0</span><span style="color: #009900;">&#93;</span>.<span style="color: #660066;">value</span>.<span style="color: #660066;">id</span><span style="color: #339933;">,</span>
                callback <span style="color: #339933;">:</span> <span style="color: #003366; font-weight: bold;">function</span><span style="color: #009900;">&#40;</span>tweets<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
                    tweets.<span style="color: #660066;">each</span><span style="color: #009900;">&#40;</span><span style="color: #003366; font-weight: bold;">function</span><span style="color: #009900;">&#40;</span>tweet<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
                        couchDB.<span style="color: #660066;">saveDoc</span><span style="color: #009900;">&#40;</span>tweet<span style="color: #339933;">,</span> <span style="color: #009900;">&#123;</span>
                            success <span style="color: #339933;">:</span> <span style="color: #003366; font-weight: bold;">function</span><span style="color: #009900;">&#40;</span>doc<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
                                sys.<span style="color: #660066;">log</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">&quot;Document &quot;</span> <span style="color: #339933;">+</span> doc._id <span style="color: #339933;">+</span> <span style="color: #3366CC;">&quot; successfully saved&quot;</span><span style="color: #009900;">&#41;</span>
                            <span style="color: #009900;">&#125;</span><span style="color: #339933;">,</span>
                            error <span style="color: #339933;">:</span> <span style="color: #003366; font-weight: bold;">function</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
                                sys.<span style="color: #660066;">log</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">&quot;Document failed to save&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
                            <span style="color: #009900;">&#125;</span>
                        <span style="color: #009900;">&#125;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
                    <span style="color: #009900;">&#125;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span> 
&nbsp;
                <span style="color: #009900;">&#125;</span>
            <span style="color: #009900;">&#125;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
        <span style="color: #009900;">&#125;</span><span style="color: #339933;">,</span>
        error <span style="color: #339933;">:</span> <span style="color: #003366; font-weight: bold;">function</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
            sys.<span style="color: #660066;">log</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">&quot;Failed to retrieve documents&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
        <span style="color: #009900;">&#125;</span>
    <span style="color: #009900;">&#125;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
    ...
<span style="color: #009900;">&#125;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span></pre></div></div>

<p>There&#8217;s a &#8216;success&#8217; callback for calling &#8216;couchDB.view&#8217; and then a &#8216;callback&#8217; callback for calling &#8216;twitter.query&#8217; and finally a &#8216;success&#8217; callback from calling &#8216;couchDB.saveDoc&#8217;. </p>
<p>To me it&#8217;s not that obvious what the code is doing at first glance &#8211; perhaps because I&#8217;m not that used to this style of programming &#8211; but I&#8217;m intrigued if there&#8217;s a way to write the code to make it more readable.
</li>
<li>I haven&#8217;t yet worked out a good way to test drive code in a node.js module. As I understand it all the functions we define except for ones added to the &#8216;exports&#8217; object are private to the module so there&#8217;s no way to test against that code directly unless you pull it out into another module.
<p>At the moment I&#8217;m just changing the code and then restarting the server and checking if it&#8217;s working or not. It&#8217;s probably not the most effective feedback cycle but it&#8217;s working reasonably well so far.
</ul>
<p>I&#8217;ve put the code that I&#8217;ve written so far as gists on github:</p>
<ul>
<li><a href="http://gist.github.com/339579">twitter-server.js</a></li>
<li><a href="http://gist.github.com/339583">twitter.js</a></li>
<li><a href="http://gist.github.com/339591">encoding.js</a></li>
</ul>
<p>That can be run with the following command from the terminal:</p>

<div class="wp_syntax"><div class="code"><pre class="text" style="font-family:monospace;">node twitter-server.js</pre></div></div>

]]></content:encoded>
			<wfw:commentRss>http://www.markhneedham.com/blog/2010/03/21/node-js-a-little-application-with-twitter-couchdb/feed/</wfw:commentRss>
		<slash:comments>5</slash:comments>
		</item>
		<item>
		<title>node.js: First thoughts</title>
		<link>http://www.markhneedham.com/blog/2010/03/15/node-js-first-thoughts/</link>
		<comments>http://www.markhneedham.com/blog/2010/03/15/node-js-first-thoughts/#comments</comments>
		<pubDate>Mon, 15 Mar 2010 00:09:47 +0000</pubDate>
		<dc:creator>Mark Needham</dc:creator>
				<category><![CDATA[Javascript]]></category>

		<guid isPermaLink="false">http://www.markhneedham.com/blog/?p=2259</guid>
		<description><![CDATA[I recently came across node.js via a blog post by Paul Gross and I&#8217;ve been playing around with it a bit over the weekend trying to hook up some code to call through to the Twitter API and then return the tweets on my friend timeline. node.js gives us event driven I/O using JavaScript running [...]]]></description>
			<content:encoded><![CDATA[<p>I recently came across <a href="http://nodejs.org/">node.js</a> via <a href="http://www.pgrs.net/2010/2/28/node-js-redis-and-resque">a blog post by Paul Gross</a> and I&#8217;ve been playing around with it a bit over the weekend trying to hook up some code to call through to the Twitter API and then return the tweets on my friend timeline.</p>
<p>node.js gives us event driven I/O using JavaScript running server side on top of <a href="http://code.google.com/p/v8/">Google&#8217;s V8 JavaScript engine</a>.</p>
<p>Simon Willison has <a href="http://www.slideshare.net/simon/evented-io-based-web-servers-explained-using-bunnies - Simon Willison's talk">part of a presentation on slideshare</a> where he describes the difference between the typical thread per request approach and the event based approach to dealing with web requests using the metaphor of bunnies. He also has <a href="http://simonwillison.net/2009/Nov/23/node/">a blog post where he describes this is more detail</a>.</p>
<p>Another resource I found useful is <a href="http://jsconfeu.blip.tv/file/2899135/">a video from jsconf.eu</a> where the creator of node.js, Ryan Dahl, explains the philosophy behind event driven I/O and gives several examples using node.js.</p>
<p>These are some of my thoughts so far:</p>
<ul>
<li>I&#8217;m not used to have <a href="http://en.wikipedia.org/wiki/Event-driven_programming">so many callbacks spread all around the code</a> and I&#8217;m still getting used to the idea that they aren&#8217;t executed until the event actually happens!
<p>I often find myself looking at a piece of code and not understanding how it can possibly work because I&#8217;m assuming that the function passed in is executed immediately when in fact it isn&#8217;t.
</li>
<li>If you make a web request the response comes back in chunks so the callback we setup to capture the response will be called multiple times with different parts of the response message.
<p>For example I have this code to call Twitter and return all my friends&#8217; status updates:</p>

<div class="wp_syntax"><div class="code"><pre class="javascript" style="font-family:monospace;"><span style="color: #003366; font-weight: bold;">var</span> sys <span style="color: #339933;">=</span> require<span style="color: #009900;">&#40;</span><span style="color: #3366CC;">&quot;sys&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">,</span>
    http <span style="color: #339933;">=</span> require<span style="color: #009900;">&#40;</span><span style="color: #3366CC;">'http'</span><span style="color: #009900;">&#41;</span>
&nbsp;
exports.<span style="color: #660066;">getTweets</span> <span style="color: #339933;">=</span> <span style="color: #003366; font-weight: bold;">function</span><span style="color: #009900;">&#40;</span>callBack<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
    <span style="color: #003366; font-weight: bold;">var</span> twitter <span style="color: #339933;">=</span> http.<span style="color: #660066;">createClient</span><span style="color: #009900;">&#40;</span><span style="color: #CC0000;">80</span><span style="color: #339933;">,</span> <span style="color: #3366CC;">&quot;www.twitter.com&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
    <span style="color: #003366; font-weight: bold;">var</span> request <span style="color: #339933;">=</span> twitter.<span style="color: #660066;">request</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">&quot;GET&quot;</span><span style="color: #339933;">,</span> <span style="color: #3366CC;">&quot;/statuses/friends_timeline.json&quot;</span><span style="color: #339933;">,</span>
                                  <span style="color: #009900;">&#123;</span><span style="color: #3366CC;">&quot;host&quot;</span><span style="color: #339933;">:</span> <span style="color: #3366CC;">&quot;www.twitter.com&quot;</span><span style="color: #339933;">,</span>
                                   <span style="color: #3366CC;">&quot;Authorization&quot;</span> <span style="color: #339933;">:</span> <span style="color: #3366CC;">&quot;Basic &quot;</span> <span style="color: #339933;">+</span> <span style="color: #3366CC;">&quot;xxx&quot;</span><span style="color: #009900;">&#125;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
    request.<span style="color: #660066;">addListener</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">'response'</span><span style="color: #339933;">,</span> <span style="color: #003366; font-weight: bold;">function</span> <span style="color: #009900;">&#40;</span>response<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
        <span style="color: #003366; font-weight: bold;">var</span> tweets <span style="color: #339933;">=</span> <span style="color: #3366CC;">&quot;&quot;</span><span style="color: #339933;">;</span>
&nbsp;
        response.<span style="color: #660066;">addListener</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">&quot;data&quot;</span><span style="color: #339933;">,</span> <span style="color: #003366; font-weight: bold;">function</span> <span style="color: #009900;">&#40;</span>chunk<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
            tweets <span style="color: #339933;">+=</span> chunk<span style="color: #339933;">;</span>
        <span style="color: #009900;">&#125;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
        response.<span style="color: #660066;">addListener</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">&quot;end&quot;</span><span style="color: #339933;">,</span> <span style="color: #003366; font-weight: bold;">function</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
            callBack.<span style="color: #660066;">call</span><span style="color: #009900;">&#40;</span><span style="color: #000066; font-weight: bold;">this</span><span style="color: #339933;">,</span> tweets<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
        <span style="color: #009900;">&#125;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
    <span style="color: #009900;">&#125;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
    request.<span style="color: #000066;">close</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #009900;">&#125;</span><span style="color: #339933;">;</span></pre></div></div>

<p>I originally thought that the listener for &#8216;data&#8217; would only be called once but it gets called 8 times sometimes so that I&#8217;ve created the &#8216;tweets&#8217; variable which allows us to wait until we have the full response before firing the callback when the &#8216;end&#8217; event is fired.</p>
<p>I&#8217;m not sure whether I&#8217;m missing the point a bit by doing this and I think I possibly need to get more used to designing functions which can deal with streams rather than expecting to have all of the data.</li>
<li>It seems like node.js would be perfect for a version of <a href="http://code.google.com/p/http-impersonator/">my colleagues Julio Maia and Fabio Lessa&#8217;s  http-impersonator</a> which is a Java application used to record and replay requests/responses made across http-based protocols.
<p>I haven&#8217;t quite worked out the best way to test the above code &#8211; ideally I want to stub out the HTTP request so that the test doesn&#8217;t have to go across the wire. Micheil Smith pointed me towards <a href="http://fakeweb.rubyforge.org/">fakeweb</a> which allows the faking of HTTP requests/responses in Ruby so I&#8217;ll probably have a go at creating something similar. </li>
</ul>
<p>So far node.js seems really cool and writing code using it is really fun. I&#8217;m still not sure exactly where it will fit in some of the architectures that I&#8217;ve worked on but the model it encourages feels really natural to work with.</p>
]]></content:encoded>
			<wfw:commentRss>http://www.markhneedham.com/blog/2010/03/15/node-js-first-thoughts/feed/</wfw:commentRss>
		<slash:comments>2</slash:comments>
		</item>
		<item>
		<title>Javascript: Function scoping</title>
		<link>http://www.markhneedham.com/blog/2010/03/10/javascript-function-scoping/</link>
		<comments>http://www.markhneedham.com/blog/2010/03/10/javascript-function-scoping/#comments</comments>
		<pubDate>Wed, 10 Mar 2010 23:06:31 +0000</pubDate>
		<dc:creator>Mark Needham</dc:creator>
				<category><![CDATA[Javascript]]></category>

		<guid isPermaLink="false">http://www.markhneedham.com/blog/?p=2241</guid>
		<description><![CDATA[My colleague John Hume wrote an interesting post about his experience with the &#8216;const&#8217; keyword in ActionScript where he describes the problems with trying to capture a loop variable in a closure and then evaluating it later on in the code. Since ActionScript and JavaScript are both dialects of ECMAscript, this is a problem in [...]]]></description>
			<content:encoded><![CDATA[<p>My colleague John Hume wrote <a href="http://elhumidor.blogspot.com/2010/03/actionscript-const-gotcha.html">an interesting post about his experience with the &#8216;const&#8217; keyword in ActionScript</a> where he describes the problems with trying to capture a loop variable in a closure and then evaluating it later on in the code.</p>
<p>Since ActionScript and JavaScript are both dialects of <a href="http://en.wikipedia.org/wiki/ECMAScript">ECMAscript</a>, this is a problem in JavaScript as well, and is due to the fact that variables in JavaScript have <a href="http://www.slideshare.net/douglascrockford/crockford-on-javascript-act-iii-function-the-ultimate">function scope rather than block scope</a> which is the case in many other languages.</p>
<p>This problem would tend to reveal itself in code where we try to capture a loop variable in an anonymous function and use it later on, like so:</p>

<div class="wp_syntax"><div class="code"><pre class="javascript" style="font-family:monospace;"><span style="color: #003366; font-weight: bold;">function</span> getValues<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
    <span style="color: #003366; font-weight: bold;">var</span> x <span style="color: #339933;">=</span> <span style="color: #003366; font-weight: bold;">new</span> Array<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
    <span style="color: #000066; font-weight: bold;">for</span><span style="color: #009900;">&#40;</span><span style="color: #003366; font-weight: bold;">var</span> i<span style="color: #339933;">=</span><span style="color: #CC0000;">0</span><span style="color: #339933;">;</span> i <span style="color: #339933;">&lt;</span> <span style="color: #CC0000;">10</span><span style="color: #339933;">;</span> i<span style="color: #339933;">++</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
       x<span style="color: #009900;">&#91;</span>i<span style="color: #009900;">&#93;</span> <span style="color: #339933;">=</span> <span style="color: #003366; font-weight: bold;">function</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span> <span style="color: #000066; font-weight: bold;">return</span> i<span style="color: #339933;">;</span> <span style="color: #009900;">&#125;</span>
    <span style="color: #009900;">&#125;</span>
    <span style="color: #000066; font-weight: bold;">return</span> x<span style="color: #339933;">;</span>
<span style="color: #009900;">&#125;</span><span style="color: #339933;">;</span>
&nbsp;
<span style="color: #003366; font-weight: bold;">var</span> values <span style="color: #339933;">=</span> getValues<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #000066; font-weight: bold;">for</span><span style="color: #009900;">&#40;</span><span style="color: #003366; font-weight: bold;">var</span> j<span style="color: #339933;">=</span><span style="color: #CC0000;">0</span><span style="color: #339933;">;</span> j <span style="color: #339933;">&lt;</span> values.<span style="color: #660066;">length</span><span style="color: #339933;">;</span> j<span style="color: #339933;">++</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
    console.<span style="color: #660066;">log</span><span style="color: #009900;">&#40;</span>values<span style="color: #009900;">&#91;</span>j<span style="color: #009900;">&#93;</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #009900;">&#125;</span></pre></div></div>

<p>We might expect that to print the sequence of numbers 0-9 on the screen but what we actually get is &#8217;10&#8242; printed 10 times.</p>
<p>There are a couple of things that I initially found strange about this:</p>
<ol>
<li>Why doesn&#8217;t it print out the numbers 0-9?</li>
<li>Given that it doesn&#8217;t do that why does it print out &#8217;10&#8242; 10 times instead of &#8217;9&#8242; 10 times?</li>
</ol>
<p>The answer to the first question is that &#8216;i&#8217; gets assigned a new value on each iteration of the loop and we don&#8217;t evaluate &#8216;i&#8217; until we evaluate the anonymous function on line 11.</p>
<p>The value when we do evaluate it would be the last value that it was set to by the loop which in this case that would be &#8217;10&#8242; because that&#8217;s the value that &#8216;i&#8217; has to be <a href="http://twitter.com/jason_diamond/statuses/10283944438">in order for</a> <a href="http://twitter.com/drunkcod/statuses/10283979588">the loop to terminate</a>.</p>
<p>This is <a href="http://twitter.com/davcamer/statuses/10290979811">actually a problem in C# as well</a> &#8211; the following code will output &#8217;10&#8242; 10 times as well:</p>

<div class="wp_syntax"><div class="code"><pre class="csharp" style="font-family:monospace;"><span style="color: #008000;">&#91;</span>Test<span style="color: #008000;">&#93;</span>
<span style="color: #0600FF; font-weight: bold;">public</span> <span style="color: #6666cc; font-weight: bold;">void</span> ClosureOnTheSameValue<span style="color: #008000;">&#40;</span><span style="color: #008000;">&#41;</span>
<span style="color: #008000;">&#123;</span>
    var values <span style="color: #008000;">=</span> <span style="color: #008000;">new</span> List<span style="color: #008000;">&lt;</span>Func<span style="color: #008000;">&lt;</span><span style="color: #6666cc; font-weight: bold;">int</span><span style="color: #008000;">&gt;&gt;</span><span style="color: #008000;">&#40;</span><span style="color: #008000;">&#41;</span><span style="color: #008000;">;</span>
    <span style="color: #0600FF; font-weight: bold;">for</span><span style="color: #008000;">&#40;</span><span style="color: #6666cc; font-weight: bold;">int</span> i<span style="color: #008000;">=</span><span style="color: #FF0000;">0</span><span style="color: #008000;">;</span> i <span style="color: #008000;">&lt;</span> <span style="color: #FF0000;">10</span><span style="color: #008000;">;</span> i<span style="color: #008000;">++</span><span style="color: #008000;">&#41;</span>
    <span style="color: #008000;">&#123;</span>
        values<span style="color: #008000;">.</span><span style="color: #0000FF;">Add</span><span style="color: #008000;">&#40;</span><span style="color: #008000;">&#40;</span><span style="color: #008000;">&#41;</span> <span style="color: #008000;">=&gt;</span> i<span style="color: #008000;">&#41;</span><span style="color: #008000;">;</span>
    <span style="color: #008000;">&#125;</span>
&nbsp;
    <span style="color: #0600FF; font-weight: bold;">foreach</span> <span style="color: #008000;">&#40;</span>var value <span style="color: #0600FF; font-weight: bold;">in</span> values<span style="color: #008000;">&#41;</span>
    <span style="color: #008000;">&#123;</span>
        Console<span style="color: #008000;">.</span><span style="color: #0000FF;">WriteLine</span><span style="color: #008000;">&#40;</span>value<span style="color: #008000;">&#40;</span><span style="color: #008000;">&#41;</span><span style="color: #008000;">&#41;</span><span style="color: #008000;">;</span>
    <span style="color: #008000;">&#125;</span>
<span style="color: #008000;">&#125;</span></pre></div></div>

<p>Again we capture &#8216;i&#8217; inside a closure and since we only evaluate that value when it&#8217;s actually used it will always refer to the last value that &#8216;i&#8217; was set to which in this case means that it will always output a value of 10.</p>
<p>To fix this in C# we could just create a temporary variable &#8211; something which Resharper will actually suggest to us:</p>

<div class="wp_syntax"><div class="code"><pre class="csharp" style="font-family:monospace;"><span style="color: #008000;">&#91;</span>Test<span style="color: #008000;">&#93;</span>
<span style="color: #0600FF; font-weight: bold;">public</span> <span style="color: #6666cc; font-weight: bold;">void</span> ClosureOnDifferentValue<span style="color: #008000;">&#40;</span><span style="color: #008000;">&#41;</span>
<span style="color: #008000;">&#123;</span>
    var values <span style="color: #008000;">=</span> <span style="color: #008000;">new</span> List<span style="color: #008000;">&lt;</span>Func<span style="color: #008000;">&lt;</span><span style="color: #6666cc; font-weight: bold;">int</span><span style="color: #008000;">&gt;&gt;</span><span style="color: #008000;">&#40;</span><span style="color: #008000;">&#41;</span><span style="color: #008000;">;</span>
    <span style="color: #0600FF; font-weight: bold;">for</span><span style="color: #008000;">&#40;</span><span style="color: #6666cc; font-weight: bold;">int</span> i<span style="color: #008000;">=</span><span style="color: #FF0000;">0</span><span style="color: #008000;">;</span> i <span style="color: #008000;">&lt;</span> <span style="color: #FF0000;">10</span><span style="color: #008000;">;</span> i<span style="color: #008000;">++</span><span style="color: #008000;">&#41;</span>
    <span style="color: #008000;">&#123;</span>
        var idash <span style="color: #008000;">=</span> i<span style="color: #008000;">;</span>
        values<span style="color: #008000;">.</span><span style="color: #0000FF;">Add</span><span style="color: #008000;">&#40;</span><span style="color: #008000;">&#40;</span><span style="color: #008000;">&#41;</span> <span style="color: #008000;">=&gt;</span> idash<span style="color: #008000;">&#41;</span><span style="color: #008000;">;</span>
    <span style="color: #008000;">&#125;</span>
&nbsp;
    <span style="color: #0600FF; font-weight: bold;">foreach</span> <span style="color: #008000;">&#40;</span>var value <span style="color: #0600FF; font-weight: bold;">in</span> values<span style="color: #008000;">&#41;</span>
    <span style="color: #008000;">&#123;</span>
        Console<span style="color: #008000;">.</span><span style="color: #0000FF;">WriteLine</span><span style="color: #008000;">&#40;</span>value<span style="color: #008000;">&#40;</span><span style="color: #008000;">&#41;</span><span style="color: #008000;">&#41;</span><span style="color: #008000;">;</span>
    <span style="color: #008000;">&#125;</span>
<span style="color: #008000;">&#125;</span></pre></div></div>

<p>This works in C# because variables have block scope which means that we have a new version of &#8216;idash&#8217; for each of the functions that we add to the &#8216;values&#8217; collection.</p>
<p>Sadly the same trick doesn&#8217;t work in JavaScript because variables have function scope in Javascript:</p>

<div class="wp_syntax"><div class="code"><pre class="javascript" style="font-family:monospace;"><span style="color: #003366; font-weight: bold;">function</span> getValues<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
    <span style="color: #003366; font-weight: bold;">var</span> x <span style="color: #339933;">=</span> <span style="color: #003366; font-weight: bold;">new</span> Array<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
    <span style="color: #000066; font-weight: bold;">for</span><span style="color: #009900;">&#40;</span><span style="color: #003366; font-weight: bold;">var</span> i<span style="color: #339933;">=</span><span style="color: #CC0000;">0</span><span style="color: #339933;">;</span> i <span style="color: #339933;">&lt;</span> <span style="color: #CC0000;">10</span><span style="color: #339933;">;</span> i<span style="color: #339933;">++</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
       <span style="color: #003366; font-weight: bold;">var</span> idash <span style="color: #339933;">=</span> i<span style="color: #339933;">;</span>
       x<span style="color: #009900;">&#91;</span>i<span style="color: #009900;">&#93;</span> <span style="color: #339933;">=</span> <span style="color: #003366; font-weight: bold;">function</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span> <span style="color: #000066; font-weight: bold;">return</span> idash<span style="color: #339933;">;</span> <span style="color: #009900;">&#125;</span>
    <span style="color: #009900;">&#125;</span>
    <span style="color: #000066; font-weight: bold;">return</span> x<span style="color: #339933;">;</span>
<span style="color: #009900;">&#125;</span><span style="color: #339933;">;</span>
&nbsp;
<span style="color: #003366; font-weight: bold;">var</span> values <span style="color: #339933;">=</span> getValues<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #000066; font-weight: bold;">for</span><span style="color: #009900;">&#40;</span><span style="color: #003366; font-weight: bold;">var</span> j<span style="color: #339933;">=</span><span style="color: #CC0000;">0</span><span style="color: #339933;">;</span> j <span style="color: #339933;">&lt;</span> values.<span style="color: #660066;">length</span><span style="color: #339933;">;</span> j<span style="color: #339933;">++</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
    console.<span style="color: #660066;">log</span><span style="color: #009900;">&#40;</span>values<span style="color: #009900;">&#91;</span>j<span style="color: #009900;">&#93;</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #009900;">&#125;</span></pre></div></div>

<p>The &#8216;idash&#8217; temporary variable that we created to try and solve the problem gets assigned a new value in each iteration of the loop because that variable is only declared once for the whole function. </p>
<p>The code above could be written like this to make that clearer:</p>

<div class="wp_syntax"><div class="code"><pre class="javascript" style="font-family:monospace;"><span style="color: #003366; font-weight: bold;">function</span> getValues<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
    <span style="color: #003366; font-weight: bold;">var</span> x <span style="color: #339933;">=</span> <span style="color: #003366; font-weight: bold;">new</span> Array<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
    <span style="color: #003366; font-weight: bold;">var</span> idash<span style="color: #339933;">;</span>
&nbsp;
    <span style="color: #000066; font-weight: bold;">for</span><span style="color: #009900;">&#40;</span><span style="color: #003366; font-weight: bold;">var</span> i<span style="color: #339933;">=</span><span style="color: #CC0000;">0</span><span style="color: #339933;">;</span> i <span style="color: #339933;">&lt;</span> <span style="color: #CC0000;">10</span><span style="color: #339933;">;</span> i<span style="color: #339933;">++</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
       idash <span style="color: #339933;">=</span> i<span style="color: #339933;">;</span>
       x<span style="color: #009900;">&#91;</span>i<span style="color: #009900;">&#93;</span> <span style="color: #339933;">=</span> <span style="color: #003366; font-weight: bold;">function</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span> <span style="color: #000066; font-weight: bold;">return</span> idash<span style="color: #339933;">;</span> <span style="color: #009900;">&#125;</span>
    <span style="color: #009900;">&#125;</span>
    <span style="color: #000066; font-weight: bold;">return</span> x<span style="color: #339933;">;</span>
<span style="color: #009900;">&#125;</span><span style="color: #339933;">;</span>
&nbsp;
<span style="color: #003366; font-weight: bold;">var</span> values <span style="color: #339933;">=</span> getValues<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #000066; font-weight: bold;">for</span><span style="color: #009900;">&#40;</span><span style="color: #003366; font-weight: bold;">var</span> j<span style="color: #339933;">=</span><span style="color: #CC0000;">0</span><span style="color: #339933;">;</span> j <span style="color: #339933;">&lt;</span> values.<span style="color: #660066;">length</span><span style="color: #339933;">;</span> j<span style="color: #339933;">++</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
    console.<span style="color: #660066;">log</span><span style="color: #009900;">&#40;</span>values<span style="color: #009900;">&#91;</span>j<span style="color: #009900;">&#93;</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #009900;">&#125;</span></pre></div></div>

<p>As John points out:</p>
<blockquote><p>
Here&#8217;s something I either never knew or at some point forgot about JavaScript: variables are lexically scoped, but only function bodies introduce new lexical scopes.
</p></blockquote>
<p>In this case we actually end up printing &#8217;9&#8242; 10 times because that&#8217;s the maximum value that gets assigned to &#8216;idash&#8217;.</p>
<p>One solution is to create a temporary variable inside an anonymous function that we execute immediately, like this:</p>

<div class="wp_syntax"><div class="code"><pre class="javascript" style="font-family:monospace;"><span style="color: #003366; font-weight: bold;">function</span> getValues<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
    <span style="color: #003366; font-weight: bold;">var</span> x <span style="color: #339933;">=</span> <span style="color: #003366; font-weight: bold;">new</span> Array<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
    <span style="color: #000066; font-weight: bold;">for</span><span style="color: #009900;">&#40;</span><span style="color: #003366; font-weight: bold;">var</span> i<span style="color: #339933;">=</span><span style="color: #CC0000;">0</span><span style="color: #339933;">;</span> i <span style="color: #339933;">&lt;</span> <span style="color: #CC0000;">10</span><span style="color: #339933;">;</span> i<span style="color: #339933;">++</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
        <span style="color: #009900;">&#40;</span><span style="color: #003366; font-weight: bold;">function</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
            <span style="color: #003366; font-weight: bold;">var</span> idash <span style="color: #339933;">=</span> i<span style="color: #339933;">;</span>
            x<span style="color: #009900;">&#91;</span>i<span style="color: #009900;">&#93;</span> <span style="color: #339933;">=</span> <span style="color: #003366; font-weight: bold;">function</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span> <span style="color: #000066; font-weight: bold;">return</span> idash<span style="color: #339933;">;</span> <span style="color: #009900;">&#125;</span> <span style="color: #009900;">&#125;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
    <span style="color: #009900;">&#125;</span>
    <span style="color: #000066; font-weight: bold;">return</span> x<span style="color: #339933;">;</span>
<span style="color: #009900;">&#125;</span><span style="color: #339933;">;</span>
&nbsp;
<span style="color: #003366; font-weight: bold;">var</span> values <span style="color: #339933;">=</span> getValues<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #000066; font-weight: bold;">for</span><span style="color: #009900;">&#40;</span><span style="color: #003366; font-weight: bold;">var</span> j<span style="color: #339933;">=</span><span style="color: #CC0000;">0</span><span style="color: #339933;">;</span> j <span style="color: #339933;">&lt;</span> values.<span style="color: #660066;">length</span><span style="color: #339933;">;</span> j<span style="color: #339933;">++</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
    console.<span style="color: #660066;">log</span><span style="color: #009900;">&#40;</span>values<span style="color: #009900;">&#91;</span>j<span style="color: #009900;">&#93;</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #009900;">&#125;</span></pre></div></div>

<p>Now &#8216;idash&#8217; is scoped inside the anonymous function and we therefore end up with a new value each time like we want.</p>
<p><a href="http://twitter.com/raphscallion/statuses/10288673700">Raph</a> pointed out that we could achieve the same thing in a simpler way with the following code:</p>

<div class="wp_syntax"><div class="code"><pre class="javascript" style="font-family:monospace;"><span style="color: #003366; font-weight: bold;">function</span> getValues<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
    <span style="color: #003366; font-weight: bold;">var</span> x <span style="color: #339933;">=</span> <span style="color: #003366; font-weight: bold;">new</span> Array<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
    <span style="color: #000066; font-weight: bold;">for</span><span style="color: #009900;">&#40;</span><span style="color: #003366; font-weight: bold;">var</span> i<span style="color: #339933;">=</span><span style="color: #CC0000;">0</span><span style="color: #339933;">;</span> i <span style="color: #339933;">&lt;</span> <span style="color: #CC0000;">10</span><span style="color: #339933;">;</span> i<span style="color: #339933;">++</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#40;</span><span style="color: #003366; font-weight: bold;">function</span><span style="color: #009900;">&#40;</span>i<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
        x<span style="color: #009900;">&#91;</span>i<span style="color: #009900;">&#93;</span> <span style="color: #339933;">=</span> <span style="color: #003366; font-weight: bold;">function</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span> <span style="color: #000066; font-weight: bold;">return</span> i<span style="color: #339933;">;</span> <span style="color: #009900;">&#125;</span><span style="color: #339933;">;</span>
    <span style="color: #009900;">&#125;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#40;</span>i<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
    <span style="color: #000066; font-weight: bold;">return</span> x<span style="color: #339933;">;</span>
<span style="color: #009900;">&#125;</span><span style="color: #339933;">;</span>
&nbsp;
<span style="color: #003366; font-weight: bold;">var</span> values <span style="color: #339933;">=</span> getValues<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #000066; font-weight: bold;">for</span><span style="color: #009900;">&#40;</span><span style="color: #003366; font-weight: bold;">var</span> j<span style="color: #339933;">=</span><span style="color: #CC0000;">0</span><span style="color: #339933;">;</span> j <span style="color: #339933;">&lt;</span> values.<span style="color: #660066;">length</span><span style="color: #339933;">;</span> j<span style="color: #339933;">++</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
    console.<span style="color: #660066;">log</span><span style="color: #009900;">&#40;</span>values<span style="color: #009900;">&#91;</span>j<span style="color: #009900;">&#93;</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #009900;">&#125;</span></pre></div></div>

<p>Here we define a for loop with just a single statement so we can lose the &#8216;{}&#8217; and just call an anonymous function passing in &#8216;i&#8217;.</p>
<p>Of course this example is truly contrived but I wanted to pick something simple enough that I could try and follow exactly how it worked.</p>
<p>I&#8217;m not entirely sure of the terminology around closures and scoping so if I&#8217;ve described anything incorrectly then please correct me!</p>
]]></content:encoded>
			<wfw:commentRss>http://www.markhneedham.com/blog/2010/03/10/javascript-function-scoping/feed/</wfw:commentRss>
		<slash:comments>5</slash:comments>
		</item>
		<item>
		<title>Javascript: The &#8216;new&#8217; keyword</title>
		<link>http://www.markhneedham.com/blog/2010/03/06/javascript-the-new-keyword/</link>
		<comments>http://www.markhneedham.com/blog/2010/03/06/javascript-the-new-keyword/#comments</comments>
		<pubDate>Sat, 06 Mar 2010 15:16:02 +0000</pubDate>
		<dc:creator>Mark Needham</dc:creator>
				<category><![CDATA[Javascript]]></category>

		<guid isPermaLink="false">http://www.markhneedham.com/blog/?p=2226</guid>
		<description><![CDATA[I came across an interesting post by John Resig where he describes a &#8216;makeClass&#8217; function that he uses in his code to create functions which can instantiate objects regardless of whether the user calls that function with or without the new keyword. The main reason that the new keyword seems to be considered harmful is [...]]]></description>
			<content:encoded><![CDATA[<p>I came across an <a href="http://ejohn.org/blog/simple-class-instantiation/">interesting post by John Resig where he describes a &#8216;makeClass&#8217; function</a> that he uses in his code to create functions which can instantiate objects regardless of whether the user calls that function with or without the new keyword.</p>
<p>The main reason that the new keyword seems to be considered harmful is because we might make assumptions in our function that it will be called with the new keyword which changes the meaning of &#8216;this&#8217; inside that function.</p>
<p>For example in my <a href="http://gist.github.com/317106">Bowling Game example</a> I assume that the &#8216;BowlingGame&#8217; function will be called with the new keyword.</p>
<p>I wanted to see if I could refactor that code to use the <a href="http://yuiblog.com/blog/2007/06/12/module-pattern/">module pattern</a> instead so as a first step I changed the instantiation of the &#8216;bowlingGame&#8217; variable in the <a href="http://gist.github.com/317533">tests</a> to not call the function with &#8216;new&#8217; to see if it would make any noticeable difference:</p>

<div class="wp_syntax"><div class="code"><pre class="javascript" style="font-family:monospace;">Screw.<span style="color: #660066;">Unit</span><span style="color: #009900;">&#40;</span><span style="color: #003366; font-weight: bold;">function</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
	describe<span style="color: #009900;">&#40;</span><span style="color: #3366CC;">&quot;bowling game scorecard&quot;</span><span style="color: #339933;">,</span> <span style="color: #003366; font-weight: bold;">function</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
		<span style="color: #003366; font-weight: bold;">var</span> bowlingGame<span style="color: #339933;">;</span>
&nbsp;
		before<span style="color: #009900;">&#40;</span><span style="color: #003366; font-weight: bold;">function</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
	    	     bowlingGame <span style="color: #339933;">=</span> BowlingGame<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
	  	<span style="color: #009900;">&#125;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span></pre></div></div>

<p>There is no noticeable difference in the way any of the tests work but in fact <a href="http://stackoverflow.com/questions/383402/is-javascript-s-new-keyword-considered-harmful">all of the functions I defined have been added to the global object</a> (in this case window) instead of onto &#8216;BowlingGame&#8217;.</p>
<p>I changed one of the tests to check that this was the case&#8230;</p>

<div class="wp_syntax"><div class="code"><pre class="javascript" style="font-family:monospace;">...
			<span style="color: #660066;">it</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">&quot;should score a single throw&quot;</span><span style="color: #339933;">,</span> <span style="color: #003366; font-weight: bold;">function</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
				console.<span style="color: #660066;">log</span><span style="color: #009900;">&#40;</span>window.<span style="color: #660066;">roll</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
				bowlingGame.<span style="color: #660066;">roll</span><span style="color: #009900;">&#40;</span><span style="color: #CC0000;">5</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
				<span style="color: #009900;">&#40;</span><span style="color: #CC0000;">19</span><span style="color: #009900;">&#41;</span>.<span style="color: #660066;">times</span><span style="color: #009900;">&#40;</span><span style="color: #003366; font-weight: bold;">function</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span> gutterBall<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span> <span style="color: #009900;">&#125;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
				expect<span style="color: #009900;">&#40;</span>bowlingGame.<span style="color: #660066;">score</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span>.<span style="color: #660066;">to</span><span style="color: #009900;">&#40;</span>equal<span style="color: #339933;">,</span> <span style="color: #CC0000;">5</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
			<span style="color: #009900;">&#125;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
...</pre></div></div>

<p>&#8230;which logs &#8216;undefined&#8217; to Firebug if the new keyword is used to instantiate &#8216;bowlingGame&#8217; and &#8216;function()&#8217; if it wasn&#8217;t.</p>
<p>The danger here is that you could change the meaning of the &#8216;roll&#8217; function outside of the &#8216;BowlingGame&#8217; if you wanted to.</p>
<p>To give a contrived example perhaps we could change &#8216;roll&#8217; so that it actually called the original function twice instead of once:</p>

<div class="wp_syntax"><div class="code"><pre class="javascript" style="font-family:monospace;">...
			<span style="color: #660066;">it</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">&quot;should score a single throw&quot;</span><span style="color: #339933;">,</span> <span style="color: #003366; font-weight: bold;">function</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
				<span style="color: #003366; font-weight: bold;">var</span> originalRoll <span style="color: #339933;">=</span> window.<span style="color: #660066;">roll</span><span style="color: #339933;">;</span>
				window.<span style="color: #660066;">roll</span> <span style="color: #339933;">=</span> <span style="color: #003366; font-weight: bold;">function</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
					originalRoll.<span style="color: #660066;">apply</span><span style="color: #009900;">&#40;</span><span style="color: #000066; font-weight: bold;">this</span><span style="color: #339933;">,</span> arguments<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
					originalRoll.<span style="color: #660066;">apply</span><span style="color: #009900;">&#40;</span><span style="color: #000066; font-weight: bold;">this</span><span style="color: #339933;">,</span> arguments<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
					console.<span style="color: #660066;">log</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">&quot;roll isn't what you'd expect anymore&quot;</span><span style="color: #009900;">&#41;</span>				
				<span style="color: #009900;">&#125;</span><span style="color: #339933;">;</span>
&nbsp;
				bowlingGame.<span style="color: #660066;">roll</span><span style="color: #009900;">&#40;</span><span style="color: #CC0000;">5</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
				<span style="color: #009900;">&#40;</span><span style="color: #CC0000;">19</span><span style="color: #009900;">&#41;</span>.<span style="color: #660066;">times</span><span style="color: #009900;">&#40;</span><span style="color: #003366; font-weight: bold;">function</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span> gutterBall<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span> <span style="color: #009900;">&#125;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
				expect<span style="color: #009900;">&#40;</span>bowlingGame.<span style="color: #660066;">score</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span>.<span style="color: #660066;">to</span><span style="color: #009900;">&#40;</span>equal<span style="color: #339933;">,</span> <span style="color: #CC0000;">5</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
			<span style="color: #009900;">&#125;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>	
...</pre></div></div>

<p>In this case you would probably never do that because it&#8217;s just a small bit of code but you wouldn&#8217;t want to add random functions to the global object in any reasonably sized javascript application.</p>
<p><a href="http://stackoverflow.com/questions/383402/is-javascript-s-new-keyword-considered-harmful#383503">Shog9 points to a bit of code which allows us to stop users from calling constructor functions without the new keyword</a>:</p>

<div class="wp_syntax"><div class="code"><pre class="javascript" style="font-family:monospace;">BowlingGame  <span style="color: #339933;">=</span> <span style="color: #003366; font-weight: bold;">function</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
	<span style="color: #000066; font-weight: bold;">if</span> <span style="color: #009900;">&#40;</span> <span style="color: #339933;">!</span><span style="color: #009900;">&#40;</span><span style="color: #000066; font-weight: bold;">this</span> <span style="color: #000066; font-weight: bold;">instanceof</span> arguments.<span style="color: #660066;">callee</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#41;</span> 
	   <span style="color: #000066; font-weight: bold;">throw</span> Error<span style="color: #009900;">&#40;</span><span style="color: #3366CC;">&quot;Constructor called as a function&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
...</pre></div></div>

<p>When &#8216;BowlingGame&#8217; is called without the new keyword then &#8216;this&#8217; will refer to &#8216;window&#8217; which means that it won&#8217;t be an instance of &#8216;arguments.callee&#8217; which in this case is the &#8216;BowlingGame&#8217; function.</p>
]]></content:encoded>
			<wfw:commentRss>http://www.markhneedham.com/blog/2010/03/06/javascript-the-new-keyword/feed/</wfw:commentRss>
		<slash:comments>1</slash:comments>
		</item>
		<item>
		<title>Javascript: Confusing &#8216;call&#8217; and &#8216;apply&#8217;</title>
		<link>http://www.markhneedham.com/blog/2010/02/28/javascript-confusing-call-and-apply/</link>
		<comments>http://www.markhneedham.com/blog/2010/02/28/javascript-confusing-call-and-apply/#comments</comments>
		<pubDate>Sun, 28 Feb 2010 01:45:49 +0000</pubDate>
		<dc:creator>Mark Needham</dc:creator>
				<category><![CDATA[Javascript]]></category>

		<guid isPermaLink="false">http://www.markhneedham.com/blog/?p=2211</guid>
		<description><![CDATA[I wrote a couple of weeks ago about using the &#8216;call&#8217; and &#8216;apply&#8217; functions in Javascript when passing functions around and while working on our IE6 specific code I realised that I&#8217;d got them mixed up. We were writing some code to override one of our functions so that we could call the original function [...]]]></description>
			<content:encoded><![CDATA[<p>I wrote a couple of weeks ago about <a href="http://www.markhneedham.com/blog/2010/02/12/javascript-passing-functions-around-with-call-and-apply/">using the &#8216;call&#8217; and &#8216;apply&#8217; functions in Javascript when passing functions around</a> and while working on <a href="http://www.markhneedham.com/blog/2010/02/28/javascript-isolating-browser-specific-code/">our IE6 specific code</a> I realised that I&#8217;d got them mixed up. </p>
<p>We were writing some code to override one of our functions so that we could call the original function and then do something else after that.</p>
<p>The code was roughly like this:</p>

<div class="wp_syntax"><div class="code"><pre class="javascript" style="font-family:monospace;">Foo <span style="color: #339933;">=</span> <span style="color: #009900;">&#123;</span>
    bar <span style="color: #339933;">:</span> <span style="color: #003366; font-weight: bold;">function</span><span style="color: #009900;">&#40;</span>duck<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
      console.<span style="color: #660066;">log</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">&quot;bar &quot;</span> <span style="color: #339933;">+</span> duck.<span style="color: #660066;">quack</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>  
    <span style="color: #009900;">&#125;</span>
<span style="color: #009900;">&#125;</span><span style="color: #339933;">;</span></pre></div></div>

<p>The code that I originally wrote to capture the original function, call it and then do the additional behaviour was like this:</p>

<div class="wp_syntax"><div class="code"><pre class="javascript" style="font-family:monospace;"><span style="color: #009900;">&#40;</span><span style="color: #003366; font-weight: bold;">function</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
  <span style="color: #003366; font-weight: bold;">var</span> originalBar <span style="color: #339933;">=</span> Foo.<span style="color: #660066;">bar</span><span style="color: #339933;">;</span>
&nbsp;
   Foo.<span style="color: #660066;">bar</span> <span style="color: #339933;">=</span> <span style="color: #003366; font-weight: bold;">function</span><span style="color: #009900;">&#40;</span>duck<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
        originalBar.<span style="color: #660066;">call</span><span style="color: #009900;">&#40;</span><span style="color: #000066; font-weight: bold;">this</span><span style="color: #339933;">,</span> arguments<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
        console.<span style="color: #660066;">log</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">&quot;new bar&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
   <span style="color: #009900;">&#125;</span><span style="color: #339933;">;</span>
<span style="color: #009900;">&#125;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span></pre></div></div>

<p>When we call the function:</p>

<div class="wp_syntax"><div class="code"><pre class="javascript" style="font-family:monospace;">Foo.<span style="color: #660066;">bar</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#123;</span> quack <span style="color: #339933;">:</span> <span style="color: #003366; font-weight: bold;">function</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span> <span style="color: #000066; font-weight: bold;">return</span> <span style="color: #3366CC;">&quot;quacking&quot;</span> <span style="color: #009900;">&#125;</span> <span style="color: #009900;">&#125;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span></pre></div></div>

<p>We get the following error:</p>

<div class="wp_syntax"><div class="code"><pre class="text" style="font-family:monospace;">TypeError: duck.quack is not a function</pre></div></div>

<p>&#8216;arguments&#8217; is a local variable in any Javascript function which contains all the arguments passed to the function stored in an array type structure.</p>
<p>However, I had forgotten that when using the &#8216;call&#8217; function we need to pass the full list of parameters individually rather than as an array so in this case we would need to pass &#8216;duck&#8217; in specifically:</p>

<div class="wp_syntax"><div class="code"><pre class="javascript" style="font-family:monospace;"><span style="color: #009900;">&#40;</span><span style="color: #003366; font-weight: bold;">function</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
  <span style="color: #003366; font-weight: bold;">var</span> originalBar <span style="color: #339933;">=</span> Foo.<span style="color: #660066;">bar</span><span style="color: #339933;">;</span>
&nbsp;
   Foo.<span style="color: #660066;">bar</span> <span style="color: #339933;">=</span> <span style="color: #003366; font-weight: bold;">function</span><span style="color: #009900;">&#40;</span>duck<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
        originalBar.<span style="color: #660066;">call</span><span style="color: #009900;">&#40;</span><span style="color: #000066; font-weight: bold;">this</span><span style="color: #339933;">,</span> duck<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
        console.<span style="color: #660066;">log</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">&quot;new bar&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
   <span style="color: #009900;">&#125;</span><span style="color: #339933;">;</span>
<span style="color: #009900;">&#125;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span></pre></div></div>

<p>Now when we run the function we get the expected behaviour:</p>

<div class="wp_syntax"><div class="code"><pre class="javascript" style="font-family:monospace;">Foo.<span style="color: #660066;">bar</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#123;</span> quack <span style="color: #339933;">:</span> <span style="color: #003366; font-weight: bold;">function</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span> <span style="color: #000066; font-weight: bold;">return</span> <span style="color: #3366CC;">&quot;quacking&quot;</span> <span style="color: #009900;">&#125;</span> <span style="color: #009900;">&#125;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span></pre></div></div>


<div class="wp_syntax"><div class="code"><pre class="text" style="font-family:monospace;">bar quacking
new bar</pre></div></div>

<p>This is where apply comes in handy because apply allows us to pass in &#8216;arguments&#8217; as the second parameter and it will send all the arguments of the function that we&#8217;re inside to the function that we&#8217;re calling which is exactly what we want in this case.</p>
<p>Using &#8216;apply&#8217; we would end up with the following code:</p>

<div class="wp_syntax"><div class="code"><pre class="javascript" style="font-family:monospace;"><span style="color: #009900;">&#40;</span><span style="color: #003366; font-weight: bold;">function</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
  <span style="color: #003366; font-weight: bold;">var</span> originalBar <span style="color: #339933;">=</span> Foo.<span style="color: #660066;">bar</span><span style="color: #339933;">;</span>
&nbsp;
   Foo.<span style="color: #660066;">bar</span> <span style="color: #339933;">=</span> <span style="color: #003366; font-weight: bold;">function</span><span style="color: #009900;">&#40;</span>duck<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
        originalBar.<span style="color: #660066;">apply</span><span style="color: #009900;">&#40;</span><span style="color: #000066; font-weight: bold;">this</span><span style="color: #339933;">,</span> arguments<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
        console.<span style="color: #660066;">log</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">&quot;new bar&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
   <span style="color: #009900;">&#125;</span><span style="color: #339933;">;</span>
<span style="color: #009900;">&#125;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span></pre></div></div>

<p>In this case the function only takes in one argument so there&#8217;s not much noticeable improvement in the code but when a function takes multiple arguments then using &#8216;apply&#8217; is certainly a cleaner approach.</p>
]]></content:encoded>
			<wfw:commentRss>http://www.markhneedham.com/blog/2010/02/28/javascript-confusing-call-and-apply/feed/</wfw:commentRss>
		<slash:comments>6</slash:comments>
		</item>
	</channel>
</rss>

