<?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>I does Javascript! &#187; General</title>
	<atom:link href="http://blog.stchur.com/category/general/feed/" rel="self" type="application/rss+xml" />
	<link>http://blog.stchur.com</link>
	<description>If you don&#039;t expect too much from me, you might not be let down.</description>
	<lastBuildDate>Tue, 03 Jan 2012 16:01:10 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.3</generator>
		<item>
		<title>Dealing with CSS Vendor Prefixes</title>
		<link>http://blog.stchur.com/2011/11/07/dealing-with-css-vendor-prefixes/</link>
		<comments>http://blog.stchur.com/2011/11/07/dealing-with-css-vendor-prefixes/#comments</comments>
		<pubDate>Tue, 08 Nov 2011 06:03:56 +0000</pubDate>
		<dc:creator>sstchur</dc:creator>
				<category><![CDATA[General]]></category>

		<guid isPermaLink="false">http://blog.stchur.com/?p=194</guid>
		<description><![CDATA[The IE team recently posted what they're calling a best practice for dealing with vendor prefixes in modern browsers. After reading it, I thought I would post the solution used in Gimme, which I implemented as soon as I had to start dealing with vendor prefixes. I've yanked it out and posted it here as [...]]]></description>
			<content:encoded><![CDATA[	<p>The IE team recently posted what they're calling <a href="http://blogs.msdn.com/b/ie/archive/2011/10/28/a-best-practice-for-programming-with-vendor-prefixes.aspx" title="A Best Practice for Programming with Vendor Prefixes">a best practice for dealing with vendor prefixes in modern browsers</a>.</p>
	<p>After reading it, I thought I would post the solution used in Gimme, which I implemented as soon as I had to start dealing with vendor prefixes.</p>
	<p>I've yanked it out and posted it here as a stand-alone function called getStyleName().  You can use it when you want to set some element's style in Javascript.  You always pass it the "standard" name (without any prefix) and it will do (or try to do) the right thing in the right browser.</p>
	<p>For instance:</p>
	<div class="dean_ch" style="white-space: nowrap;"><span class="kw2">var</span> someElem = document.<span class="me1">getElementById</span><span class="br0">&#40;</span><span class="st0">'foo'</span><span class="br0">&#41;</span>;</p>
	<p><span class="co1">// MozTransform in Mozilla, webkitTransform in webkit, etc...</span><br />
someElem.<span class="me1">style</span><span class="br0">&#91;</span>getStyleName<span class="br0">&#40;</span><span class="st0">'transform'</span><span class="br0">&#41;</span><span class="br0">&#93;</span> = <span class="st0">'rotate(90deg)'</span>;</div>
	<p>Here is the getStyleName function in full:</p>
	<div class="dean_ch" style="white-space: nowrap;"><span class="kw2">var</span> getStyleName = <span class="kw2">function</span><span class="br0">&#40;</span><span class="br0">&#41;</span><br />
<span class="br0">&#123;</span><br />
&nbsp; &nbsp;<span class="kw2">var</span> styleHash = <span class="br0">&#123;</span><span class="br0">&#125;</span>;<br />
&nbsp; &nbsp;<span class="kw2">var</span> vendorPrefixes = <span class="br0">&#91;</span> <span class="st0">'O'</span>, <span class="st0">'ms'</span>, <span class="st0">'Moz'</span>, <span class="st0">'Webkit'</span> <span class="br0">&#93;</span>;</p>
	<p>&nbsp; &nbsp;<span class="kw1">return</span> <span class="kw2">function</span><span class="br0">&#40;</span>styleName, elem<span class="br0">&#41;</span><br />
&nbsp; &nbsp;<span class="br0">&#123;</span><br />
&nbsp; &nbsp;&nbsp; &nbsp;elem = elem || document.<span class="me1">createElement</span><span class="br0">&#40;</span><span class="st0">'div'</span><span class="br0">&#41;</span>;<br />
&nbsp; &nbsp;&nbsp; &nbsp;<span class="kw2">var</span> s = styleHash<span class="br0">&#91;</span>styleName<span class="br0">&#93;</span>;<br />
&nbsp; &nbsp;&nbsp; &nbsp;<span class="kw1">if</span> <span class="br0">&#40;</span>!s<span class="br0">&#41;</span><br />
&nbsp; &nbsp;&nbsp; &nbsp;<span class="br0">&#123;</span>&nbsp; &nbsp; &nbsp;&nbsp; &nbsp;<br />
&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp;<span class="co1">// hope the browser supports the &quot;official&quot; (non-vendor-prefixed) style first</span><br />
&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp;<span class="kw2">var</span> camStyle = camelize<span class="br0">&#40;</span>styleName<span class="br0">&#41;</span>;<br />
&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp;<br />
&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp;<span class="co1">// if not, then ask getVendorStyle for help -- it will try the various vendor prefixes seeking</span><br />
&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp;<span class="co1">// one that works (if it doesn't find one, it will return an empty string, in which case we</span><br />
&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp;<span class="co1">// revert back to originally passed in styleName)</span><br />
&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp;<span class="kw1">if</span> <span class="br0">&#40;</span>elem.<span class="me1">style</span><span class="br0">&#91;</span>camStyle<span class="br0">&#93;</span> === undefined<span class="br0">&#41;</span><br />
&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp;<span class="br0">&#123;</span><br />
&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp;camStyle = getVendorStyle<span class="br0">&#40;</span>camStyle, elem<span class="br0">&#41;</span> || styleName;<br />
&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp;<span class="br0">&#125;</span></p>
	<p>&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp;<span class="co1">// finally, store the camelized &amp; vendor-specific (if applicable) value in the styleHash where key = styleName that was passed in</span><br />
&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp;s = styleHash<span class="br0">&#91;</span>styleName<span class="br0">&#93;</span> = camStyle;<br />
&nbsp; &nbsp;&nbsp; &nbsp;<span class="br0">&#125;</span><br />
&nbsp; &nbsp;&nbsp; &nbsp;<span class="kw1">return</span> s;<br />
&nbsp; &nbsp;<span class="br0">&#125;</span></p>
	<p>&nbsp; &nbsp;<span class="kw2">function</span> getVendorStyle<span class="br0">&#40;</span>style, elem<span class="br0">&#41;</span><br />
&nbsp; &nbsp;<span class="br0">&#123;</span><br />
&nbsp; &nbsp;&nbsp; &nbsp;<span class="kw2">var</span> s = camelize<span class="br0">&#40;</span><span class="st0">'-'</span> + style<span class="br0">&#41;</span>;<br />
&nbsp; &nbsp;&nbsp; &nbsp;<span class="kw2">var</span> i = vendorPrefixes.<span class="me1">length</span>;<br />
&nbsp; &nbsp;&nbsp; &nbsp;<span class="kw1">while</span> <span class="br0">&#40;</span>i--<span class="br0">&#41;</span><br />
&nbsp; &nbsp;&nbsp; &nbsp;<span class="br0">&#123;</span><br />
&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp;style = vendorPrefixes<span class="br0">&#91;</span>i<span class="br0">&#93;</span> + s;<br />
&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp;<span class="kw1">if</span> <span class="br0">&#40;</span>elem.<span class="me1">style</span><span class="br0">&#91;</span>style<span class="br0">&#93;</span> !== undefined<span class="br0">&#41;</span><br />
&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp;<span class="br0">&#123;</span><br />
&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp;<span class="kw1">return</span> style;<br />
&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp;<span class="br0">&#125;</span><br />
&nbsp; &nbsp;&nbsp; &nbsp;<span class="br0">&#125;</span><br />
&nbsp; &nbsp;&nbsp; &nbsp;<span class="kw1">return</span> <span class="st0">''</span>;<br />
&nbsp; &nbsp;<span class="br0">&#125;</span>;</p>
	<p>&nbsp; &nbsp;<span class="kw2">function</span> camelize<span class="br0">&#40;</span>s<span class="br0">&#41;</span><br />
&nbsp; &nbsp;<span class="br0">&#123;</span><br />
&nbsp; &nbsp;&nbsp; &nbsp;<span class="kw1">return</span> s.<span class="me1">replace</span><span class="br0">&#40;</span><span class="re0">/-<span class="br0">&#40;</span>\w<span class="br0">&#41;</span>/g</span>, <span class="kw2">function</span><span class="br0">&#40;</span>m, c<span class="br0">&#41;</span><br />
&nbsp; &nbsp;&nbsp; &nbsp;<span class="br0">&#123;</span><br />
&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp;<span class="kw1">return</span> c.<span class="me1">toUpperCase</span><span class="br0">&#40;</span><span class="br0">&#41;</span>;<br />
&nbsp; &nbsp;&nbsp; &nbsp;<span class="br0">&#125;</span><span class="br0">&#41;</span>;<br />
&nbsp; &nbsp;<span class="br0">&#125;</span><br />
<span class="br0">&#125;</span><span class="br0">&#40;</span><span class="br0">&#41;</span>;</div>
]]></content:encoded>
			<wfw:commentRss>http://blog.stchur.com/2011/11/07/dealing-with-css-vendor-prefixes/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>True private variables in Javascript with prototype</title>
		<link>http://blog.stchur.com/2011/09/26/true-private-variables-in-javascript-with-prototype/</link>
		<comments>http://blog.stchur.com/2011/09/26/true-private-variables-in-javascript-with-prototype/#comments</comments>
		<pubDate>Tue, 27 Sep 2011 05:09:49 +0000</pubDate>
		<dc:creator>sstchur</dc:creator>
				<category><![CDATA[General]]></category>

		<guid isPermaLink="false">http://blog.stchur.com/?p=179</guid>
		<description><![CDATA[Most folks know how to mimic public, private, and "privileged" variables in Javascript, but I think at some point, when getting familiar with prototypal inheritance, most scripters wonder if there is a way to access the private members (the ones created in a closure) of an instance within a function defined using prototype. There is [...]]]></description>
			<content:encoded><![CDATA[	<p>Most folks know how to mimic public, private, and "privileged" variables in Javascript, but I think at some point, when getting familiar with prototypal inheritance, most scripters wonder if there is a way to access the private members (the ones created in a closure) of an instance within a function defined using prototype.</p>
	<p>There is really no good way to do this.  Here's a simple prototype example:</p>
	<div class="dean_ch" style="white-space: nowrap;"><span class="kw2">function</span> Cat<span class="br0">&#40;</span><span class="kw3">name</span>, age<span class="br0">&#41;</span><br />
<span class="br0">&#123;</span><br />
&nbsp; &nbsp;<span class="kw1">this</span>.<span class="kw3">name</span> = <span class="kw3">name</span>;<br />
&nbsp; &nbsp;<span class="kw1">this</span>.<span class="me1">age</span> = age;<br />
<span class="br0">&#125;</span><br />
Cat.<span class="me1">prototype</span>.<span class="me1">introduce</span> = <span class="kw2">function</span><span class="br0">&#40;</span><span class="br0">&#41;</span><br />
<span class="br0">&#123;</span><br />
&nbsp; &nbsp;<span class="kw3">alert</span><span class="br0">&#40;</span><span class="st0">'I am a cat. My name is '</span> + <span class="kw1">this</span>.<span class="kw3">name</span> + <span class="st0">', and I am '</span> + <span class="kw1">this</span>.<span class="me1">age</span> + <span class="st0">' year(s) old.'</span>;<br />
<span class="br0">&#125;</span>;</p>
	<p><span class="kw2">var</span> c = <span class="kw2">new</span> Cat<span class="br0">&#40;</span><span class="st0">'Garfield'</span>, <span class="nu0">7</span><span class="br0">&#41;</span>;<br />
c.<span class="me1">introduce</span><span class="br0">&#40;</span><span class="br0">&#41;</span>;</div>
	<p>The <code>introduce</code> function was defined with <code>.prototype</code> so the <code>this</code> keyword in that context refers to the Cat instance in question.  Using it, we can get at the <code>.name</code> and <code>.age</code> properties. But this also means that the <code>introduce</code> function isn't the only thing that has access to those properties.  In fact, the whole world has access.  They are, essentially, public properties.</p>
	<div class="dean_ch" style="white-space: nowrap;"><span class="kw3">alert</span><span class="br0">&#40;</span>c.<span class="kw3">name</span><span class="br0">&#41;</span>;&nbsp;&nbsp; &nbsp;<span class="co1">// directly accessible</span><br />
<span class="kw3">alert</span><span class="br0">&#40;</span>c.<span class="me1">age</span><span class="br0">&#41;</span>;&nbsp; &nbsp; &nbsp;<span class="co1">// directly accessible</span><br />
&nbsp;</div>
	<p>What if you don't want that?  What if you want to take the name that is passed into the constructor, modify it in some way (let's say, convert it to uppercase) and then offer read only access to it?</p>
	<div class="dean_ch" style="white-space: nowrap;"><span class="kw2">function</span> Cat<span class="br0">&#40;</span><span class="kw3">name</span>, age<span class="br0">&#41;</span><br />
<span class="br0">&#123;</span><br />
&nbsp; &nbsp;<span class="kw1">this</span>.<span class="kw3">name</span> = <span class="kw3">name</span>.<span class="me1">toUpperCase</span><span class="br0">&#40;</span><span class="br0">&#41;</span>;<br />
<span class="br0">&#125;</span><br />
Cat.<span class="me1">prototype</span>.<span class="me1">getName</span> = <span class="kw2">function</span><span class="br0">&#40;</span><span class="br0">&#41;</span><br />
<span class="br0">&#123;</span><br />
&nbsp; &nbsp;<span class="kw1">return</span> <span class="kw1">this</span>.<span class="kw3">name</span>;<br />
<span class="br0">&#125;</span>;</p>
	<p><span class="kw2">var</span> c = <span class="kw2">new</span> Cat<span class="br0">&#40;</span><span class="st0">'Garfield'</span>, <span class="nu0">7</span><span class="br0">&#41;</span>;<br />
<span class="kw3">alert</span><span class="br0">&#40;</span>c.<span class="me1">getName</span><span class="br0">&#40;</span><span class="br0">&#41;</span><span class="br0">&#41;</span>;&nbsp; &nbsp; &nbsp;<span class="co1">// GARFIELD</span><br />
c.<span class="kw3">name</span> = <span class="st0">'STEPHEN'</span>;&nbsp; &nbsp; &nbsp;<span class="co1">// oops, should this be allowed?</span><br />
<span class="kw3">alert</span><span class="br0">&#40;</span>c.<span class="me1">getName</span><span class="br0">&#40;</span><span class="br0">&#41;</span><span class="br0">&#41;</span>;&nbsp; &nbsp; &nbsp;<span class="co1">// STEPHEN, clearly not the kind of encapsulation we're going for</span><br />
&nbsp;</div>
	<p>So how can we allow <code>getName</code> to have access to <code>.name</code>, but at the same time disallow access to <code>.name</code> outside of prototype defined functions?  Well, as I said, you really can't.  I mean, there's no <strong>good</strong> way.  But there is a sort-of way.</p>
	<div class = "note"><strong>DISCLAIMER: I am NOT recommending that you actually do what I'm about to demonstrate.  At the same time, I am not telling you NOT to do it either.  From a performance perspective, this is completely unvalidated (at least by me).  So if you want to do something like this, best do your homework and figure out if it will offer you an acceptable level of performance -- I am not attempting to answer that for you.</strong></div>
	<p>Alright.  With the disclaimer out of the way, here we go:</p>
	<div class="dean_ch" style="white-space: nowrap;"><span class="kw2">var</span> Cat = <span class="kw2">function</span><span class="br0">&#40;</span><span class="br0">&#41;</span><br />
<span class="br0">&#123;</span><br />
&nbsp; &nbsp;<span class="kw2">var</span> guid = <span class="nu0">0</span>;<br />
&nbsp; &nbsp;<span class="kw2">var</span> privates = <span class="br0">&#123;</span><span class="br0">&#125;</span>;</p>
	<p>&nbsp; &nbsp;<span class="kw2">function</span> cat<span class="br0">&#40;</span><span class="kw3">name</span>, age<span class="br0">&#41;</span><br />
&nbsp; &nbsp;<span class="br0">&#123;</span><br />
&nbsp; &nbsp;&nbsp; &nbsp;<span class="kw1">this</span>._$guid = guid++;<br />
&nbsp; &nbsp;&nbsp; &nbsp;privates<span class="br0">&#91;</span><span class="kw1">this</span>._$guid<span class="br0">&#93;</span> =<br />
&nbsp; &nbsp;&nbsp; &nbsp;<span class="br0">&#123;</span><br />
&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp;<span class="kw3">name</span>: <span class="kw3">name</span>,<br />
&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp;age: age<br />
&nbsp; &nbsp;&nbsp; &nbsp;<span class="br0">&#125;</span>;<br />
&nbsp; &nbsp;<span class="br0">&#125;</span><br />
&nbsp; &nbsp;cat.<span class="me1">prototype</span>.<span class="me1">getName</span> = <span class="kw2">function</span><span class="br0">&#40;</span><span class="br0">&#41;</span><br />
&nbsp; &nbsp;<span class="br0">&#123;</span><br />
&nbsp; &nbsp;&nbsp; &nbsp;<span class="kw1">return</span> privates<span class="br0">&#91;</span><span class="kw1">this</span>._$guid<span class="br0">&#93;</span>.<span class="kw3">name</span>;<br />
&nbsp; &nbsp;<span class="br0">&#125;</span>;<br />
&nbsp; &nbsp;cat.<span class="me1">prototype</span>.<span class="me1">getAge</span> = <span class="kw2">function</span><span class="br0">&#40;</span><span class="br0">&#41;</span><br />
&nbsp; &nbsp;<span class="br0">&#123;</span><br />
&nbsp; &nbsp;&nbsp; &nbsp;<span class="kw1">return</span> privates<span class="br0">&#91;</span><span class="kw1">this</span>._$guid<span class="br0">&#93;</span>.<span class="me1">age</span>;<br />
&nbsp; &nbsp;<span class="br0">&#125;</span>;</p>
	<p>&nbsp; &nbsp;<span class="kw1">return</span> cat;<br />
<span class="br0">&#125;</span><span class="br0">&#40;</span><span class="br0">&#41;</span>;</p>
	<p><span class="kw2">var</span> cat1 = <span class="kw2">new</span> Cat<span class="br0">&#40;</span><span class="st0">'Garfield'</span>, <span class="nu0">7</span><span class="br0">&#41;</span>;<br />
<span class="kw2">var</span> cat2 = <span class="kw2">new</span> Cat<span class="br0">&#40;</span><span class="st0">'Fluffy'</span>, <span class="nu0">13</span><span class="br0">&#41;</span>;</p>
	<p><span class="kw3">alert</span><span class="br0">&#40;</span>cat1.<span class="me1">getName</span> === cat2.<span class="me1">getName</span><span class="br0">&#41;</span>;&nbsp; &nbsp; &nbsp;<span class="co1">// true</span><br />
<span class="kw3">alert</span><span class="br0">&#40;</span>cat1.<span class="kw3">name</span><span class="br0">&#41;</span>;&nbsp;&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp;<span class="co1">// oops, no dice (which is good)</span><br />
<span class="kw3">alert</span><span class="br0">&#40;</span>cat1.<span class="me1">getName</span><span class="br0">&#40;</span><span class="br0">&#41;</span><span class="br0">&#41;</span>;&nbsp; &nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp;<span class="co1">// Garfield</span><br />
<span class="kw3">alert</span><span class="br0">&#40;</span>cat2.<span class="me1">age</span><span class="br0">&#41;</span>;&nbsp; &nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp;<span class="co1">// oops, no dice</span><br />
<span class="kw3">alert</span><span class="br0">&#40;</span>cat2.<span class="me1">getAge</span><span class="br0">&#40;</span><span class="br0">&#41;</span><span class="br0">&#41;</span>;&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp;<span class="co1">// 13</span><br />
&nbsp;</div>
	<p>First, I alert cat1.getName === cat2.getName to illustrate that prototype really is being used here.  The instances cat1 and cat2 share the implementation of the getName function (they don't each get their own -- something that would have been true had we used the closure model exclusively).</p>
	<p>Second, you'll notice that I do not have direct access to <code>.name</code> or <code>.age</code>.  So this seems to do what we want.</p>
	<p>Now, more alert readers will no doubt have noticed that I cheated.  I'm still using the <code>this</code> keyword from within my prototype defined functions, <code>getName</code> and <code>getAge</code>.  This is so that I can access the instances GUID (<code>._$guid</code>).  The GUID is, of course, nothing more than counter, but it serves as a way to track the created instances.  Using it, we can get access to property bag in the <code>privates</code> hash that "belongs to" the particular Cat instance we're interested in.</p>
	<p>But there is a flaw here, of course.  The <code>._$guid</code>, despite its marginally cryptic name, is completely unprotected.  That is, the whole world has access to it.  So one could really hork things badly if he decided to do something like:</p>
	<div class="dean_ch" style="white-space: nowrap;"><span class="kw2">var</span> garfield = <span class="kw2">new</span> Cat<span class="br0">&#40;</span><span class="st0">'Garfield'</span>, <span class="nu0">7</span><span class="br0">&#41;</span>;<br />
<span class="kw2">var</span> fluffy = <span class="kw2">new</span> Cat<span class="br0">&#40;</span><span class="st0">'Fluffy'</span>, <span class="nu0">10</span><span class="br0">&#41;</span>;</p>
	<p>fluffy._$guid = <span class="nu0">0</span>;<br />
<span class="kw3">alert</span><span class="br0">&#40;</span>fluffy.<span class="me1">getName</span><span class="br0">&#40;</span><span class="br0">&#41;</span><span class="br0">&#41;</span>;&nbsp; &nbsp;&nbsp; &nbsp;<span class="co1">// Garfield, oops! &nbsp;this is NOT good</span></div>
	<p>Now, in this example, I chose a valid GUID (that of Garfield), but I could have just as easily chosen an invalid GUID, which would have caused a script error. Of course, the script error is easily preventable by ensuring that the <code>._$guid</code> actually exists as a key in the <code>privates</code> hash before attempting to access it, but this does nothing to ensure the integrity of the data, and that's a problem.</p>
	<p>Turns out though, that we can add a bit more code.  A bit of code that involves a check prior to accessing data from the <code>privates</code> hash to ensure that the instance's <code>_$guid</code> hasn't been tampered with.  If it has, we throw an exception and refuse to go any further.</p>
	<p>Here's the revision:</p>
	<div class="dean_ch" style="white-space: nowrap;"><span class="kw2">var</span> Cat = <span class="kw2">function</span><span class="br0">&#40;</span><span class="br0">&#41;</span><br />
<span class="br0">&#123;</span><br />
&nbsp; &nbsp;<span class="kw2">var</span> guid = <span class="nu0">0</span>;<br />
&nbsp; &nbsp;<span class="kw2">var</span> privates = <span class="br0">&#123;</span><span class="br0">&#125;</span>;<br />
&nbsp; &nbsp;<span class="kw2">var</span> check = <span class="br0">&#123;</span><span class="br0">&#125;</span>;</p>
	<p>&nbsp; &nbsp;<span class="kw2">function</span> cat<span class="br0">&#40;</span><span class="kw3">name</span>, age<span class="br0">&#41;</span><br />
&nbsp; &nbsp;<span class="br0">&#123;</span><br />
&nbsp; &nbsp;&nbsp; &nbsp;<span class="kw1">this</span>._$guid = guid++;<br />
&nbsp; &nbsp;&nbsp; &nbsp;check<span class="br0">&#91;</span><span class="kw1">this</span>._$guid<span class="br0">&#93;</span> = <span class="kw1">this</span>;<br />
&nbsp; &nbsp;&nbsp; &nbsp;privates<span class="br0">&#91;</span><span class="kw1">this</span>._$guid<span class="br0">&#93;</span> =<br />
&nbsp; &nbsp;&nbsp; &nbsp;<span class="br0">&#123;</span><br />
&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp;<span class="kw3">name</span>: <span class="kw3">name</span>,<br />
&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp;age: age<br />
&nbsp; &nbsp;&nbsp; &nbsp;<span class="br0">&#125;</span>;<br />
&nbsp; &nbsp;&nbsp; &nbsp;<br />
&nbsp; &nbsp;<span class="br0">&#125;</span><br />
&nbsp; &nbsp;cat.<span class="me1">prototype</span>.<span class="me1">getName</span> = <span class="kw2">function</span><span class="br0">&#40;</span><span class="br0">&#41;</span><br />
&nbsp; &nbsp;<span class="br0">&#123;</span><br />
&nbsp; &nbsp;&nbsp; &nbsp;<span class="kw1">if</span> <span class="br0">&#40;</span>check<span class="br0">&#91;</span><span class="kw1">this</span>._$guid<span class="br0">&#93;</span> !== <span class="kw1">this</span><span class="br0">&#41;</span><br />
&nbsp; &nbsp;&nbsp; &nbsp;<span class="br0">&#123;</span><br />
&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp;<span class="kw1">throw</span> <span class="st0">'Oops, looks like this instances has been tampered with.'</span>;<br />
&nbsp; &nbsp;&nbsp; &nbsp;<span class="br0">&#125;</span><br />
&nbsp; &nbsp;&nbsp; &nbsp;<span class="kw1">return</span> privates<span class="br0">&#91;</span><span class="kw1">this</span>._$guid<span class="br0">&#93;</span>.<span class="kw3">name</span>;<br />
&nbsp; &nbsp;<span class="br0">&#125;</span>;<br />
&nbsp; &nbsp;cat.<span class="me1">prototype</span>.<span class="me1">getAge</span> = <span class="kw2">function</span><span class="br0">&#40;</span><span class="br0">&#41;</span><br />
&nbsp; &nbsp;<span class="br0">&#123;</span><br />
&nbsp; &nbsp;&nbsp; &nbsp;<span class="kw1">if</span> <span class="br0">&#40;</span>check<span class="br0">&#91;</span><span class="kw1">this</span>._$guid<span class="br0">&#93;</span> !== <span class="kw1">this</span><span class="br0">&#41;</span><br />
&nbsp; &nbsp;&nbsp; &nbsp;<span class="br0">&#123;</span><br />
&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp;<span class="kw1">throw</span> <span class="st0">'Oops, looks like this instances has been tampered with.'</span>;<br />
&nbsp; &nbsp;&nbsp; &nbsp;<span class="br0">&#125;</span><br />
&nbsp; &nbsp;&nbsp; &nbsp;<span class="kw1">return</span> privates<span class="br0">&#91;</span><span class="kw1">this</span>._$guid<span class="br0">&#93;</span>.<span class="me1">age</span>;<br />
&nbsp; &nbsp;<span class="br0">&#125;</span>;</p>
	<p>&nbsp; &nbsp;<span class="kw1">return</span> cat;<br />
<span class="br0">&#125;</span><span class="br0">&#40;</span><span class="br0">&#41;</span>;</p>
	<p>
<span class="kw2">var</span> fluffy = <span class="kw2">new</span> Cat<span class="br0">&#40;</span><span class="st0">'Fluffy'</span>, <span class="nu0">7</span><span class="br0">&#41;</span>;<br />
<span class="kw2">var</span> snowball = <span class="kw2">new</span> Cat<span class="br0">&#40;</span><span class="st0">'Snowball'</span>, <span class="nu0">12</span><span class="br0">&#41;</span>;<br />
<span class="kw2">var</span> garfield = <span class="kw2">new</span> Cat<span class="br0">&#40;</span><span class="st0">'Garfield'</span>, <span class="nu0">9</span><span class="br0">&#41;</span>;</p>
	<p><span class="kw3">alert</span><span class="br0">&#40;</span>fluffy.<span class="me1">getName</span><span class="br0">&#40;</span><span class="br0">&#41;</span><span class="br0">&#41;</span>;&nbsp; &nbsp;&nbsp; &nbsp;<span class="co1">// Fluffy</span><br />
<span class="kw3">alert</span><span class="br0">&#40;</span>snowball.<span class="me1">getName</span><span class="br0">&#40;</span><span class="br0">&#41;</span><span class="br0">&#41;</span>;&nbsp;&nbsp; &nbsp;<span class="co1">// Snowball</span><br />
<span class="kw3">alert</span><span class="br0">&#40;</span>garfield.<span class="me1">getName</span><span class="br0">&#40;</span><span class="br0">&#41;</span><span class="br0">&#41;</span>;&nbsp;&nbsp; &nbsp;<span class="co1">// Garfield</span><br />
fluffy._$guid = <span class="nu0">2</span>;<br />
<span class="kw3">alert</span><span class="br0">&#40;</span>fluffy.<span class="me1">getName</span><span class="br0">&#40;</span><span class="br0">&#41;</span><span class="br0">&#41;</span>;&nbsp; &nbsp;&nbsp; &nbsp;<span class="co1">// Throws an exception, b/c fluffy's _$guid was modified</span></div>
	<p>I even created a nice illustration to help you visualize this <img src='http://blog.stchur.com/wp-includes/images/smilies/icon_smile.gif' alt=':-)' class='wp-smiley' /> </p>
	<p><a href="http://blog.stchur.com/wp-content/uploads/2011/09/cats.jpg"><img src="http://blog.stchur.com/wp-content/uploads/2011/09/cats-300x225.jpg" alt="Visualization of private variables using prototype in Javascript" title="Visualization of private variables using prototype in Javascript" width="300" height="225" class="alignnone size-medium wp-image-187" /></a></p>
	<p>As you can plainly see, the keys in the <code>privates</code> hash correspond to the <code>._$guid</code> property of Cat instance.  In other words, if you match up an instance's <code>._$guid</code> with a key in the <code>privates</code> hash, you'll have access to that instance's collection of member variables.</p>
	<p>You'll also notice that the keys in the <code>check</code> hash match the keys in the <code>privates</code> hash.  However, they don't point to a property bag, they point to an actual Cat instance.  In the <code>getName</code> function, we have access to the instance (through the use of the <code>this</code> keyword), and we can use that to perform a check.  If an instance's public <code>._$guid</code> has been tampered with, then looking in the <code>check</code> hash for that <code>$_guid</code> will result in accessing a Cat instance that isn't equal (identical) to <code>this</code>.  In that case, we can throw an exception.  Otherwise, we go ahead and access and entry from the <code>privates</code> hash, and this gives us access to the current Cat's members.</p>
	<p>Before closing, I want to once again make clear that I am most definitely NOT recommending that you use this model, but neither am I discouraging the use of it (I'm a little bit annoying that way, I know).  It's simply untested in terms of memory and speed performance.  Whether or not this yields anything that would constitute acceptable performance, I do not know and have not attempted to determine.  I imagine there could be some scenarios in which it is probably okay.  And I imagine there are other scenarios in which it might be a nightmare, but I'm only guessing on both counts.</p>
	<p>If you decide to experiment with this, definitely report back here with your findings for all to see.</p>
	<p>Happy scripting!
</p>
]]></content:encoded>
			<wfw:commentRss>http://blog.stchur.com/2011/09/26/true-private-variables-in-javascript-with-prototype/feed/</wfw:commentRss>
		<slash:comments>2</slash:comments>
		</item>
		<item>
		<title>Dependency Injection / Dependency Management (part 1)</title>
		<link>http://blog.stchur.com/2011/08/05/dependency-injection-dependency-management-part-1/</link>
		<comments>http://blog.stchur.com/2011/08/05/dependency-injection-dependency-management-part-1/#comments</comments>
		<pubDate>Sat, 06 Aug 2011 01:19:31 +0000</pubDate>
		<dc:creator>sstchur</dc:creator>
				<category><![CDATA[General]]></category>

		<guid isPermaLink="false">http://blog.stchur.com/?p=151</guid>
		<description><![CDATA[I'm not an expert when it comes to dependency injection, but I do know a little. My team uses Unity in one of its projects, so I have some exposure. Recently, I started work on a project (hoping to eventually release it as OSS) on a Dependency Injection (like) / Dependency Management framework for the [...]]]></description>
			<content:encoded><![CDATA[	<p>I'm not an expert when it comes to dependency injection, but I do know a little.  My team uses <a href = "http://unity.codeplex.com/" title = "Unity">Unity</a> in one of its projects, so I have some exposure.</p>
	<p>Recently, I started work on a project (hoping to eventually release it as OSS) on a Dependency Injection (like) / Dependency Management framework for the client (that is, written in Javascript).</p>
	<p>Since it was (is) a fun project for me, I thought I might blog about some of the more interesting parts of what I've done.</p>
	<p>Dependency Resolution:<br />
One of the key parts of Dependency Injection is dependency resolution.  That is, if I need an instance of something, say, a Foo, and if creating a Foo requires an instance of say, a Bar, then my DI framework ought to resolve the Bar for me on its way towards creating my Foo.</p>
	<div class="dean_ch" style="white-space: nowrap;"><span class="kw2">var</span> registration =<br />
<span class="br0">&#123;</span><br />
&nbsp; &nbsp;Foo: <span class="br0">&#123;</span> className: <span class="st0">'Stephen.Stchur.Foo'</span>, params: <span class="br0">&#91;</span> <span class="st0">'Bar'</span> <span class="br0">&#93;</span> <span class="br0">&#125;</span>,<br />
&nbsp; &nbsp;Bar: <span class="br0">&#123;</span> className: <span class="st0">'Stephen.Stchur.Bar'</span> <span class="br0">&#125;</span><br />
<span class="br0">&#125;</span></p>
	<p><span class="co1">// obviously, code for Container, not show here (yet)</span><br />
<span class="kw2">var</span> c = <span class="kw2">new</span> Container<span class="br0">&#40;</span>registration<span class="br0">&#41;</span>;<br />
<span class="kw2">var</span> f = c.<span class="me1">instance</span><span class="br0">&#40;</span><span class="st0">'Foo'</span><span class="br0">&#41;</span>;&nbsp;<span class="co1">// container &quot;knows&quot; Foo needs Bar; it takes care of it for us</span></div>
	<p>So what I'm focusing on in this blog post is the comment above: "container 'knows' Foo need Bar; it takes care of it for us.</p>
	<p>In a future post, I'll be talking more about the management part (involving not only resolving types on demand, but also downloading dependencies and groups of dependencies prior to resolution).</p>
	<p>But for now, let's just assume that all dependencies needed to create anything/everything have already been preloaded.</p>
	<p>So how does the container know that Foo needs a Bar?  Well, it's not too hard to figure out if you look at the registration.  It's just a dictionary of key/value pairs, where the value is a simple object literal containing a couple properties:</p>
	<ol>
	<li>className - required, the fully qualified name of the thing that the container will eventually be creating instances of</li>
	<li>params - optional, an array of string keys that map to other values in the registration dictionary</li>
	</ol>
	<p>Container knows that Foo needs a Bar because that was explicitly spelled out in the registration when we defined:</p>
	<div class="dean_ch" style="white-space: nowrap;">Foo: <span class="br0">&#123;</span> className: <span class="st0">'Stephen.Stchur.Foo'</span>, params: <span class="br0">&#91;</span> <span class="st0">'Bar'</span> <span class="br0">&#93;</span> <span class="br0">&#125;</span></div>
	<p>The params array tells the container everything it needs to know.  So what does the code that actually implements this type resolution look like? Glad you asked.  Code below, explanation to follow:</p>
	<div class="dean_ch" style="white-space: nowrap;"><span class="kw2">function</span> Container<span class="br0">&#40;</span>reg<span class="br0">&#41;</span><br />
<span class="br0">&#123;</span><br />
&nbsp; &nbsp; <span class="kw2">function</span> instance<span class="br0">&#40;</span>factoryName<span class="br0">&#41;</span><br />
&nbsp; &nbsp; <span class="br0">&#123;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <span class="kw2">var</span> params = reg<span class="br0">&#91;</span>factoryName<span class="br0">&#93;</span>.<span class="me1">params</span>;<br />
&nbsp; &nbsp; &nbsp; &nbsp; <span class="kw2">var</span> i = params.<span class="me1">length</span>; &nbsp; &nbsp;<br />
&nbsp; &nbsp; &nbsp; &nbsp; <span class="kw1">while</span> <span class="br0">&#40;</span>i--<span class="br0">&#41;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <span class="br0">&#123;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span class="kw2">var</span> f = reg<span class="br0">&#91;</span>params<span class="br0">&#91;</span>i<span class="br0">&#93;</span><span class="br0">&#93;</span>;<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span class="kw1">if</span> <span class="br0">&#40;</span>f<span class="br0">&#41;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span class="br0">&#123;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; params<span class="br0">&#91;</span>i<span class="br0">&#93;</span> = instance<span class="br0">&#40;</span>params<span class="br0">&#91;</span>i<span class="br0">&#93;</span><span class="br0">&#41;</span>;<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span class="br0">&#125;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <span class="br0">&#125;</span></p>
	<p>&nbsp; &nbsp; &nbsp; &nbsp; <span class="kw1">return</span> create<span class="br0">&#40;</span>reg<span class="br0">&#91;</span>factoryName<span class="br0">&#93;</span>.<span class="me1">className</span>, params<span class="br0">&#41;</span><br />
&nbsp; &nbsp; <span class="br0">&#125;</span><br />
&nbsp; &nbsp; <span class="kw1">this</span>.<span class="me1">instance</span> = instance;</p>
	<p>&nbsp; &nbsp; <span class="kw2">function</span> create<span class="br0">&#40;</span>type, params<span class="br0">&#41;</span><br />
&nbsp; &nbsp; <span class="br0">&#123;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <span class="kw2">var</span> constructor = <span class="kw2">new</span> <span class="kw2">Function</span><span class="br0">&#40;</span><span class="st0">'return '</span> + type<span class="br0">&#41;</span><span class="br0">&#40;</span><span class="br0">&#41;</span>;<br />
&nbsp; &nbsp; &nbsp; &nbsp; <span class="kw2">var</span> inst = <span class="kw2">new</span> constructor<span class="br0">&#40;</span><span class="br0">&#41;</span>;<br />
&nbsp; &nbsp; &nbsp; &nbsp; inst.<span class="me1">init</span> &amp;&amp; inst.<span class="me1">init</span>.<span class="me1">apply</span><span class="br0">&#40;</span>inst, params<span class="br0">&#41;</span>;<br />
&nbsp; &nbsp; &nbsp; &nbsp; <span class="kw1">return</span> inst;<br />
&nbsp; &nbsp; <span class="br0">&#125;</span><br />
<span class="br0">&#125;</span></p>
	<p><span class="kw2">var</span> c = <span class="kw2">new</span> Container<span class="br0">&#40;</span>registration<span class="br0">&#41;</span>;<br />
<span class="kw2">var</span> f = c.<span class="me1">instance</span><span class="br0">&#40;</span><span class="st0">'Foo'</span><span class="br0">&#41;</span>;</div>
	<p>Now this is not actually the code used in the project I'm working on.  It's actually rather different, but this will suffice for now.  One thing to note is that I'm really not performing any sort of safety checks here.  If you ask the container for an instance of something that hasn't been registered, it will not fail gracefully (and it really should).  But that's an easy change and not really the focus here.</p>
	<p>There isn't a ton code that requires explanation here, except for a couple pieces.  The first piece that I suspect will raise some eyebrows is:</p>
	<div class="dean_ch" style="white-space: nowrap;"><span class="kw2">var</span> constructor = <span class="kw2">new</span> <span class="kw2">Function</span><span class="br0">&#40;</span><span class="st0">'return '</span> + type<span class="br0">&#41;</span><span class="br0">&#40;</span><span class="br0">&#41;</span>;</div>
	<p>Now let me say right off the bat that I'm not claiming this is the best way to do it (certainly it's not the only way).  Essentially, this is just a confusing way of doing:</p>
	<div class="dean_ch" style="white-space: nowrap;"><span class="kw2">var</span> constructor = <span class="kw1">eval</span><span class="br0">&#40;</span>type<span class="br0">&#41;</span>;</div>
	<p>So why not just do that?  Well, I'm not saying you can't, but a lot of people have a hard-line stance against using eval.  Also, in some browsers, eval was <strong>much</strong> slower than using new Function(..).  However, in other browsers, eval was faster (sometime nominally, sometimes significantly).</p>
	<p>Finally, there is another option in this case: you can split the type string on the dot, and actually attempt to access the parts as properties.  It would ultimately translate to something like:</p>
	<div class="dean_ch" style="white-space: nowrap;"><span class="kw2">var</span> constructor = window<span class="br0">&#91;</span><span class="st0">'Stephen'</span><span class="br0">&#93;</span><span class="br0">&#91;</span><span class="st0">'Stchur'</span><span class="br0">&#93;</span><span class="br0">&#91;</span><span class="st0">'Foo];</span></div>
	<p>Of course, you wouldn't hard code it like that, you'd split the string and loop, but I trust you get the idea.  For simple cases, this actually turns out to be quite fast, so it's something to consider.  But also worth considering is that fact that, for small numbers of repeated executions, it matters very little which method you choose.  And if you cache the resolved constructor once you get it, it matters even less.</p>
	<p>Anyway, I used new Function(..) in this post, because I think it's an interesting way to accomplish the task (not necessarily the best, but chances are you haven't seen it before, so you learned something, and that's what this blog is all about -- spreading knowledge and learning).</p>
	<p>If you're unfamiliar with Function (capital F) in Javascript, allow me to explain.</p>
	<p>Function in Javascript is a constructor that takes a string and returns you a function whose logic is whatever string-code you passed into it.  For example:</p>
	<div class="dean_ch" style="white-space: nowrap;"><span class="kw2">var</span> sayHello = <span class="kw2">new</span> <span class="kw2">Function</span><span class="br0">&#40;</span><span class="st0">'alert(&quot;hello there&quot;);'</span><span class="br0">&#41;</span>;<br />
sayHello<span class="br0">&#40;</span><span class="br0">&#41;</span>; &nbsp; &nbsp; &nbsp; <span class="co1">// alerts &quot;hello there&quot;</span></div>
	<p>It's not too hard then to see how we've leveraged Function in our case.  We created a function whose job it is to return a reference to function named, type.  That is, if type = "Foo" then this code:</p>
	<div class="dean_ch" style="white-space: nowrap;"><span class="kw2">var</span> constructor = <span class="kw2">new</span> <span class="kw2">Function</span><span class="br0">&#40;</span><span class="st0">'return '</span> + type<span class="br0">&#41;</span></div>
	<p>Will result in the variable <code>constructor</code>, pointing to the function, <code>Foo</code> (so it essentially <em>is</em> Foo).  Another way of thinking about it is that the implementation of the function we've create is:</p>
	<div class="dean_ch" style="white-space: nowrap;"><span class="kw1">return</span> Foo;</div>
	<p>And when you execute that function, what you get back is a reference to the function Foo, which is the constructor you care about.</p>
	<p>Great! Now what?  Well, now we should invoke the constructor.  Unfortunately, things fall a part a little bit here (but only a little).  There isn't really a great way to invoke a constructor with the <code>new</code> keyword and at the same time pass in an array of params to be used as the individual arguments to that constructor.  More clever Javascript Ninjas will give you creative solutions.  You can create an empty object, and then call <code>.apply</code> on the constructor, passing in the object you created as the <code>this</code> object and the items in whatever array you pass in will becomes the individual arguments to the constructor.  This however, won't result in an object that actually <em>is</em> an instance of your constructor.</p>
	<div class="dean_ch" style="white-space: nowrap;"><span class="kw2">function</span> Cat<span class="br0">&#40;</span><span class="kw3">name</span>, age<span class="br0">&#41;</span><br />
<span class="br0">&#123;</span><br />
&nbsp; &nbsp; <span class="kw1">this</span>.<span class="me1">description</span> = <span class="st0">'furry'</span>;<br />
&nbsp; &nbsp; <span class="kw1">this</span>.<span class="kw3">name</span> = <span class="kw3">name</span>;<br />
&nbsp; &nbsp; <span class="kw1">this</span>.<span class="me1">age</span> = age;<br />
<span class="br0">&#125;</span><br />
<span class="kw2">var</span> c = <span class="br0">&#123;</span><span class="br0">&#125;</span>;<br />
Cat.<span class="me1">apply</span><span class="br0">&#40;</span>c, <span class="br0">&#91;</span> <span class="st0">'Garfield'</span>, <span class="nu0">10</span> <span class="br0">&#93;</span><span class="br0">&#41;</span>;<br />
c <span class="kw1">instanceof</span> Cat; <span class="co1">// false <img src='http://blog.stchur.com/wp-includes/images/smilies/icon_sad.gif' alt=':-(' class='wp-smiley' />  </span></div>
	<p>You can carry this further though.  You can set the native prototype of your <code>c</code> object to be Cat's prototype:</p>
	<div class="dean_ch" style="white-space: nowrap;">
<span class="kw2">var</span> c = <span class="br0">&#123;</span><span class="br0">&#125;</span>;<br />
c.__proto__ = Cat.<span class="me1">prototype</span>;<br />
c <span class="kw1">instanceof</span> Cat; <span class="co1">// true <img src='http://blog.stchur.com/wp-includes/images/smilies/icon_smile.gif' alt=':-)' class='wp-smiley' /> </span></div>
	<p>However, support for <code>__proto__</code> is not so good in several older versions of a certain, still-fairly-popular-but-I-have-no-idea-why browser (I'm a Firefox guy myself).</p>
	<p>It's worth pointing out here that your temptation to try <code>c.prototype = Cat.prototype</code> will not work, but I'm not going to get into why.  This isn't a post about prototype in Javascript.</p>
	<p>If you care to, you could iterate Cat's prototype and manually copy over all the properties.  Then your object would satisfy as a Cat if you're okay with duck typing.  But it still wouldn't actually <em>be</em> a Cat.</p>
	<p>Well, if you're only targeting browsers that support <code>__proto__</code>, then that's an option.  If you don't care about your object actually <em>being</em> a true instance of the thing you're creating, then you can use <code>.apply</code> and copy over the prototype.  Or, if you have some magic pixie dust that I don't have, then I suppose that's an option too (please share).</p>
	<p>Short of any of those things, your other option is to mandate some sort of a convention.  Two conventions come to mind that are simply and work well.</p>
	<ol>
	<li>Mandate that all constructors be able to successfully execute, parameterless. And then automatically call (if it exists) some special initialization function (I like <code>.init</code>) that take all the params that really matter.</li>
	<li>Mandate that all constructors take only a single param - an array of values needed to construct the instance.</li>
	</ol>
	<p>I chose option 1 for the sample code in this post.  I like it better than option 2 because I have a Chocolate Lab named Guinness (that didn't make any sense).</p>
	<p>Moving on...</p>
	<p>With the instantiation out of the way, the only remaining bit of code might be the resolution of the params.</p>
	<p>First we grab a reference to the array of params, as defined by this factory's definition in the registration:</p>
	<div class="dean_ch" style="white-space: nowrap;"><span class="kw2">var</span> params = reg<span class="br0">&#91;</span>factoryName<span class="br0">&#93;</span>.<span class="me1">params</span>;</div>
	<p>We then loop through it (backwards, because that's how I roll), and pull out each value, assigning it to the variable, <code>f</code>.  We assume that f points to a valid, registered, factory.  If not, we leave the value alone (this isn't particular useful right now, but in a future blog post, you'll see the code reworked a bit so that the <code>.instance</code> function can accept arbitrary values to be used as other arguments in constructing your object).</p>
	<p>Now if <code>f</code> <em>does</em> point to a valid, registered factory, then we go ahead and resolve it.  Resursively.  By calling <code>.instanace</code> again.  This is important, in case of the params, are factories that take other params that are factories.</p>
	<p>Once we've resolved a param though, we can actually just have the result "take over" what was in the original params array at the index originally.</p>
	<div class="dean_ch" style="white-space: nowrap;">params<span class="br0">&#91;</span>i<span class="br0">&#93;</span> = instance<span class="br0">&#40;</span>params<span class="br0">&#91;</span>i<span class="br0">&#93;</span><span class="br0">&#41;</span>;</div>
	<p>In this way, the right value will be in the right place in the params array when it finally gets plumbed through to the call to <code>inst.init.apply(inst, params)</code></p>
	<p>Makes perfect sense right?</p>
	<p>Honestly, I think it's harder to speak/write about than it is to just understand.  It's not super complicated.  It will get more complicated though.  But that's for a future blog post.  In the mean time, here's a full, copy/pastable bit of code that actually works.</p>
	<div class="dean_ch" style="white-space: nowrap;"><span class="kw2">var</span> Stephen =<br />
<span class="br0">&#123;</span><br />
&nbsp; &nbsp; Stchur:<br />
&nbsp; &nbsp; <span class="br0">&#123;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; Foo: <span class="kw2">function</span><span class="br0">&#40;</span><span class="br0">&#41;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <span class="br0">&#123;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span class="kw1">this</span>.<span class="me1">desc</span> = <span class="st0">'foo!'</span>;<br />
&nbsp; &nbsp; &nbsp; &nbsp; <span class="br0">&#125;</span>,</p>
	<p>&nbsp; &nbsp; &nbsp; &nbsp; Bar: <span class="kw2">function</span><span class="br0">&#40;</span><span class="br0">&#41;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <span class="br0">&#123;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span class="kw1">this</span>.<span class="me1">desc</span> = <span class="st0">'bar!'</span>;<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span class="kw1">this</span>.<span class="me1">init</span> = <span class="kw2">function</span><span class="br0">&#40;</span>f<span class="br0">&#41;</span> <span class="br0">&#123;</span> <span class="kw1">this</span>.<span class="me1">myFoo</span> = f; <span class="br0">&#125;</span>;<br />
&nbsp; &nbsp; &nbsp; &nbsp; <span class="br0">&#125;</span>,</p>
	<p>&nbsp; &nbsp; &nbsp; &nbsp; Baz: <span class="kw2">function</span><span class="br0">&#40;</span><span class="br0">&#41;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <span class="br0">&#123;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span class="kw1">this</span>.<span class="me1">desc</span> = <span class="st0">'baz!'</span>;<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span class="kw1">this</span>.<span class="me1">init</span> = <span class="kw2">function</span><span class="br0">&#40;</span>f, b<span class="br0">&#41;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span class="br0">&#123;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span class="kw1">this</span>.<span class="me1">myFoo</span> = f;<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span class="kw1">this</span>.<span class="me1">myBar</span> = b;<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span class="br0">&#125;</span>;<br />
&nbsp; &nbsp; &nbsp; &nbsp; <span class="br0">&#125;</span><br />
&nbsp; &nbsp; <span class="br0">&#125;</span><br />
<span class="br0">&#125;</span>;</p>
	<p><span class="kw2">var</span> registration =<br />
<span class="br0">&#123;</span><br />
&nbsp; &nbsp; Foo: <span class="br0">&#123;</span> className: <span class="st0">'Stephen.Stchur.Foo'</span> <span class="br0">&#125;</span>,<br />
&nbsp; &nbsp; Bar: <span class="br0">&#123;</span> className: <span class="st0">'Stephen.Stchur.Bar'</span>, params: <span class="br0">&#91;</span><span class="st0">'Foo'</span><span class="br0">&#93;</span> <span class="br0">&#125;</span>,<br />
&nbsp; &nbsp; Baz: <span class="br0">&#123;</span> className: <span class="st0">'Stephen.Stchur.Baz'</span>, params: <span class="br0">&#91;</span><span class="st0">'Foo'</span>,<span class="st0">'Bar'</span><span class="br0">&#93;</span> <span class="br0">&#125;</span><br />
<span class="br0">&#125;</span>;</p>
	<p><span class="kw2">function</span> Container<span class="br0">&#40;</span>reg<span class="br0">&#41;</span><br />
<span class="br0">&#123;</span><br />
&nbsp; &nbsp; <span class="kw2">function</span> instance<span class="br0">&#40;</span>factoryName<span class="br0">&#41;</span><br />
&nbsp; &nbsp; <span class="br0">&#123;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <span class="kw2">var</span> params = reg<span class="br0">&#91;</span>factoryName<span class="br0">&#93;</span>.<span class="me1">params</span> || <span class="br0">&#91;</span><span class="br0">&#93;</span>;<br />
&nbsp; &nbsp; &nbsp; &nbsp; <span class="kw2">var</span> i = params.<span class="me1">length</span>; &nbsp; &nbsp;<br />
&nbsp; &nbsp; &nbsp; &nbsp; <span class="kw1">while</span> <span class="br0">&#40;</span>i--<span class="br0">&#41;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <span class="br0">&#123;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span class="kw2">var</span> f = reg<span class="br0">&#91;</span>params<span class="br0">&#91;</span>i<span class="br0">&#93;</span><span class="br0">&#93;</span>;<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span class="kw1">if</span> <span class="br0">&#40;</span>f<span class="br0">&#41;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span class="br0">&#123;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; params<span class="br0">&#91;</span>i<span class="br0">&#93;</span> = instance<span class="br0">&#40;</span>params<span class="br0">&#91;</span>i<span class="br0">&#93;</span><span class="br0">&#41;</span>;<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span class="br0">&#125;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <span class="br0">&#125;</span></p>
	<p>&nbsp; &nbsp; &nbsp; &nbsp; <span class="kw1">return</span> create<span class="br0">&#40;</span>reg<span class="br0">&#91;</span>factoryName<span class="br0">&#93;</span>.<span class="me1">className</span>, params<span class="br0">&#41;</span><br />
&nbsp; &nbsp; <span class="br0">&#125;</span><br />
&nbsp; &nbsp; <span class="kw1">this</span>.<span class="me1">instance</span> = instance;</p>
	<p>&nbsp; &nbsp; <span class="kw2">function</span> create<span class="br0">&#40;</span>type, params<span class="br0">&#41;</span><br />
&nbsp; &nbsp; <span class="br0">&#123;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <span class="kw2">var</span> constructor = <span class="kw2">new</span> <span class="kw2">Function</span><span class="br0">&#40;</span><span class="st0">'return '</span> + type<span class="br0">&#41;</span><span class="br0">&#40;</span><span class="br0">&#41;</span>;<br />
&nbsp; &nbsp; &nbsp; &nbsp; <span class="kw2">var</span> inst = <span class="kw2">new</span> constructor<span class="br0">&#40;</span><span class="br0">&#41;</span>;<br />
&nbsp; &nbsp; &nbsp; &nbsp; inst.<span class="me1">init</span> &amp;&amp; inst.<span class="me1">init</span>.<span class="me1">apply</span><span class="br0">&#40;</span>inst, params<span class="br0">&#41;</span>;<br />
&nbsp; &nbsp; &nbsp; &nbsp; <span class="kw1">return</span> inst;<br />
&nbsp; &nbsp; <span class="br0">&#125;</span><br />
<span class="br0">&#125;</span></p>
	<p><span class="kw2">var</span> c = <span class="kw2">new</span> Container<span class="br0">&#40;</span>registration<span class="br0">&#41;</span>;<br />
<span class="kw2">var</span> f = c.<span class="me1">instance</span><span class="br0">&#40;</span><span class="st0">'Baz'</span><span class="br0">&#41;</span>;</div>
	<p>Look for part 2 coming soon.</p>
	<p>Happy scripting!</p>
]]></content:encoded>
			<wfw:commentRss>http://blog.stchur.com/2011/08/05/dependency-injection-dependency-management-part-1/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Javascript Quiz</title>
		<link>http://blog.stchur.com/2011/06/10/javascript-quiz/</link>
		<comments>http://blog.stchur.com/2011/06/10/javascript-quiz/#comments</comments>
		<pubDate>Fri, 10 Jun 2011 18:45:25 +0000</pubDate>
		<dc:creator>sstchur</dc:creator>
				<category><![CDATA[General]]></category>

		<guid isPermaLink="false">http://blog.stchur.com/?p=146</guid>
		<description><![CDATA[Inspired by some Javascript questions I was asked recently by a few co-workers, I decided to put together what started out a little reference/cheat-sheet, but which ultimately took the form of a Javascript Quiz. It's meant to be fun and informative. You keep track of your own answers (if you want to); no one ever [...]]]></description>
			<content:encoded><![CDATA[	<p>Inspired by some Javascript questions I was asked recently by a few co-workers, I decided to put together what started out a little reference/cheat-sheet, but which ultimately took the form of a Javascript Quiz.</p>
	<p>It's meant to be fun and informative.  You keep track of your own answers (if you want to); no one ever has to know what you got right or wrong.</p>
	<p>I should mentioned that I did NOT test this in all browsers.  I'm a Firefox guy, so that's where I do most of my testing and development for little one-off personal projects like this.  I did take a quick pass with IE8 and Chrome, but I did NOT test with IE6 or IE7 or Safari.  If something doesn't work in one of those browsers, feel free to let me know, but bear in mind that there is a strong possibility that I will not care in the least <img src='http://blog.stchur.com/wp-includes/images/smilies/icon_biggrin.gif' alt=':-D' class='wp-smiley' /> </p>
	<p>And of course, there's always multiple ways to solve a problem.  I do not claim any sort of authority with any of the answers I've provided.  If you dislike an answer of feel you have a better one, good for you <img src='http://blog.stchur.com/wp-includes/images/smilies/icon_wink.gif' alt=';-)' class='wp-smiley' /> </p>
	<p>Anyway, here it is: <a href="http://blog.stchur.com/blogcode/jsquiz">http://blog.stchur.com/blogcode/jsquiz</a></p>
	<p>Enjoy!
</p>
]]></content:encoded>
			<wfw:commentRss>http://blog.stchur.com/2011/06/10/javascript-quiz/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>My New Blog</title>
		<link>http://blog.stchur.com/2008/11/07/my-new-blog/</link>
		<comments>http://blog.stchur.com/2008/11/07/my-new-blog/#comments</comments>
		<pubDate>Fri, 07 Nov 2008 20:12:47 +0000</pubDate>
		<dc:creator>sstchur</dc:creator>
				<category><![CDATA[General]]></category>

		<guid isPermaLink="false">http://blog.stchur.com/2008/11/07/my-new-blog/</guid>
		<description><![CDATA[This blog is mostly technical. I do, from time to time, post about non-technical things, but I try to keep this blog more or less about programming, the web, and javascript. But I'm a loud-mouth and I have lots to say. So rather than clutter up this blog with "too much" incoherent babble, I decided [...]]]></description>
			<content:encoded><![CDATA[	<p>This blog is mostly technical.  I do, from time to time, post about non-technical things, but I try to keep this blog more or less about programming, the web, and javascript.</p>
	<p>But I'm a loud-mouth and I have lots to say.  So rather than clutter up this blog with "too much" incoherent babble, I decided to start another.</p>
	<p>My new blog is called Stchur on Whatever (<a href = "http://whatever.stchur.com" title = "Stchur on Whatever">http://whatever.stchur.com</a>) and on it, I'll write about whatever the heck I feel like.</p>
	<p>So please check it out.</p>
]]></content:encoded>
			<wfw:commentRss>http://blog.stchur.com/2008/11/07/my-new-blog/feed/</wfw:commentRss>
		<slash:comments>2</slash:comments>
		</item>
		<item>
		<title>Microsoft Releases New Version of Live Maps</title>
		<link>http://blog.stchur.com/2008/09/24/microsoft-releases-new-version-of-live-maps/</link>
		<comments>http://blog.stchur.com/2008/09/24/microsoft-releases-new-version-of-live-maps/#comments</comments>
		<pubDate>Thu, 25 Sep 2008 02:40:34 +0000</pubDate>
		<dc:creator>sstchur</dc:creator>
				<category><![CDATA[General]]></category>
		<category><![CDATA[Gimme]]></category>
		<category><![CDATA[Live Maps]]></category>

		<guid isPermaLink="false">http://blog.stchur.com/2008/09/24/microsoft-releases-new-version-of-live-maps/</guid>
		<description><![CDATA[Microsoft released a new version of Live Maps today. This version features multi-way point routing as well as a number of other nice little updates. Another cool thing about this release is that it features Gimme quite prominently. After getting directions, you'll notice the ability to expand/collapse the directions specific to each point along the [...]]]></description>
			<content:encoded><![CDATA[	<p>Microsoft released a new version of <a href = "http://maps.live.com" title = "Live Maps">Live Maps</a> today.  This version features multi-way point routing as well as a number of other nice little updates.</p>
	<p>Another cool thing about this release is that it features <a href = "http://codeplex.com/gimme" title = "The Gimme Javascript Library">Gimme</a> quite prominently.  After getting directions, you'll notice the ability to expand/collapse the directions specific to each point along the route.  That nice, smooth animation you see is Gimme doing what Gimme does best!</p>
]]></content:encoded>
			<wfw:commentRss>http://blog.stchur.com/2008/09/24/microsoft-releases-new-version-of-live-maps/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Wither IE6?</title>
		<link>http://blog.stchur.com/2008/07/22/wither-ie6/</link>
		<comments>http://blog.stchur.com/2008/07/22/wither-ie6/#comments</comments>
		<pubDate>Tue, 22 Jul 2008 13:00:07 +0000</pubDate>
		<dc:creator>sstchur</dc:creator>
				<category><![CDATA[Beating IE into submission]]></category>
		<category><![CDATA[General]]></category>
		<category><![CDATA[MiniPosts]]></category>
		<category><![CDATA[Opinions]]></category>
		<category><![CDATA[Web-related]]></category>

		<guid isPermaLink="false">http://blog.stchur.com/2008/07/22/wither-ie6/</guid>
		<description><![CDATA[Welcome aboard guys. Glad you finally decided to start thinking about what I discussed long ago.]]></description>
			<content:encoded><![CDATA[<p>Welcome aboard <a href = "http://ajaxian.com/archives/pondering-support-of-ie6" title = "Pondering Support for IE6">guys</a>.  Glad you <em>finally</em> decided to start thinking about what <a href = "http://blog.stchur.com/2008/01/15/ie5-is-dead-and-ie6-is-on-death-row/" title = "IE5 is dead (and IE6 is on death row)">I discussed</a> long ago.</p>]]></content:encoded>
			<wfw:commentRss>http://blog.stchur.com/2008/07/22/wither-ie6/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Firefox 3</title>
		<link>http://blog.stchur.com/2008/06/19/firefox-3/</link>
		<comments>http://blog.stchur.com/2008/06/19/firefox-3/#comments</comments>
		<pubDate>Thu, 19 Jun 2008 14:45:50 +0000</pubDate>
		<dc:creator>sstchur</dc:creator>
				<category><![CDATA[Firefox Related]]></category>
		<category><![CDATA[General]]></category>
		<category><![CDATA[MiniPosts]]></category>
		<category><![CDATA[Mozilla-specific]]></category>
		<category><![CDATA[Web-related]]></category>

		<guid isPermaLink="false">http://blog.stchur.com/2008/06/19/firefox-3/</guid>
		<description><![CDATA[Well unless you've been living under a rock, you know that Firefox 3 was released on Tuesday. This is a big deal for me. When other browser makers release a new version, it's like eh... ok, that's cool (I guess). But when Mozilla releases a new Firefox, I've got a brand new shiny app to [...]]]></description>
			<content:encoded><![CDATA[<p>Well unless you've been living under a rock, you know that <a href = "http://www.getfirefox.com" Title = "Firefox">Firefox 3</a> was released on Tuesday.  This is a big deal for me. When other browser makers release a new version, it's like eh... ok, that's cool (I guess).  But when Mozilla releases a new Firefox, I've got a brand new shiny app to get me through my day to day web browsing.</p>

<p>Besides the new theme (on Windows) being a little bland, FF3 does <em>not</em> disappoint!</p>]]></content:encoded>
			<wfw:commentRss>http://blog.stchur.com/2008/06/19/firefox-3/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Palindromes</title>
		<link>http://blog.stchur.com/2008/06/14/palindromes/</link>
		<comments>http://blog.stchur.com/2008/06/14/palindromes/#comments</comments>
		<pubDate>Sat, 14 Jun 2008 19:06:11 +0000</pubDate>
		<dc:creator>sstchur</dc:creator>
				<category><![CDATA[Computing]]></category>
		<category><![CDATA[General]]></category>
		<category><![CDATA[Javascript basics]]></category>
		<category><![CDATA[Performance]]></category>
		<category><![CDATA[Useful Functions]]></category>
		<category><![CDATA[javascript]]></category>
		<category><![CDATA[palindrome]]></category>

		<guid isPermaLink="false">http://blog.stchur.com/2008/06/14/palindromes/</guid>
		<description><![CDATA[The other evening while I was watching TV, I happened to have my laptop out, so I started dabbling around in Firebug just for the heck of it. I'll often just start writing functions for no particular reason, other than to refresh my skills or perhaps discover something about Javascript that I hadn't previously known. [...]]]></description>
			<content:encoded><![CDATA[	<p>The other evening while I was watching TV, I happened to have my laptop out, so I started dabbling around in Firebug just for the heck of it.  I'll often just start writing functions for no particular reason, other than to refresh my skills or perhaps discover something about Javascript that I hadn't previously known.</p>
	<p>So I was monkeying around, and I decided to write a function to determine if a string were a <a href = "http://en.wikipedia.org/wiki/Palindrome" title = "Wikipedia: Palindrome">palindrome</a>.  This is not a very difficult problem, but it might make for a descent introductory interview question (one of those questions you'd ask to rule out the truly inept).</p>
	<p>With problems like this, I'm mostly interested in two things:</p>
	<ol>
	<li>Writing the function in a clever way, so as to achieve the least amount of code possible</li>
	<li>Writing the function in the most efficient way possible</li>
	</ol>
	<p>Now, if it were Ruby, writing the clever solution (which might also happen to be the most efficient solution in that language -- not sure) would be ridiculously easy:</p>
	<div class="dean_ch" style="white-space: nowrap;"><span class="kw1">def</span> pal<span class="br0">&#40;</span>s<span class="br0">&#41;</span><br />
&nbsp; &nbsp;s == s.<span class="me1">reverse</span><br />
<span class="kw1">end</span></div>
	<p>That's so trivial, it's probably not even worth writing.</p>
	<p>But Javascript doesn't have a .reverse() method on strings, so you have to take an extra step (still pretty easy though):</p>
	<div class="dean_ch" style="white-space: nowrap;"><span class="kw2">function</span> pal<span class="br0">&#40;</span>s<span class="br0">&#41;</span><br />
<span class="br0">&#123;</span><br />
&nbsp; &nbsp;<span class="kw1">return</span> s === s.<span class="me1">split</span><span class="br0">&#40;</span><span class="st0">''</span><span class="br0">&#41;</span>.<span class="me1">reverse</span><span class="br0">&#40;</span><span class="br0">&#41;</span>.<span class="me1">join</span><span class="br0">&#40;</span><span class="st0">''</span><span class="br0">&#41;</span>;<br />
<span class="br0">&#125;</span></div>
	<p>Split the string into an array, reverse the array, and then join it back together.  Since this is all native Javascript stuff, it turns out to be reasonably fast, though one might expect you could do better.</p>
	<p>For grins and giggles, I decided to see how a manual solution, where I compare characters starting at each end of the string and work towards the middle, would compare:</p>
	<div class="dean_ch" style="white-space: nowrap;"><span class="kw2">function</span> pal<span class="br0">&#40;</span>s<span class="br0">&#41;</span><br />
<span class="br0">&#123;</span><br />
&nbsp; &nbsp;<span class="kw2">var</span> i, j,<br />
&nbsp; &nbsp;len = s.<span class="me1">length</span>,<br />
&nbsp; &nbsp;mid = Math.<span class="me1">floor</span><span class="br0">&#40;</span>len / <span class="nu0">2</span><span class="br0">&#41;</span>;</p>
	<p>&nbsp; &nbsp;<span class="kw1">for</span> <span class="br0">&#40;</span>i = <span class="nu0">0</span>, j = len - <span class="nu0">1</span>; i &lt; mid; i++, j--<span class="br0">&#41;</span><br />
&nbsp; &nbsp;<span class="br0">&#123;</span><br />
&nbsp; &nbsp;&nbsp; &nbsp;<span class="kw1">if</span> <span class="br0">&#40;</span>s.<span class="me1">charAt</span><span class="br0">&#40;</span>i<span class="br0">&#41;</span> !== s.<span class="me1">charAt</span><span class="br0">&#40;</span>j<span class="br0">&#41;</span><span class="br0">&#41;</span> <span class="kw1">return</span> <span class="kw2">false</span>;<br />
&nbsp; &nbsp;<span class="br0">&#125;</span><br />
&nbsp; &nbsp;<span class="kw1">return</span> <span class="kw2">true</span>;<br />
<span class="br0">&#125;</span></div>
	<p>This function will probably fail quickly, so that's good, but it also turns out to be generally faster than the split/reverse/join version.  It makes sense, since you're going through the string only one time and twice as fast as normal since you're traversing from both ends at the same time.</p>
	<p>Can you do even better though?  Maybe.  At least in Firefox you can, and quick tests seemed to confirm across other browsers too (but I didn't test extensively).</p>
	<p>What if we split the string in half, and only reverse half of it.  Then we can compare the first half with the original half:</p>
	<div class="dean_ch" style="white-space: nowrap;"><span class="kw2">function</span> pal<span class="br0">&#40;</span>s<span class="br0">&#41;</span><br />
<span class="br0">&#123;</span><br />
&nbsp; &nbsp;<span class="kw2">var</span> len = s.<span class="me1">length</span>,<br />
&nbsp; &nbsp;x = len / <span class="nu0">2</span>,<br />
&nbsp; &nbsp;y = x === Math.<span class="me1">floor</span><span class="br0">&#40;</span>x<span class="br0">&#41;</span> ? x : <span class="br0">&#40;</span>x = Math.<span class="me1">floor</span><span class="br0">&#40;</span>x<span class="br0">&#41;</span><span class="br0">&#41;</span> + <span class="nu0">1</span>;</p>
	<p>&nbsp; &nbsp;<span class="kw1">return</span> s.<span class="me1">substr</span><span class="br0">&#40;</span><span class="nu0">0</span>, x<span class="br0">&#41;</span>.<span class="me1">split</span><span class="br0">&#40;</span><span class="st0">''</span><span class="br0">&#41;</span>.<span class="me1">reverse</span><span class="br0">&#40;</span><span class="br0">&#41;</span>.<span class="me1">join</span><span class="br0">&#40;</span><span class="st0">''</span><span class="br0">&#41;</span> === s.<span class="me1">substr</span><span class="br0">&#40;</span>y,len<span class="br0">&#41;</span>;<br />
<span class="br0">&#125;</span></div>
	<p>The idea here is just split the string in half, reverse one of the two pieces and compare them.  For example:</p>
	<p>TOOT: splits into "TO" and "OT."  Reverse one of them, say the second part ("OT") and compare.</p>
	<p>If you've got a odd number of characters, you can ignore the middle most character:</p>
	<p>RACECAR: splits into "RAC" and "CAR" (ignore the "E" in the middle).  Reverse the second part ("CAR") and compare.</p>
	<p>As it turned out, this seemed to be faster than the other two methods.  Not by orders of magnitude, but not insignificantly either.  Here are the number from Firefox, running each function 10,000 time on the string "gohangasalamiimalasagnahog" (go hang a salami, i'm a lasagna hog).</p>
	<ul>
	<li>(Traverse from both ends): 797ms</li>
	<li>(Split/Reverse/Join): 969ms</li>
	<li>(Compare halves): 640ms</li>
	</ul>
	<p>Of course, numbers varied slightly during each run, but overall the they were consistent relative to each other.</p>
	<p>So there ya go.  Nothing in particular I wanted to point out here; just found it interesting and thought you might too.</p>
	<p>Think my code sucks?  Got a better solution?  Say so in the comments (you won't hurt my feelings).</p>
]]></content:encoded>
			<wfw:commentRss>http://blog.stchur.com/2008/06/14/palindromes/feed/</wfw:commentRss>
		<slash:comments>5</slash:comments>
		</item>
		<item>
		<title>Another Puzzle</title>
		<link>http://blog.stchur.com/2008/06/09/another-puzzle/</link>
		<comments>http://blog.stchur.com/2008/06/09/another-puzzle/#comments</comments>
		<pubDate>Mon, 09 Jun 2008 17:25:38 +0000</pubDate>
		<dc:creator>sstchur</dc:creator>
				<category><![CDATA[Computing]]></category>
		<category><![CDATA[General]]></category>
		<category><![CDATA[npr]]></category>
		<category><![CDATA[puzzle]]></category>

		<guid isPermaLink="false">http://blog.stchur.com/2008/06/09/another-puzzle/</guid>
		<description><![CDATA[Well after some significant thought, I have decided that I was either not given enough information to solve the puzzle in my last post, or I'm going to need a hint from the person who gave me the puzzle. Either way though, I don't have an answer to post as I promised I would, but [...]]]></description>
			<content:encoded><![CDATA[	<p>Well after some significant thought, I have decided that I was either not given enough information to solve the puzzle in my <a href = "http://blog.stchur.com/2008/06/05/a-puzzle/" Title = "Incoherent Babble | A Puzzle">last post</a>, or I'm going to need a hint from the person who gave me the puzzle.</p>
	<p>Either way though, I don't have an answer to post as I promised I would, but not to worry... I have something better: another puzzle!</p>
	<p>This one I heard on NPR's Puzzle Master, so I know it is legit:</p>
	<div>
On a calculator display there are 5 digits.  The first 4 digits are: 8 7 3 5.  What is the fifth digit?
</div>
	<p>It's not a trick question, and there is a simple and elegant solution to the puzzle.</p>
	<p><button onclick="document.getElementById('theAnswer').style.display='block';">Display the answer</button></p>
	<div id = "theAnswer" style = "display: none; border: 2px solid #ccc; margin: 1em; padding: 1em; background: #333;">
	<p>The fifth digit is 5 (so that the calclator display reads: 8 7 3 5 5).  Why?  Because it fits the pattern.  Digits displayed on a calculator are usually displayed with bars. For example a 3 would be displayed with 5 bars, like so:</p>
	<p style = "margin-left: 3em;">
_<br />
_|<br />
_|
</p>
	<p>So if you start with an 8, you can easily figure out the pattern.  An 8 is made up of 7 bars, so 7 is the next digit.  A 7 is made up of 3 bars so 3 is the next digit.  A 3 is made up of 5 bars so 5 is the next digit.  And finally, a 5 is made up of 5 bars, so 5 is again the next digit.</p>
	</div>
	<p>In case you're wondering, I heard this in the car listening to the radio, and I'd estimate that I drove about 5 miles or so before the answers came to me, so I'll guess that that was about 5 minutes.  How long did it take you to solve?</p>
	<p>Comments welcome.</p>
]]></content:encoded>
			<wfw:commentRss>http://blog.stchur.com/2008/06/09/another-puzzle/feed/</wfw:commentRss>
		<slash:comments>5</slash:comments>
		</item>
	</channel>
</rss>

