<?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; javascript</title>
	<atom:link href="http://coffeeonthekeyboard.com/tag/javascript/feed/" rel="self" type="application/rss+xml" />
	<link>http://coffeeonthekeyboard.com</link>
	<description>by James Socol</description>
	<lastBuildDate>Fri, 20 Apr 2012 22:17:14 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.3.2</generator>
<atom:link rel="hub" href="http://pubsubhubbub.appspot.com"/>		<item>
		<title>The Problem with TodaysMeet</title>
		<link>http://coffeeonthekeyboard.com/the-problem-with-todaysmeet-550/</link>
		<comments>http://coffeeonthekeyboard.com/the-problem-with-todaysmeet-550/#comments</comments>
		<pubDate>Sat, 29 Jan 2011 16:35:42 +0000</pubDate>
		<dc:creator>James</dc:creator>
				<category><![CDATA[Articles]]></category>
		<category><![CDATA[architecture]]></category>
		<category><![CDATA[Design]]></category>
		<category><![CDATA[framework]]></category>
		<category><![CDATA[javascript]]></category>
		<category><![CDATA[PHP]]></category>
		<category><![CDATA[todaysmeet]]></category>
		<category><![CDATA[webdev]]></category>

		<guid isPermaLink="false">http://coffeeonthekeyboard.com/?p=550</guid>
		<description><![CDATA[TodaysMeet is a project I started in 2008 to help my father solve a problem in one of his classes. The fact that it&#8217;s as popular as it is—mostly in education—never ceases to amaze me. Unfortunately, I don&#8217;t give TodaysMeet the attention it, and more importantly its users, deserve. This is because TodaysMeet has two [...]]]></description>
			<content:encoded><![CDATA[<p><a href="http://todaysmeet.com/">TodaysMeet</a> is a project I started in 2008 to help <a href="http://speedchange.blogspot.com">my father</a> solve a problem in one of his classes. The fact that it&#8217;s as popular as it is—mostly in education—never ceases to amaze me.</p>
<p>Unfortunately, I don&#8217;t give TodaysMeet the attention it, and more importantly its users, deserve. This is because TodaysMeet has two fatal flaws that, if they haven&#8217;t crippled it yet, will someday.</p>
<ul>
<li>The UI is based on proof-of-concept JavaScript.</li>
<li>The back-end is based on <a href="http://svn.jamessocol.com/maveric">my own framework</a>.</li>
</ul>
<p>What follows is the sad history of TodaysMeet development.</p>
<h3>Origin Story</h3>
<p>TodaysMeet came out of a conversation between my father and I, but it&#8217;s origins are slightly older. In some downtime in late 2007 I was trying to familiarize myself with various JavaScript frameworks by writing a UI for the same back-end in each of them. It was a pretty basic Ajax comment system. I believe it polled the server every minute. If I remember correctly, I got busy and abandoned it after creating <a href="http://www.prototypejs.org/">Prototype</a> and <a href="http://jquery.com/">jQuery</a> versions.</p>
<p>Around the same time I was enamored of <a href="http://rubyonrails.org/">Rails</a>, and trying to round out <a href="http://svn.jamessocol.com/maveric">Maveric</a> into a decent Rails-inspired PHP framework.</p>
<p>So when my father said he wanted something like Twitter for a single classroom, that he could project on a wall, and wouldn&#8217;t require signing up, I put these things together in my head. TodaysMeet is basically the proof-of-concept Prototype JS running on top of an old version of Maveric.</p>
<h3>The Situation Now</h3>
<p><a href="http://www.brandonsavage.net/why-every-developer-should-write-their-own-framework/">Every developer should write a framework</a>, I think it&#8217;s a fantastic learning experience. But they should never build a production website out of it.</p>
<p>Even though Maveric got a little better after I created TodaysMeet, it&#8217;s still based on an untested, unsupported framework with no support for basic things like storage back-ends or caching.</p>
<p>The UI is still based on Prototype, which I haven&#8217;t used in years, and the fundamental client-server interactions are still that original &#8220;learning the library&#8221; code.</p>
<p>Essentially, TodaysMeet is a prototype masquerading as a production-ready product.</p>
<p>The result is that working on it is slow, difficult, and frankly unpleasant. Adding features—like the long-promised password protected rooms—is painful and, with no test suite, dangerous. The one real feature I added, Twitter integration, barely works when it works at all.</p>
<p>But users don&#8217;t care about any of that. They see that it works, mostly. They might see that it doesn&#8217;t get much attention and the UI feels three years old (because it is, of course).</p>
<h3>Where Do We Go From Here?</h3>
<p>TodaysMeet <em>could</em> be awesome, but it needs to go all the way down to the basic stack and get rebuilt. TodaysMeet is an absolutely perfect candidate for all sorts of new, exciting tools and techniques. To use any of them means starting over.</p>
<p>This is the first of a two-part post. In the next part, I&#8217;m going to outline the architecture I <em>want</em>, instead of the architecture I <em>have</em>.</p>
<p>Hopefully, some social aspect of talking about this will lead to me actually doing something about it.</p>
]]></content:encoded>
			<wfw:commentRss>http://coffeeonthekeyboard.com/the-problem-with-todaysmeet-550/feed/</wfw:commentRss>
		<slash:comments>3</slash:comments>
		</item>
		<item>
		<title>JavaScript: Private Static Members, Part 2</title>
		<link>http://coffeeonthekeyboard.com/javascript-private-static-members-part-2-218/</link>
		<comments>http://coffeeonthekeyboard.com/javascript-private-static-members-part-2-218/#comments</comments>
		<pubDate>Tue, 19 May 2009 14:55:36 +0000</pubDate>
		<dc:creator>James</dc:creator>
				<category><![CDATA[Articles]]></category>
		<category><![CDATA[Code]]></category>
		<category><![CDATA[Front-end]]></category>
		<category><![CDATA[javascript]]></category>

		<guid isPermaLink="false">http://coffeeonthekeyboard.com/?p=218</guid>
		<description><![CDATA[Finally, it&#8217;s time to finish up the lesson on private static members and methods in JavaScript. Last time, I introduced the technique of creating and immediately executing a function, using parentheses. I talked a little about returning a function and storing it in a variable. var myFunc = &#40;function &#40;&#41; &#123; &#160; return function &#40;&#41; [...]]]></description>
			<content:encoded><![CDATA[<p>Finally, it&#8217;s time to finish up the lesson on private static members and methods in JavaScript.</p>
<p><a href="http://coffeeonthekeyboard.com/javascript-private-static-members-part-1-208/">Last time</a>, I introduced the technique of creating and immediately executing a function, using parentheses. I talked a little about <em>returning</em> a function and storing it in a variable.</p>
<div class="dean_ch" style="white-space: wrap;">
<ol>
<li class="li1">
<div class="de1"><span class="kw2">var</span> myFunc = <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; <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></div>
</li>
<li class="li1">
<div class="de1">&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; <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>;</div>
</li>
<li class="li1">
<div class="de1">&nbsp;</div>
</li>
<li class="li1">
<div class="de1"><span class="kw3">alert</span><span class="br0">&#40;</span>myFunc<span class="br0">&#41;</span>; <span class="co1">// &quot;function () &#8230; &quot;</span></div>
</li>
<li class="li1">
<div class="de1">&nbsp;</div>
</li>
<li class="li1">
<div class="de1">myFunc<span class="br0">&#40;</span><span class="br0">&#41;</span>; <span class="co1">// Hello, World!</span></div>
</li>
</ol>
</div>
<p><span id="more-218"></span><br />
There are a lot of things you can do with this trick, like create interesting <a href="http://coffeeonthekeyboard.com/firefox-open-in-blank-tab-197/">bookmarklets</a>. But let&#8217;s see how you can use it to protect information on the class level.</p>
<p>Here we&#8217;ll take advantage of JavaScript&#8217;s scope behavior. Remember that a function uses the variables where it is <em>defined</em>, not executed. Perhaps a better example&#8230;</p>
<div class="dean_ch" style="white-space: wrap;">
<ol>
<li class="li1">
<div class="de1"><span class="kw2">var</span> myFunc = <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; <span class="kw2">var</span> message = <span class="st0">&quot;I&#8217;m hidden.&quot;</span>;</div>
</li>
<li class="li1">
<div class="de1">&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></div>
</li>
<li class="li1">
<div class="de1">&nbsp; &nbsp; <span class="kw3">alert</span><span class="br0">&#40;</span>message<span class="br0">&#41;</span>;</div>
</li>
<li class="li2">
<div class="de2">&nbsp; <span class="br0">&#125;</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">&nbsp;</div>
</li>
<li class="li1">
<div class="de1"><span class="kw2">var</span> message = <span class="st0">&quot;I&#8217;m visible.&quot;</span>;</div>
</li>
<li class="li1">
<div class="de1">&nbsp;</div>
</li>
<li class="li2">
<div class="de2">myFunc<span class="br0">&#40;</span><span class="br0">&#41;</span>; <span class="co1">// &quot;I&#8217;m hidden.&quot;</span></div>
</li>
</ol>
</div>
<p>We can see that the inner function (which is returned from the outer function and set to <code>myFunc</code>) uses the value of <code>message</code> from the block where it was defined, not executed.</p>
<p>Now you should be able to see where this is going. Let&#8217;s look at a more complex example, extended from the <a href="http://coffeeonthekeyboard.com/private-variables-in-javascript-177/">first part</a>:</p>
<div class="dean_ch" style="white-space: wrap;">
<ol>
<li class="li1">
<div class="de1"><span class="kw2">var</span> Product = <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="kw2">var</span> partNumRegex = <span class="re0">/^\d<span class="br0">&#123;</span><span class="nu0">4</span><span class="br0">&#125;</span>\-\d<span class="br0">&#123;</span><span class="nu0">2</span><span class="br0">&#125;</span>$/</span>;</div>
</li>
<li class="li1">
<div class="de1">&nbsp;</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> num <span class="br0">&#41;</span></div>
</li>
<li class="li2">
<div class="de2">&nbsp; &nbsp; <span class="br0">&#123;</span></div>
</li>
<li class="li1">
<div class="de1">&nbsp; &nbsp; &nbsp; &nbsp; <span class="kw2">var</span> partNum = <span class="kw2">null</span>;</div>
</li>
<li class="li1">
<div class="de1">&nbsp;</div>
</li>
<li class="li1">
<div class="de1">&nbsp; &nbsp; &nbsp; &nbsp; <span class="kw1">this</span>.<span class="me1">setPartNum</span> = <span class="kw2">function</span> <span class="br0">&#40;</span> n <span class="br0">&#41;</span></div>
</li>
<li class="li1">
<div class="de1">&nbsp; &nbsp; &nbsp; &nbsp; <span class="br0">&#123;</span></div>
</li>
<li class="li2">
<div class="de2">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span class="kw1">if</span> <span class="br0">&#40;</span>partNumRegex.<span class="me1">test</span><span class="br0">&#40;</span>n<span class="br0">&#41;</span><span class="br0">&#41;</span> <span class="br0">&#123;</span></div>
</li>
<li class="li1">
<div class="de1">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; partNum = n;</div>
</li>
<li class="li1">
<div class="de1">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span class="kw1">return</span> <span class="kw2">true</span>;</div>
</li>
<li class="li1">
<div class="de1">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span class="br0">&#125;</span> <span class="kw1">else</span> <span class="br0">&#123;</span></div>
</li>
<li class="li1">
<div class="de1">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span class="kw1">return</span> <span class="kw2">false</span>;</div>
</li>
<li class="li2">
<div class="de2">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span class="br0">&#125;</span></div>
</li>
<li class="li1">
<div class="de1">&nbsp; &nbsp; &nbsp; &nbsp; <span class="br0">&#125;</span></div>
</li>
<li class="li1">
<div class="de1">&nbsp;</div>
</li>
<li class="li1">
<div class="de1">&nbsp; &nbsp; &nbsp; &nbsp; <span class="kw1">this</span>.<span class="me1">getPartNum</span> = <span class="kw2">function</span> <span class="br0">&#40;</span><span class="br0">&#41;</span></div>
</li>
<li class="li1">
<div class="de1">&nbsp; &nbsp; &nbsp; &nbsp; <span class="br0">&#123;</span></div>
</li>
<li class="li2">
<div class="de2">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span class="kw1">return</span> partNum;</div>
</li>
<li class="li1">
<div class="de1">&nbsp; &nbsp; &nbsp; &nbsp; <span class="br0">&#125;</span></div>
</li>
<li class="li1">
<div class="de1">&nbsp;</div>
</li>
<li class="li1">
<div class="de1">&nbsp; &nbsp; &nbsp; &nbsp; <span class="kw1">if</span> <span class="br0">&#40;</span>num<span class="br0">&#41;</span> <span class="kw1">this</span>.<span class="me1">setPartNum</span><span class="br0">&#40;</span>num<span class="br0">&#41;</span>;</div>
</li>
<li class="li1">
<div class="de1">&nbsp;</div>
</li>
<li class="li2">
<div class="de2">&nbsp; &nbsp; &nbsp; &nbsp; <span class="kw1">return</span> <span class="kw1">this</span>;</div>
</li>
<li class="li1">
<div class="de1">&nbsp; &nbsp; <span class="br0">&#125;</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">&nbsp;</div>
</li>
<li class="li1">
<div class="de1"><span class="kw2">var</span> car = <span class="kw2">new</span> Product;</div>
</li>
<li class="li2">
<div class="de2">car.<span class="me1">setPartNum</span><span class="br0">&#40;</span><span class="st0">&#8217;1234-56&#8242;</span><span class="br0">&#41;</span>;</div>
</li>
<li class="li1">
<div class="de1">&nbsp;</div>
</li>
<li class="li1">
<div class="de1"><span class="kw2">var</span> table = <span class="kw2">new</span> Product;</div>
</li>
<li class="li1">
<div class="de1">table.<span class="me1">setPartNum</span><span class="br0">&#40;</span><span class="st0">&#8217;345678&#8242;</span><span class="br0">&#41;</span>;</div>
</li>
<li class="li1">
<div class="de1">&nbsp;</div>
</li>
<li class="li2">
<div class="de2"><span class="kw3">alert</span><span class="br0">&#40;</span><span class="st0">&quot;Car: &quot;</span>+car.<span class="me1">getPartNum</span><span class="br0">&#40;</span><span class="br0">&#41;</span><span class="br0">&#41;</span>; <span class="co1">// &quot;Car: 1234-56&quot;</span></div>
</li>
<li class="li1">
<div class="de1"><span class="kw3">alert</span><span class="br0">&#40;</span><span class="st0">&quot;Table: &quot;</span>+table.<span class="me1">getPartNum</span><span class="br0">&#40;</span><span class="br0">&#41;</span><span class="br0">&#41;</span>; <span class="co1">// &quot;Table: null&quot;</span></div>
</li>
</ol>
</div>
<p>What&#8217;s the advantage here? The variable <code>partNumRegex</code> is <em>not</em> copied by the <code>new</code> operator. In a small example like this, there is not much benefit, but if you had hundreds of <code>Product</code> objects, you could save a significant amount of memory.</p>
<p>There are a few major drawbacks: a public static (class) method cannot access a private static method or variable. For example:</p>
<div class="dean_ch" style="white-space: wrap;">
<ol>
<li class="li1">
<div class="de1"><span class="kw2">var</span> Product = <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="kw2">var</span> partNumRegex = <span class="re0">/^\d<span class="br0">&#123;</span><span class="nu0">4</span><span class="br0">&#125;</span>\-\d<span class="br0">&#123;</span><span class="nu0">2</span><span class="br0">&#125;</span>$/</span>;</div>
</li>
<li class="li1">
<div class="de1">&nbsp;</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></div>
</li>
<li class="li2">
<div class="de2">&nbsp; &nbsp; &nbsp; &nbsp; <span class="co1">// snip</span></div>
</li>
<li class="li1">
<div class="de1">&nbsp; &nbsp; <span class="br0">&#125;</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">&nbsp;</div>
</li>
<li class="li1">
<div class="de1">Product.<span class="me1">validPartNum</span> = <span class="kw2">function</span> <span class="br0">&#40;</span>num<span class="br0">&#41;</span> <span class="br0">&#123;</span></div>
</li>
<li class="li2">
<div class="de2">&nbsp; &nbsp; <span class="kw1">if</span> <span class="br0">&#40;</span>partNumRegex.<span class="me1">test</span><span class="br0">&#40;</span>num<span class="br0">&#41;</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; &nbsp; &nbsp; <span class="kw1">return</span> <span class="kw2">true</span>;</div>
</li>
<li class="li1">
<div class="de1">&nbsp; &nbsp; <span class="br0">&#125;</span></div>
</li>
<li class="li1">
<div class="de1">&nbsp; &nbsp; <span class="kw1">return</span> <span class="kw2">false</span>;</div>
</li>
<li class="li1">
<div class="de1"><span class="br0">&#125;</span></div>
</li>
</ol>
</div>
<p>The class method <code>validPartNum</code> has no access to the private class variable <code>partNumRegex</code>, and so will throw an error at (1). Adding an accessor <em>must</em> be done on the <em>instance</em>, not the class, like so:</p>
<div class="dean_ch" style="white-space: wrap;">
<ol>
<li class="li1">
<div class="de1"><span class="kw2">var</span> Product = <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="kw2">var</span> partNumRegex = <span class="re0">/^\d<span class="br0">&#123;</span><span class="nu0">4</span><span class="br0">&#125;</span>\-\d<span class="br0">&#123;</span><span class="nu0">2</span><span class="br0">&#125;</span>$/</span>;</div>
</li>
<li class="li1">
<div class="de1">&nbsp;</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></div>
</li>
<li class="li2">
<div class="de2">&nbsp; &nbsp; &nbsp; &nbsp; <span class="kw1">this</span>.<span class="me1">getPartNumRegex</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; &nbsp; &nbsp; &nbsp; &nbsp; <span class="kw1">return</span> partNumRegex;</div>
</li>
<li class="li1">
<div class="de1">&nbsp; &nbsp; &nbsp; &nbsp; <span class="br0">&#125;</span></div>
</li>
<li class="li1">
<div class="de1">&nbsp;</div>
</li>
<li class="li1">
<div class="de1">&nbsp; &nbsp; &nbsp; &nbsp; <span class="co1">// snip</span></div>
</li>
<li class="li2">
<div class="de2">&nbsp; &nbsp; <span class="br0">&#125;</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>
</ol>
</div>
<p>But then you cannot access the private variable without first creating an instance of the class, and the accessor function is copied with the <code>new</code> operator. New methods added to the <code>Product.prototype</code> object are likewise unable to access the private static variables. This is a limitation of JavaScript.</p>
<p>Even with these limitations, the ability to hide implementation behind an agreed-upon interface is powerful. (JavaScript doesn&#8217;t actually have interfaces, but you can just write it down.) Behind the scenes, you could load new data via Ajax, without ever exposing your Ajax method to that new guy down the hall who likes to misuse everything he can:</p>
<div class="dean_ch" style="white-space: wrap;">
<ol>
<li class="li1">
<div class="de1"><span class="kw2">var</span> Product = <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="kw2">var</span> partNumRegex = <span class="re0">/^\d<span class="br0">&#123;</span><span class="nu0">4</span><span class="br0">&#125;</span>\-\d<span class="br0">&#123;</span><span class="nu0">2</span><span class="br0">&#125;</span>$/</span>;</div>
</li>
<li class="li1">
<div class="de1">&nbsp;</div>
</li>
<li class="li1">
<div class="de1">&nbsp; &nbsp; <span class="co1">// private static function, not copied with &quot;new&quot;</span></div>
</li>
<li class="li2">
<div class="de2">&nbsp; &nbsp; <span class="kw2">function</span> loadPartData<span class="br0">&#40;</span>partNum<span class="br0">&#41;</span> <span class="br0">&#123;</span></div>
</li>
<li class="li1">
<div class="de1">&nbsp; &nbsp; &nbsp; &nbsp; <span class="co1">// load data via Ajax</span></div>
</li>
<li class="li1">
<div class="de1">&nbsp; &nbsp; <span class="br0">&#125;</span></div>
</li>
<li class="li1">
<div class="de1">&nbsp;</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></div>
</li>
<li class="li2">
<div class="de2">&nbsp;</div>
</li>
<li class="li1">
<div class="de1">&nbsp; &nbsp; &nbsp; &nbsp; <span class="kw2">var</span> partNum = <span class="kw2">null</span>;</div>
</li>
<li class="li1">
<div class="de1">&nbsp;</div>
</li>
<li class="li1">
<div class="de1">&nbsp; &nbsp; &nbsp; &nbsp; <span class="co1">// snip</span></div>
</li>
<li class="li1">
<div class="de1">&nbsp;</div>
</li>
<li class="li2">
<div class="de2">&nbsp; &nbsp; &nbsp; &nbsp; <span class="kw1">this</span>.<span class="me1">setPartNum</span><span class="br0">&#40;</span>num<span class="br0">&#41;</span> <span class="br0">&#123;</span></div>
</li>
<li class="li1">
<div class="de1">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span class="kw1">if</span><span class="br0">&#40;</span>partNumRegex.<span class="me1">test</span><span class="br0">&#40;</span>num<span class="br0">&#41;</span><span class="br0">&#41;</span> <span class="br0">&#123;</span></div>
</li>
<li class="li1">
<div class="de1">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; partNum = num;</div>
</li>
<li class="li1">
<div class="de1">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span class="kw2">var</span> data = loadPartData<span class="br0">&#40;</span>partNum<span class="br0">&#41;</span>;</div>
</li>
<li class="li1">
<div class="de1">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span class="kw1">this</span>.<span class="me1">productName</span> = data.<span class="me1">productName</span>;</div>
</li>
<li class="li2">
<div class="de2">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span class="kw1">this</span>.<span class="me1">price</span> = data.<span class="me1">price</span>;</div>
</li>
<li class="li1">
<div class="de1">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span class="kw1">return</span> <span class="kw2">true</span>;</div>
</li>
<li class="li1">
<div class="de1">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span class="br0">&#125;</span> <span class="kw1">else</span> <span class="br0">&#123;</span></div>
</li>
<li class="li1">
<div class="de1">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span class="kw1">return</span> <span class="kw2">false</span>;</div>
</li>
<li class="li1">
<div class="de1">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span class="br0">&#125;</span></div>
</li>
<li class="li2">
<div class="de2">&nbsp; &nbsp; &nbsp; &nbsp; <span class="br0">&#125;</span></div>
</li>
<li class="li1">
<div class="de1">&nbsp;</div>
</li>
<li class="li1">
<div class="de1">&nbsp; &nbsp; &nbsp; &nbsp; <span class="co1">// snip</span></div>
</li>
<li class="li1">
<div class="de1">&nbsp; &nbsp; <span class="br0">&#125;</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>
</ol>
</div>
<p>That&#8217;s it for now. I owe most of these three articles to the book <a href="http://www.amazon.com/JavaScript-Design-Patterns-Recipes-Problem-Solution/dp/159059908X">Pro JavaScript Design Patterns</a>, by Ross Harmes and Dustin Diaz. Those two are geniuses, and anyone who wants to be a better JavaScript programmer would do well to pick up their book.</p>
<p>Next up, I&#8217;ll argue why the <code>&lt;dl&gt;</code> tag is a good way to display forms semantically.</p>
]]></content:encoded>
			<wfw:commentRss>http://coffeeonthekeyboard.com/javascript-private-static-members-part-2-218/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<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>
		<item>
		<title>Firefox: Open in Blank Tab</title>
		<link>http://coffeeonthekeyboard.com/firefox-open-in-blank-tab-197/</link>
		<comments>http://coffeeonthekeyboard.com/firefox-open-in-blank-tab-197/#comments</comments>
		<pubDate>Wed, 22 Apr 2009 14:47:41 +0000</pubDate>
		<dc:creator>James</dc:creator>
				<category><![CDATA[Articles]]></category>
		<category><![CDATA[firefox]]></category>
		<category><![CDATA[javascript]]></category>
		<category><![CDATA[links]]></category>
		<category><![CDATA[tricks]]></category>

		<guid isPermaLink="false">http://coffeeonthekeyboard.com/?p=197</guid>
		<description><![CDATA[If you don&#8217;t use Firefox 3, go get it. Then finish this article. (Safari and Opera users are excused, but there&#8217;s no promise this will work for them.) One of my (few) gripes with Firefox is that bookmarks on the toolbar have no &#8220;open in blank tab&#8221; option. They have an &#8220;open in sidebar&#8221; option, [...]]]></description>
			<content:encoded><![CDATA[<p>If you don&#8217;t use Firefox 3, <a href="http://www.mozilla.com/en-US/">go get it</a>. Then finish this article. (Safari and Opera users are excused, but there&#8217;s no promise this will work for them.)</p>
<p>One of my (few) gripes with Firefox is that bookmarks on the toolbar have no &#8220;open in blank tab&#8221; option. They have an &#8220;open in sidebar&#8221; option, but those uses are rare and esoteric at best. Personally, I never use the sidebar.</p>
<p>&#8220;Open in blank tab&#8221; should basically do this: if there is a blank tab, use it; if not, create a new tab. Frankly, it could just open in a new tab regardless, but it seems like such an easy thing to add.</p>
<p>But? It can&#8217;t be done directly in Firefox. Hence, I present this small script:</p>
<div class="dean_ch" style="white-space: wrap;">
<ol>
<li class="li1">
<div class="de1">javascript:</div>
</li>
<li class="li1">
<div class="de1"><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="kw2">var</span> u=<span class="st0">&#8216;http://mail.google.com/mail&#8217;</span>;</div>
</li>
<li class="li1">
<div class="de1">&nbsp; <span class="kw1">if</span><span class="br0">&#40;</span>window.<span class="me1">location</span>==<span class="st0">&#8216;about:blank&#8217;</span><span class="br0">&#41;</span><span class="br0">&#123;</span></div>
</li>
<li class="li1">
<div class="de1">&nbsp; &nbsp; window.<span class="me1">location</span>=u;</div>
</li>
<li class="li2">
<div class="de2">&nbsp; <span class="br0">&#125;</span><span class="kw1">else</span><span class="br0">&#123;</span></div>
</li>
<li class="li1">
<div class="de1">&nbsp; &nbsp; window.<span class="kw3">open</span><span class="br0">&#40;</span>u,<span class="st0">&#8221;</span><span class="br0">&#41;</span>;</div>
</li>
<li class="li1">
<div class="de1">&nbsp; <span class="br0">&#125;</span></div>
</li>
<li class="li1">
<div class="de1"><span class="br0">&#41;</span><span class="br0">&#40;</span><span class="br0">&#41;</span>;</div>
</li>
</ol>
</div>
<p>That&#8217;s it. Try dragging this link to <a href="javascript:(function(){var%20u='http://mail.google.com/mail';%20if(window.location=='about:blank'){window.location=u;}else{window.open(u,'');}})();">GMail</a> to your bookmark toolbar. Then click the link on your toolbar. Now, open a new tab, and click the link again.</p>
<p>This isn&#8217;t exactly what I asked for. It has no way of knowing if any blank tab exists, only if the current tab is blank. And, of course, it lacks the nice favicon support.</p>
<p>But it does the job. If you change the variable <code>u</code> to something other than &#8216;http://mail.google.com/mail&#8217;, you can make the link open any other page.</p>
<p>I love anonymous functions.</p>
<p><strong>Update:</strong> If you want a bookmark for something besides GMail, you can <a href="javascript:(function(){var u=prompt('Enter the URL to open in a blank tab.','http://');prompt('Copy the text below into a new bookmark.',&quot;javascript:(function(){var u='&quot;+u+&quot;';if(window.location=='about:blank'){window.location=u;}else{window.open(u,'');}})();&quot;);})();">create your own</a>. Or you can drag <em>this</em> link to your toolbar, to make new ones whenever you want: <a href="javascript:(function(){var u=prompt('Enter the URL to open in a blank tab.',window.location);prompt('Copy the text below into a new bookmark.',&quot;javascript:(function(){var u='&quot;+u+&quot;';if(window.location=='about:blank'){window.location=u;}else{window.open(u,'');}})();&quot;);})();">Open in Blank Tab</a>.</p>
<p><strong>Update 2:</strong> Oops, fixed the &#8220;create your own&#8221; link. Tested it, then accidentally pasted in the results, instead of the actual script.</p>
]]></content:encoded>
			<wfw:commentRss>http://coffeeonthekeyboard.com/firefox-open-in-blank-tab-197/feed/</wfw:commentRss>
		<slash:comments>1</slash:comments>
		</item>
		<item>
		<title>Private Variables in JavaScript</title>
		<link>http://coffeeonthekeyboard.com/private-variables-in-javascript-177/</link>
		<comments>http://coffeeonthekeyboard.com/private-variables-in-javascript-177/#comments</comments>
		<pubDate>Wed, 11 Feb 2009 03:28:00 +0000</pubDate>
		<dc:creator>James</dc:creator>
				<category><![CDATA[Articles]]></category>
		<category><![CDATA[client-side]]></category>
		<category><![CDATA[javascript]]></category>
		<category><![CDATA[objects]]></category>
		<category><![CDATA[programming]]></category>

		<guid isPermaLink="false">http://coffeeonthekeyboard.com/?p=177</guid>
		<description><![CDATA[Ok, enough of this social/ranting stuff. Time to write something vaguely technical. I have a love-hate relationship with JavaScript. I think anyone who works with it does. Sometimes it just doesn&#8217;t do what you expect, and it&#8217;s certainly different. One trick, especially for people from real Object-Oriented languages like Java, Ruby, or let&#8217;s even say [...]]]></description>
			<content:encoded><![CDATA[<p class="note">Ok, enough of this social/ranting stuff. Time to write something vaguely technical.</p>
<p>I have a love-hate relationship with <a href="http://www.quirksmode.org/dom/compatibility.html">JavaScript</a>. I think anyone who works with it does. Sometimes it just doesn&#8217;t do what you expect, and it&#8217;s certainly different.</p>
<p>One trick, especially for people from real Object-Oriented languages like Java, Ruby, or let&#8217;s even say PHP 5, is the lack of access control. When everything is an object, the inability to hide certain values can become a problem.<span id="more-177"></span></p>
<p>Say, for example, you create an object that represents a product you sell, and you want to use one of your part numbers to identify the object. Let&#8217;s say the format for your part number is 1234-56. You might do something like&#8230;</p>
<div class="dean_ch" style="white-space: wrap;">
<ol>
<li class="li1">
<div class="de1"><span class="kw2">function</span> Product <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">this</span>.<span class="me1">partNum</span>;</div>
</li>
<li class="li1">
<div class="de1">&nbsp; &nbsp; <span class="kw1">this</span>.<span class="me1">setPartNum</span> = <span class="kw2">function</span> <span class="br0">&#40;</span> num <span class="br0">&#41;</span> <span class="br0">&#123;</span></div>
</li>
<li class="li1">
<div class="de1">&nbsp; &nbsp; &nbsp; &nbsp; <span class="kw1">if</span> <span class="br0">&#40;</span> partNumRegex.<span class="me1">match</span><span class="br0">&#40;</span>num<span class="br0">&#41;</span> <span class="br0">&#41;</span> <span class="br0">&#123;</span></div>
</li>
<li class="li2">
<div class="de2">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span class="kw1">this</span>.<span class="me1">partNum</span> = num;</div>
</li>
<li class="li1">
<div class="de1">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span class="kw1">return</span> <span class="kw2">true</span>;</div>
</li>
<li class="li1">
<div class="de1">&nbsp; &nbsp; &nbsp; &nbsp; <span class="br0">&#125;</span> <span class="kw1">else</span> <span class="br0">&#123;</span></div>
</li>
<li class="li1">
<div class="de1">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span class="kw1">return</span> <span class="kw2">false</span>;</div>
</li>
<li class="li1">
<div class="de1">&nbsp; &nbsp; &nbsp; &nbsp; <span class="br0">&#125;</span></div>
</li>
<li class="li2">
<div class="de2">&nbsp; &nbsp; <span class="br0">&#125;</span></div>
</li>
<li class="li1">
<div class="de1"><span class="br0">&#125;</span></div>
</li>
</ol>
</div>
<p>You&#8217;re expecting the rest of your team to be respectful and use the <code>Product.setPartNum()</code> method, which should stop them from using an illegally formatted part number. (<code>partNumRegex</code> is left as an exercise to the reader.)</p>
<p>But what&#8217;s stopping them from doing this?</p>
<div class="dean_ch" style="white-space: wrap;">
<ol>
<li class="li1">
<div class="de1"><span class="kw2">var</span> jacket = <span class="kw2">new</span> Product;</div>
</li>
<li class="li1">
<div class="de1">jacket.<span class="me1">partNum</span> = <span class="st0">&#8217;1234&#8242;</span>;</div>
</li>
</ol>
</div>
<p>Now when they call your Ajax-driven <code>getProductData()</code> method, they&#8217;ll get an error. Had they used the <code>setPartNum()</code> method, they would&#8217;ve seen the error much earlier.</p>
<p>One trick to JavaScript is that variables are <a href="http://en.wikipedia.org/wiki/Scope_(programming)#Static_scoping_.28also_known_as_lexical_scoping.29">lexically scoped</a>. Basically, functions use the value of a variable where they are defined, not where they&#8217;re executed, for example (or just read <a href="http://blogs.msdn.com/kartikb/archive/2009/01/15/lexical-scoping.aspx">a better article</a>):</p>
<div class="dean_ch" style="white-space: wrap;">
<ol>
<li class="li1">
<div class="de1"><span class="kw2">var</span> a = <span class="nu0">1</span>;</div>
</li>
<li class="li1">
<div class="de1">&nbsp;</div>
</li>
<li class="li1">
<div class="de1"><span class="kw2">function</span> globalA<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="kw3">alert</span><span class="br0">&#40;</span>a<span class="br0">&#41;</span>; <span class="co1">//1</span></div>
</li>
<li class="li2">
<div class="de2"><span class="br0">&#125;</span></div>
</li>
<li class="li1">
<div class="de1">&nbsp;</div>
</li>
<li class="li1">
<div class="de1"><span class="kw2">function</span> localA <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="kw2">var</span> a = <span class="nu0">2</span>;</div>
</li>
<li class="li1">
<div class="de1">&nbsp; &nbsp; <span class="kw3">alert</span><span class="br0">&#40;</span>a<span class="br0">&#41;</span>; <span class="co1">//2</span></div>
</li>
<li class="li2">
<div class="de2"><span class="br0">&#125;</span></div>
</li>
</ol>
</div>
<p>So how does this help? Well, you can effectively hide values in a deeper scope. Instead of using the <code>this</code> keyword, just define new variables within your function:</p>
<div class="dean_ch" style="white-space: wrap;">
<ol>
<li class="li1">
<div class="de1"><span class="kw2">function</span> Product <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="kw2">var</span> partNum;</div>
</li>
<li class="li1">
<div class="de1">&nbsp; &nbsp; <span class="kw1">this</span>.<span class="me1">setPartNum</span> = <span class="kw2">function</span> <span class="br0">&#40;</span>num<span class="br0">&#41;</span> <span class="br0">&#123;</span></div>
</li>
<li class="li1">
<div class="de1">&nbsp; &nbsp; &nbsp; &nbsp; <span class="co1">// Fancy checking logic</span></div>
</li>
<li class="li2">
<div class="de2">&nbsp; &nbsp; <span class="br0">&#125;</span></div>
</li>
<li class="li1">
<div class="de1">&nbsp; &nbsp; <span class="kw1">this</span>.<span class="me1">getPartNum</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; &nbsp; &nbsp; <span class="kw1">return</span> partNum;</div>
</li>
<li class="li1">
<div class="de1">&nbsp; &nbsp; <span class="br0">&#125;</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> tshirt = <span class="kw2">new</span> Product;</div>
</li>
<li class="li1">
<div class="de1">tshirt.<span class="me1">partNum</span> = <span class="nu0">7</span>;</div>
</li>
<li class="li1">
<div class="de1">tshirt.<span class="me1">getPartNum</span><span class="br0">&#40;</span><span class="br0">&#41;</span>; <span class="co1">// null</span></div>
</li>
</ol>
</div>
<p>This same thing can apply to private methods. Don&#8217;t want the new guy calling <code>superSecretInternalMethod()</code>? One common practice is to start private methods with an underscore, but a better way is to actually hide the method:</p>
<div class="dean_ch" style="white-space: wrap;">
<ol>
<li class="li1">
<div class="de1"><span class="kw2">function</span> Product<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="kw2">var</span> partNum; <span class="co1">// private part number, use accessors</span></div>
</li>
<li class="li1">
<div class="de1">&nbsp; &nbsp; <span class="kw1">this</span>.<span class="me1">setPartNum</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; &nbsp; &nbsp; <span class="co1">// as above</span></div>
</li>
<li class="li2">
<div class="de2">&nbsp; &nbsp; <span class="br0">&#125;</span></div>
</li>
<li class="li1">
<div class="de1">&nbsp; &nbsp; <span class="kw1">this</span>.<span class="me1">getPartNum</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; &nbsp; &nbsp; <span class="kw1">return</span> partNum;</div>
</li>
<li class="li1">
<div class="de1">&nbsp; &nbsp; <span class="br0">&#125;</span></div>
</li>
<li class="li1">
<div class="de1">&nbsp;</div>
</li>
<li class="li2">
<div class="de2">&nbsp; &nbsp; <span class="kw2">function</span> superSecretInternalMethod<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; &nbsp; &nbsp; <span class="co1">// Whatever your coding-ninja heart desires</span></div>
</li>
<li class="li1">
<div class="de1">&nbsp; &nbsp; &nbsp; &nbsp; <span class="co1">// I can access partNum</span></div>
</li>
<li class="li1">
<div class="de1">&nbsp; &nbsp; <span class="br0">&#125;</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> truck = <span class="kw2">new</span> Product;</div>
</li>
<li class="li1">
<div class="de1">truck.<span class="me1">superSecretInternalMethod</span><span class="br0">&#40;</span><span class="br0">&#41;</span>; <span class="co1">// JS Error: truck.superSecretInternalMethod is not a function.</span></div>
</li>
</ol>
</div>
<p>We can even go a step farther and define private class methods and variables, but this post is already long enough, and it&#8217;s rare I have material for two in a row, so I&#8217;ll save that for next time.</p>
<p class="note">If you haven&#8217;t read it, go get <a href="http://www.amazon.com/JavaScript-Design-Patterns-Recipes-Problem-Solution/dp/159059908X">Pro JavaScript Design Patterns</a>. You won&#8217;t regret it.</p>
]]></content:encoded>
			<wfw:commentRss>http://coffeeonthekeyboard.com/private-variables-in-javascript-177/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Chrome Is Not A Browser</title>
		<link>http://coffeeonthekeyboard.com/chrome-is-not-a-browser-111/</link>
		<comments>http://coffeeonthekeyboard.com/chrome-is-not-a-browser-111/#comments</comments>
		<pubDate>Wed, 03 Sep 2008 15:34:53 +0000</pubDate>
		<dc:creator>James</dc:creator>
				<category><![CDATA[CSS]]></category>
		<category><![CDATA[Standards]]></category>
		<category><![CDATA[Browsers]]></category>
		<category><![CDATA[chrome]]></category>
		<category><![CDATA[google]]></category>
		<category><![CDATA[javascript]]></category>
		<category><![CDATA[Web 2.0]]></category>

		<guid isPermaLink="false">http://coffeeonthekeyboard.com/chrome-is-not-a-browser-111/</guid>
		<description><![CDATA[If you somehow haven&#8217;t heard of it, Google&#8217;s Chrome is a neat, quick, Acid2-compliant &#8220;browser&#8221; designed to work with web applications, not web pages. Chrome certainly looks like a modern browser, with tabs along the top and an address bar and a &#8220;Most visited&#8221; home screen, it will seem familiar to anyone who&#8217;s moved past [...]]]></description>
			<content:encoded><![CDATA[<p>If you <a href="http://search.twitter.com/search?q=chrome">somehow haven&#8217;t heard of it</a>, Google&#8217;s <a href="http://www.google.com/chrome">Chrome</a> is a neat, quick, Acid2-compliant &#8220;browser&#8221; designed to work with web <em>applications</em>, not web <em>pages</em>.</p>
<p>Chrome certainly looks like a modern browser, with tabs along the top and an address bar and a &#8220;Most visited&#8221; home screen, it will seem familiar to anyone who&#8217;s moved past Internet Explorer 6.</p>
<p>And yet, my Twittersphere has been full of comments like &#8220;Nice, but not nice enough to make me drop Firefox/Safari.&#8221;</p>
<p>While there are some visual improvements, such as an extremely small &#8220;chrome&#8221; (the parts of the browser <em>around</em> the page area) footprint, the big changes are &#8220;under the hood.&#8221; Chrome is built for tabs—each tab is an isolated process; no one tab can take down the whole browser—and is built for JavaScript-heavy &#8220;web 2.0&#8243; apps—Chrome&#8217;s new V8 JavaScript engine executes a full order of magnitude faster than the current browsers, in my experience.</p>
<p>And all of those &#8220;under the hood&#8221; changes are <strong>open source</strong>.</p>
<p>Chrome is not a browser.</p>
<p>Chrome is Google&#8217;s way of making a point: modern web browsers have <a href="http://www.youtube.com/watch?v=7RSHMheDIrM">not kept up with the web</a> itself.</p>
<p>More and more, the web is becoming an interactive application, and most browsers are not built for it. They display pages, and running applications is an afterthought. While we&#8217;ve seen huge improvements in JavaScript execution in the past few years, speed is still a limitation for developers. Applications are also much more likely to crash than static pages (go ahead, just try to crash a browser with just malformed HTML) and isolating tabs will give necessary boosts to speed, stability, and security.</p>
<p><a href="http://krisabel.ctv.ca/blog/_archives/2008/9/2/3866151.html">Kris Abel</a> of <a href="http://krisabel.ctv.ca/">CTV.ca</a> said it best: &#8220;Google’s entire business takes place throughout the internet itself and so they see their interests served regardless of which company takes web browsing to the next level, in fact they see their interests served if all companies do exactly that.&#8221;</p>
<p>I&#8217;m not switching to Chrome. I doubt very many people will find it useful as a primary browser. I don&#8217;t expect many user-interface improvements, like Firefox&#8217;s vast add-on library or the accessibility features of Firefox 3, Opera or IE8.</p>
<p>I do expect any future version to have more &#8220;under the hood&#8221; improvements, and I hope that the makers of Firefox, Opera, Internet Explorer, and any new browsers that spring from this, will re-evaluate their own products and move in this direction.</p>
<p>Because when the browsers get better, the web gets better.</p>
]]></content:encoded>
			<wfw:commentRss>http://coffeeonthekeyboard.com/chrome-is-not-a-browser-111/feed/</wfw:commentRss>
		<slash:comments>3</slash:comments>
		</item>
	</channel>
</rss>

