<?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>Coffee on the Keyboard &#187; tutorial</title>
	<atom:link href="http://coffeeonthekeyboard.com/tag/tutorial/feed/" rel="self" type="application/rss+xml" />
	<link>http://coffeeonthekeyboard.com</link>
	<description>by James Socol</description>
	<lastBuildDate>Mon, 06 Feb 2012 23:33:42 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.3.1</generator>
<atom:link rel="hub" href="http://pubsubhubbub.appspot.com"/>		<item>
		<title>JavaScript: Private Static Members, Part 1</title>
		<link>http://coffeeonthekeyboard.com/javascript-private-static-members-part-1-208/</link>
		<comments>http://coffeeonthekeyboard.com/javascript-private-static-members-part-1-208/#comments</comments>
		<pubDate>Tue, 28 Apr 2009 14:47:54 +0000</pubDate>
		<dc:creator>James</dc:creator>
				<category><![CDATA[Articles]]></category>
		<category><![CDATA[Code]]></category>
		<category><![CDATA[javascript]]></category>
		<category><![CDATA[oop]]></category>
		<category><![CDATA[programming]]></category>
		<category><![CDATA[tutorial]]></category>

		<guid isPermaLink="false">http://coffeeonthekeyboard.com/?p=208</guid>
		<description><![CDATA[A little while ago I talked about creating private variables and methods in JavaScript. This works, but is not necessarily efficient: each instance of the class creates new copies of the members. While that may be exactly what you want for instance variables (think of partNum in the old examples) it is not always ideal. [...]]]></description>
			<content:encoded><![CDATA[<p><a href="http://coffeeonthekeyboard.com/private-variables-in-javascript-177/" title="A little while ago">A little while ago</a> I talked about creating private variables and methods in JavaScript. This works, but is not necessarily efficient: each instance of the class creates new copies of the members. While that may be exactly what you want for instance variables (think of <code>partNum</code> in the old examples) it is not always ideal.</p>
<p>The complexity jumps significantly, though. So I&#8217;m dividing this half into two parts.</p>
<p>To get started, we need to forget about all this Object-Oriented Programming for a minute and look at some of the neat <a href="http://coffeeonthekeyboard.com/firefox-open-in-blank-tab-197/" title="tricks">tricks</a> you can do with functions in JavaScript.</p>
<p><strong>Update:</strong> <a href="http://coffeeonthekeyboard.com/javascript-private-static-members-part-2-218/" title="Part 2">Part 2</a> is now available.<span id="more-208"></span></p>
<p>First, let&#8217;s take a look at a few ways to define a function in JavaScript:</p>
<div class="dean_ch" style="white-space: wrap;">
<ol>
<li class="li1">
<div class="de1"><span class="kw2">function</span> oneFunction <span class="br0">&#40;</span><span class="br0">&#41;</span></div>
</li>
<li class="li1">
<div class="de1"><span class="br0">&#123;</span></div>
</li>
<li class="li1">
<div class="de1">&nbsp; &nbsp; <span class="co1">// function body goes here</span></div>
</li>
<li class="li1">
<div class="de1"><span class="br0">&#125;</span></div>
</li>
<li class="li2">
<div class="de2">&nbsp;</div>
</li>
<li class="li1">
<div class="de1"><span class="kw2">var</span> anotherFunction = <span class="kw2">function</span> <span class="br0">&#40;</span><span class="br0">&#41;</span></div>
</li>
<li class="li1">
<div class="de1"><span class="br0">&#123;</span></div>
</li>
<li class="li1">
<div class="de1">&nbsp; &nbsp; <span class="co1">// function body goes here</span></div>
</li>
<li class="li1">
<div class="de1"><span class="br0">&#125;</span>;</div>
</li>
</ol>
</div>
<p>The first example, <code>oneFunction</code> should be familiar to programmers from most languages. The second one is completely equivalent, but works slightly differently. In this case, the right-hand side, a function, is being assigned to the left-hand side, the var <code>anotherFunction</code>.</p>
<p>Remember that in JavaScript, functions are first-class objects, just like everything else, so can be declared with the <code>var</code> keyword. They can also be passed to other functions as arguments, or returned from functions.</p>
<p>Now let&#8217;s take a brief look at parentheses. What do parentheses really do? Essentially, they evaluate and return whatever expression is inside them. For example:</p>
<div class="dean_ch" style="white-space: wrap;">
<ol>
<li class="li1">
<div class="de1"><span class="kw2">var</span> five = <span class="br0">&#40;</span><span class="nu0">5</span><span class="br0">&#41;</span>;</div>
</li>
<li class="li1">
<div class="de1"><span class="co1">// the expression is &quot;5&quot;</span></div>
</li>
<li class="li1">
<div class="de1">&nbsp;</div>
</li>
<li class="li1">
<div class="de1"><span class="kw2">var</span> nine = <span class="br0">&#40;</span><span class="nu0">2</span> * <span class="nu0">4</span><span class="br0">&#41;</span> + <span class="nu0">1</span>;</div>
</li>
<li class="li2">
<div class="de2"><span class="co1">// &quot;2 * 4&quot; is evaluated and returned as &quot;8&quot;</span></div>
</li>
<li class="li1">
<div class="de1">&nbsp;</div>
</li>
<li class="li1">
<div class="de1"><span class="kw2">var</span> nottrue = <span class="br0">&#40;</span><span class="kw2">true</span> || <span class="kw2">false</span><span class="br0">&#41;</span> &amp;amp;&amp;amp; <span class="kw2">false</span>;</div>
</li>
<li class="li1">
<div class="de1"><span class="co1">// &quot;true || false&quot; evalutes to &quot;true&quot;</span></div>
</li>
<li class="li1">
<div class="de1">&nbsp;</div>
</li>
<li class="li2">
<div class="de2"><span class="kw2">var</span> thirty = <span class="br0">&#40;</span><span class="br0">&#40;</span><span class="nu0">5</span>*<span class="nu0">5</span><span class="br0">&#41;</span><span class="nu0">-10</span><span class="br0">&#41;</span>*<span class="nu0">2</span>;</div>
</li>
<li class="li1">
<div class="de1"><span class="co1">// &quot;5*5&quot; is evaluated, then returned as 25 to 25-10,</span></div>
</li>
<li class="li1">
<div class="de1"><span class="co1">// which evaluates to 15, which is returned and doubled</span></div>
</li>
</ol>
</div>
<p>So parentheses are slightly more powerful than the simple grouping operation we associate with them. Sometimes we see examples like this, which may be more illustrative:</p>
<div class="dean_ch" style="white-space: wrap;">
<ol>
<li class="li1">
<div class="de1"><span class="kw2">function</span> checkName <span class="br0">&#40;</span><span class="kw3">name</span><span class="br0">&#41;</span></div>
</li>
<li class="li1">
<div class="de1"><span class="br0">&#123;</span></div>
</li>
<li class="li1">
<div class="de1">&nbsp; &nbsp;<span class="kw1">return</span> <span class="br0">&#40;</span><span class="kw3">name</span>==<span class="st0">&#8216;admin&#8217;</span><span class="br0">&#41;</span>;</div>
</li>
<li class="li1">
<div class="de1"><span class="br0">&#125;</span></div>
</li>
</ol>
</div>
<p>So what happens if we combine parentheses&#8217; ability to evaluate and return code with our ability to define functions as an expression?</p>
<div class="dean_ch" style="white-space: wrap;">
<ol>
<li class="li1">
<div class="de1"><span class="kw2">var</span> aFunc = <span class="br0">&#40;</span><span class="kw2">function</span> <span class="br0">&#40;</span><span class="br0">&#41;</span> <span class="br0">&#123;</span> <span class="coMULTI">/*&#8230;*/</span> <span class="br0">&#125;</span><span class="br0">&#41;</span>;</div>
</li>
</ol>
</div>
<p>Of course, this is just the same as <code>anotherFunction</code> above, but you can see that the right-hand side &#8220;returns&#8221; a function. Let&#8217;s do something a little different now:</p>
<div class="dean_ch" style="white-space: wrap;">
<ol>
<li class="li1">
<div class="de1"><span class="br0">&#40;</span><span class="kw2">function</span> <span class="br0">&#40;</span><span class="kw3">name</span><span class="br0">&#41;</span> <span class="br0">&#123;</span></div>
</li>
<li class="li1">
<div class="de1"><span class="kw3">alert</span><span class="br0">&#40;</span><span class="st0">&quot;Hello, &quot;</span>+<span class="kw3">name</span>+<span class="st0">&quot;!&quot;</span><span class="br0">&#41;</span>;</div>
</li>
<li class="li1">
<div class="de1"><span class="br0">&#125;</span><span class="br0">&#41;</span><span class="br0">&#40;</span><span class="st0">&quot;World&quot;</span><span class="br0">&#41;</span>;</div>
</li>
</ol>
</div>
<p>What&#8217;s going on here? The first set of parentheses [<code>(function ... )</code>] evaluate and return the code inside, creating a function. The last set [<code>("World")</code>] are then <em>calling</em> the function created by the first set. Immediately.</p>
<p>This is a powerful technique, but has certain limits. The interior function is executed immediately on creation, which means the <abbr title="Document Object Model">DOM</abbr> will probably not be loaded yet. Once the function is executed, it is lost. Trying to save it in the left-hand side of an equation will only save the <em>return value</em> of the function, not the function itself. For example:</p>
<div class="dean_ch" style="white-space: wrap;">
<ol>
<li class="li1">
<div class="de1"><span class="kw2">var</span> aFunc = <span class="br0">&#40;</span><span class="kw2">function</span> <span class="br0">&#40;</span><span class="br0">&#41;</span> <span class="br0">&#123;</span></div>
</li>
<li class="li1">
<div class="de1">&nbsp; &nbsp; <span class="kw1">return</span> <span class="st0">&quot;I&#8217;m not a function!&quot;</span>;</div>
</li>
<li class="li1">
<div class="de1"><span class="br0">&#125;</span><span class="br0">&#41;</span><span class="br0">&#40;</span><span class="br0">&#41;</span>;</div>
</li>
<li class="li1">
<div class="de1"><span class="kw3">alert</span><span class="br0">&#40;</span>aFunc<span class="br0">&#41;</span>; <span class="co1">// Alerts &quot;I&#8217;m not a function!&quot;</span></div>
</li>
</ol>
</div>
<p>But, remember what I said about functions as first-class objects? It means we can use one function as a return value from another function:</p>
<div class="dean_ch" style="white-space: wrap;">
<ol>
<li class="li1">
<div class="de1"><span class="kw2">var</span> bFunc = <span class="br0">&#40;</span><span class="kw2">function</span> <span class="br0">&#40;</span><span class="br0">&#41;</span> <span class="br0">&#123;</span> <span class="co1">// 1</span></div>
</li>
<li class="li1">
<div class="de1">&nbsp; &nbsp; <span class="kw1">return</span> <span class="kw2">function</span> <span class="br0">&#40;</span><span class="br0">&#41;</span> <span class="br0">&#123;</span> <span class="co1">// 2</span></div>
</li>
<li class="li1">
<div class="de1">&nbsp; &nbsp; &nbsp; &nbsp; <span class="kw3">alert</span><span class="br0">&#40;</span><span class="st0">&quot;Hello, World!&quot;</span><span class="br0">&#41;</span>;</div>
</li>
<li class="li1">
<div class="de1">&nbsp; &nbsp; <span class="br0">&#125;</span>;</div>
</li>
<li class="li2">
<div class="de2"><span class="br0">&#125;</span><span class="br0">&#41;</span><span class="br0">&#40;</span><span class="br0">&#41;</span>; <span class="co1">// 3</span></div>
</li>
</ol>
</div>
<p>The outer function (1) is executed immediately (3), and the var <code>bFunc</code> stores its return value, which is the inner function (2). So now, <code>bFunc</code> is a function, and calling <code>bFunc()</code> will alert &#8220;Hello, World!&#8221;.</p>
<p>We&#8217;ll stop for now. If this technique is new to you, play with it for a while. If not, just hang tight and I&#8217;ll get to <a href="http://coffeeonthekeyboard.com/javascript-private-static-members-part-2-218/" title="Part 2">Part 2</a> soon enough.</p>
]]></content:encoded>
			<wfw:commentRss>http://coffeeonthekeyboard.com/javascript-private-static-members-part-1-208/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
	</channel>
</rss>

