<?xml version="1.0" encoding="utf-8"?>
<feed xmlns="http://www.w3.org/2005/Atom">

  <title><![CDATA[LooselyTyped]]></title>
  <link href="http://looselytyped.com/atom.xml" rel="self"/>
  <link href="http://looselytyped.com/"/>
  <updated>2013-07-27T16:03:50-04:00</updated>
  <id>http://looselytyped.com/</id>
  <author>
    <name><![CDATA[Raju Gandhi]]></name>
    
  </author>
  <generator uri="http://octopress.org/">Octopress</generator>

  
  <entry>
    <title type="html"><![CDATA[On Making a Better iPhone Home Screen]]></title>
    <link href="http://looselytyped.com/blog/2013/07/24/on-making-better-iphone-home-screen/"/>
    <updated>2013-07-24T21:17:00-04:00</updated>
    <id>http://looselytyped.com/blog/2013/07/24/on-making-better-iphone-home-screen</id>
    <content type="html"><![CDATA[<p>Considering the little computers that we carry around in our pockets (or purses) all day help us with our email, internet access, personal GPS recorder, social networking, (very) personal journals and once in a while also act as a phone, it makes sense for us to want to protect all that data. The easiest way to keep our phones from prying eyes is to switch on the passcode functionality.</p>

<p>Of course this presents itself as a Catch-22. On one hand we know if we were to forget our phone at a coffee shop or a public library (or even worse lose our phone), it would prevent someone from getting to know pretty much everything there is to know about us. On the other hand it also prevents a good Samaritan from returning the phone to us in the event they find it.</p>

<p>Furthermore, locking the phone also obstructs emergency services from getting a contact person in the event of an unfortunate accident.</p>

<p>So what does one do?</p>

<!-- more -->


<p>For what it is worth, there are tons of <a href="http://en.wikipedia.org/wiki/In_case_of_emergency">ICE</a> apps out there for the iPhone (or any other smart phone). All of these <a href="https://itunes.apple.com/us/app/ice-standard-emergency-standard/id412786820?mt=8">apps</a> record relevant parts of our medical history (such as blood type, current medications and any existing allergies) allowing emergency services to quickly determine how to respond. Furthermore these allow us to make the info page as the Lock screen so the paramedic does not need to unlock the phone in order to get to that information.</p>

<p>Needless to say these serve well as our &ldquo;In case of Lost Phone&rdquo; application because they list emergency contacts as well.</p>

<p>One concern with the lock screens these apps generate is their looks :&ndash;/ Of course this may be a tad short-sighted especially in light of the medical concern, but that does not prevent us from trying, right? :)</p>

<p>Allow me to admit that I am a big fan (and long time lurker) on both <a href="http://lifehacker.com/">Lifehacker</a> and <a href="http://appchronicles.com/">App Chronicles</a> (especially they Apps Gone Free <a href="http://appchronicles.com/category/apps-gone-free/">page</a>). A while back Lifehacker posted an <a href="http://lifehacker.com/5862705/add-an-emergency-number-to-your-phones-lock-screen-wallpaper">article</a> that got me thinking on creative ways to add ICE information on the home screen. Soon after seeing that article App Chronicles posted that <a href="https://itunes.apple.com/us/app/typic-pro-photo-text-editor/id601467470?mt=8">Typic Pro &ndash; Photo Text Editor</a> that had gone free for a day and I managed to snap it up. Typic Pro allows us to add captions and design elements (like banners and frames) to our photographs which is exactly what we need.</p>

<p>Game on.</p>

<p>First thing we do is was open up the Typic Pro which presents us with an option to take a photo, or select one from our photo library. We will opt to take a new picture. The idea here was to get a completely black background, so cover up the camera lens with a finger, or rest it on any opaque surface to block out all light (be sure to switch off the flash for this) and click. There! A completely black background.</p>

<p>Then, go to town with it. Typic Pro allows us to set (at most <strong>two</strong>) captions on the picture, pick font, font colors etc. Here&rsquo;s an example.</p>

<p><img src="http://looselytyped.com/images/2013-07-24-on-making-better-iphone-home-screen/1.JPG" width="320" height="480" title="Lock Screen 1" ></p>

<p>Let us try a few more. Here is another one</p>

<p><img src="http://looselytyped.com/images/2013-07-24-on-making-better-iphone-home-screen/2.JPG" width="320" height="480" title="Lock Screen 2" ></p>

<p>Of course we need to be careful with how far we take this. Typic Pro allows us to pick one of many available fonts from a deck but the idea is to keep it legible. After all the purpose of this exercise is that anyone finding the phone should be able to read it without any difficulty.</p>

<p>Typic Pro only allows a maximum of <strong>two</strong> captions per image which can be a tad restrictive at times. Fortunately we can easily work around this. Simply line up the first two captions to accommodate for more, save the image, then open that image up again in Typic Pro and add two more!</p>

<p>As an example we can start here</p>

<p><img src="http://looselytyped.com/images/2013-07-24-on-making-better-iphone-home-screen/3.JPG" width="320" height="480" title="Partial Image" ></p>

<p>Notice now there is some some space left on the right-hand side of the name to allow for a third caption. Open the image up again in Typic Pro and add another caption.</p>

<p><img src="http://looselytyped.com/images/2013-07-24-on-making-better-iphone-home-screen/4.JPG" width="320" height="480" title="Lock Screen with more than two captions" ></p>

<p>Pretty sweet, right? :)</p>

<p>So there we have it. A nice way to create a lock screen wallpaper. Here is one in it&rsquo;s full glory</p>

<p><img src="http://looselytyped.com/images/2013-07-24-on-making-better-iphone-home-screen/5.JPG" width="320" height="480" title="Lock Screen in use" ></p>

<p>A few final points of note. One, bear in mind the idea is legibility first and foremost. Second, the image <strong>will</strong> need adjusting prior to setting it as the wallpaper especially if the text is dangerously close to the bottom edge of the screen. Third we can always use an image as a background, but since our topmost priority is legibility a blank background works best.</p>

<p>Finally, we <strong>must</strong> realize that ICE applications and cards have their place. While this is a great experiment in cosmetics it cannot (and should not) come in the way of our well-being. Do what is right, aesthetics be damned.</p>

<p>With that said, be careful and safe out there.</p>

<p>[Full disclaimer: I am in <strong>no way</strong> affiliated with Lifehacker, App Chronicles or Typic Pro apart from being a happy customer. All the links in this article are affiliate free]</p>
]]></content>
  </entry>
  
  <entry>
    <title type="html"><![CDATA[On Prototypal Inheritance - Part II]]></title>
    <link href="http://looselytyped.com/blog/2012/08/22/on-prototypal-inheritance-part-ii/"/>
    <updated>2012-08-22T20:22:00-04:00</updated>
    <id>http://looselytyped.com/blog/2012/08/22/on-prototypal-inheritance-part-ii</id>
    <content type="html"><![CDATA[<p><em>This is an article I wrote for <a href="https://www.nofluffjuststuff.com/home/magazine_subscribe">NFJS, The Magazine</a>&rsquo;s <a href="https://www.nofluffjuststuff.com/home/magazine_subscribe?id=32">April, 2012</a> issue. This is a 2-part series, this being the second one. You can find the first one <a href="http://looselytyped.com/blog/2012/08/18/on-prototypal-inheritance/">here</a></em>.</p>

<p><em>In the first installment of this series, we discussed objects and functions in JavaScript. In this second, and final, installment in this series, we will go a bit further and see how functions are used as &ldquo;instance factories&rdquo; or constructors to create objects. We will see how we can use these &ldquo;constructors&rdquo; to not only instantiate new instances, but also endow new objects with a predefined set of properties applicable to that &ldquo;class&rdquo; of objects (much like Java classes). Finally, we will put it all together to build our own hierarchy. We will also see some potential potholes and how to avoid them in your code.</em></p>

<!-- more -->


<p>In our <a href="http://looselytyped.com/blog/2012/08/18/on-prototypal-inheritance/">previous</a> discussion, we learned about JavaScript objects and that they have a few magical properties adorned upon them, specifically the <code>constructor</code> property and the <code>__proto__</code> property. We also noted that <em>all</em> objects (except <code>Object.prototype</code>) participate in a hierarchy &ndash; by having their <code>__proto__</code> property point (directly, or indirectly) to <code>Object.prototype</code>.</p>

<p>We also learned that JavaScript functions are objects themselves and have an additional property called <code>prototype</code>, which points to a plain old JavaScript object. This <code>prototype</code> object, just like any other JavaScript object, has a <code>constructor</code> property that points back to the function itself and it&rsquo;s <code>__proto__</code> property points to <code>Object.prototype</code>.</p>

<p>Before proceeding, I encourage you to fire up Google Chrome&rsquo;s Console (or Firebug&rsquo;s console) so that you can experiment with the code (See <a href="http://looselytyped.com/blog/2012/08/18/on-prototypal-inheritance/">Part I</a> to see how you can do this).</p>

<p>Also, I would like to include our last code snippet <a href="#Listing_GAN-1">Listing 1</a> and its accompanying visualization <a href="#Figure_GAN-1">Figure 1</a> from Part 1 just for reference purposes &ndash; we are going to need them for our discussion.</p>

<p><a name="Listing_GAN-1"></a></p>

<figure class='code'><figcaption><span>Listing 1 - Function.prototype </span></figcaption>
 <div class="highlight"><table><tr><td class="gutter"><pre class="line-numbers"><span class='line-number'>1</span>
<span class='line-number'>2</span>
<span class='line-number'>3</span>
<span class='line-number'>4</span>
<span class='line-number'>5</span>
<span class='line-number'>6</span>
</pre></td><td class='code'><pre><code class='javascript'><span class='line'><span class="kd">function</span> <span class="nx">Person</span><span class="p">(</span><span class="nx">name</span><span class="p">)</span> <span class="p">{</span>
</span><span class='line'>    <span class="c1">// ignore the argument for this listing</span>
</span><span class='line'><span class="p">}</span>
</span><span class='line'>
</span><span class='line'><span class="nx">console</span><span class="p">.</span><span class="nx">log</span><span class="p">(</span><span class="nx">Person</span><span class="p">.</span><span class="nx">prototype</span><span class="p">);</span> <span class="c1">// evaluates to Person { }</span>
</span><span class='line'><span class="nx">console</span><span class="p">.</span><span class="nx">log</span><span class="p">(</span><span class="k">typeof</span> <span class="nx">Person</span><span class="p">.</span><span class="nx">prototype</span><span class="p">);</span> <span class="c1">// evaluates to &quot;object&quot;</span>
</span></code></pre></td></tr></table></div></figure>


<p>Just like we discussed in Part 1, we are defining a function called <code>Person</code>. This function object has a <code>prototype</code> property that points to a simple JavaScript object, referred to as <code>Person.prototype</code>. This <code>prototype</code> has its own <code>__proto__</code> property, which in turn points to <code>Object.prototype</code>, and a <code>constructor</code> property that points back to the <code>Person</code> function object. This hierarchy is visualized in <a href="#Figure_GAN-1">Figure 1</a>.</p>

<p><a name="Figure_GAN-1"></a>
<img src="http://looselytyped.com/images/2012-08-22-on-prototypal-inheritance-part-ii/Figure_GAN-6_Color.png" width="618" height="388" title="Object.prototype visualized" ></p>

<p>So that was our recap of Part 1. Ready to begin? Let&rsquo;s go …</p>

<h2>The Constructor Invocation Pattern</h2>

<p>Let&rsquo;s say we wanted to create a &ldquo;class&rdquo; of <code>Person</code> objects. These objects are to have a name, and should be polite to one another. <a href="#Listing_GAN-2">Listing 2</a> is how we would declare and use the <code>Person</code> function to achieve this.</p>

<p><a name="Listing_GAN-2"></a></p>

<figure class='code'><figcaption><span>Listing 2 - Constructor invocation pattern </span></figcaption>
 <div class="highlight"><table><tr><td class="gutter"><pre class="line-numbers"><span class='line-number'>1</span>
<span class='line-number'>2</span>
<span class='line-number'>3</span>
<span class='line-number'>4</span>
<span class='line-number'>5</span>
<span class='line-number'>6</span>
<span class='line-number'>7</span>
<span class='line-number'>8</span>
<span class='line-number'>9</span>
<span class='line-number'>10</span>
<span class='line-number'>11</span>
<span class='line-number'>12</span>
<span class='line-number'>13</span>
</pre></td><td class='code'><pre><code class='javascript'><span class='line'><span class="kd">function</span> <span class="nx">Person</span><span class="p">(</span><span class="nx">name</span><span class="p">)</span> <span class="p">{</span>
</span><span class='line'>    <span class="k">this</span><span class="p">.</span><span class="nx">name</span> <span class="o">=</span> <span class="nx">name</span><span class="p">;</span>
</span><span class='line'>    <span class="k">this</span><span class="p">.</span><span class="nx">sayHello</span> <span class="o">=</span> <span class="kd">function</span><span class="p">(</span><span class="nx">other</span><span class="p">)</span> <span class="p">{</span>
</span><span class='line'>        <span class="k">return</span> <span class="s2">&quot;Hello &quot;</span> <span class="o">+</span> <span class="nx">other</span><span class="p">.</span><span class="nx">name</span> <span class="o">+</span> <span class="s2">&quot;! My name is &quot;</span> <span class="o">+</span> <span class="k">this</span><span class="p">.</span><span class="nx">name</span><span class="p">;</span>
</span><span class='line'>    <span class="p">}</span>
</span><span class='line'><span class="p">}</span>
</span><span class='line'>
</span><span class='line'><span class="c1">// Using the &#39;Person&#39; function as a &quot;constructor&quot;</span>
</span><span class='line'><span class="kd">var</span> <span class="nx">brendan</span> <span class="o">=</span> <span class="k">new</span> <span class="nx">Person</span><span class="p">(</span><span class="s2">&quot;Brendan&quot;</span><span class="p">);</span>
</span><span class='line'><span class="kd">var</span> <span class="nx">douglas</span> <span class="o">=</span> <span class="k">new</span> <span class="nx">Person</span><span class="p">(</span><span class="s2">&quot;Douglas&quot;</span><span class="p">);</span>
</span><span class='line'>
</span><span class='line'><span class="nx">console</span><span class="p">.</span><span class="nx">log</span><span class="p">(</span><span class="nx">brendan</span><span class="p">.</span><span class="nx">name</span><span class="p">);</span> <span class="c1">// returns &quot;Brendan&quot;</span>
</span><span class='line'><span class="nx">console</span><span class="p">.</span><span class="nx">log</span><span class="p">(</span><span class="nx">brendan</span><span class="p">.</span><span class="nx">sayHello</span><span class="p">(</span><span class="nx">douglas</span><span class="p">));</span> <span class="c1">// returns &quot;Hello Douglas! My name is Brendan&quot;</span>
</span></code></pre></td></tr></table></div></figure>


<p>Some subtle, yet important things to note here. Our <code>Person</code> function object does not have an explicit <code>return</code>. Furthermore, within <code>Person</code> we are referring to <em>some</em> object as <code>this</code>. Lastly, we invoke the <code>Person</code> function with the <code>new</code> keyword. So what&rsquo;s going on here?</p>

<p>JavaScript overloads the role of a function to act as a <em>constructor</em> when invoked with a <code>new</code> &ndash; this is called the &ldquo;Constructor Invocation pattern&rdquo;. When you do this, a new object pops into existence within the scope of the constructor, which you can refer to as <code>this</code>. Finally, the constructor <em>evaluates</em> to (that is &ldquo;returns&rdquo;) this newly created object.</p>

<p>Armed with that information, you can see what&rsquo;s going on in <a href="#Listing_GAN-2">Listing 2</a>. When we invoke <code>Person</code> with the <code>new</code> keyword, JavaScript creates a new object. This object, which we can get a handle to using the <code>this</code> keyword within the scope of the <code>Person</code> constructor, is then adorned with the <code>name</code> property, and a <code>sayHello</code> method. <code>sayHello</code> can refer to that object&rsquo;s <code>name</code> using <code>this.name</code>. Finally, <code>Person</code> returns this newly created object, which is assigned to the variable <code>brendan</code>. Essentially, <code>brendan</code> is an &ldquo;instantiation&rdquo; of the <code>Person</code> function. Another way to look at <code>Person</code> is to think of it as the &ldquo;constructor&rdquo; and the &ldquo;class&rdquo; all rolled into one.</p>

<p>I would like to point out that both <code>brendan</code> and <code>douglas</code> are JavaScript objects like any other &ndash; they both have <code>__proto__</code> pointers but with one big difference &ndash; <code>this.__proto__</code> (where <code>this</code> is the newly created object, so either <code>brendan</code> or <code>douglas</code> &ndash; See <a href="#Figure_GAN-2">Figure 2</a>) pointer points to <code>Person.prototype</code>, and not <code>Object.prototype</code>. Conceptually after evaluation, <a href="#Listing_GAN-2">Listing 2</a> looks as shown in <a href="#Figure_GAN-2">Figure 2</a>.</p>

<p><a name="Figure_GAN-2"></a>
<img src="http://looselytyped.com/images/2012-08-22-on-prototypal-inheritance-part-ii/Figure_GAN-7_Color.png" width="506" height="370" title="Objects created using the new operator" ></p>

<p>Compare <a href="#Figure_GAN-2">Figure 2</a> with <a href="#Figure_GAN-1">Figure 1</a> and see how we have managed to <strong>intercept</strong> the <code>__proto__</code> lookup chain. If we were to introduce <code>sayGoodbye</code> as a method in <code>Person.prototype</code> only <code>brendan</code> and <code>douglas</code> would see that method as part of the method lookup &ndash; all other objects that directly (or indirectly) inherit from <code>Object.prototype</code> are not affected! This allows us to define methods that apply only to a specific (in this case <code>Person</code>) &ldquo;class&rdquo; of objects. Pretty neat, right?</p>

<p>Let&rsquo;s take it up another notch. We have the <code>Person</code> function defined, and consequently we get a <code>Person.prototype</code>. But suppose we realize we need to model <code>Mammal</code>, from which <code>Person</code> (and subsequently) <code>brendan</code> and <code>douglas</code> are to inherit from. How do we go about doing this? Think about this for a few minutes before taking a peek at <a href="#Listing_GAN-3">Listing 3</a> (Hint: You have to figure out a way to make <code>Person</code>&rsquo;s prototype point to <code>Mammal</code>&rsquo;s prototype)</p>

<p><a name="Listing_GAN-3"></a></p>

<figure class='code'><figcaption><span>Listing 3 - Creating deeper hierarchies </span></figcaption>
 <div class="highlight"><table><tr><td class="gutter"><pre class="line-numbers"><span class='line-number'>1</span>
<span class='line-number'>2</span>
<span class='line-number'>3</span>
<span class='line-number'>4</span>
<span class='line-number'>5</span>
<span class='line-number'>6</span>
<span class='line-number'>7</span>
<span class='line-number'>8</span>
<span class='line-number'>9</span>
<span class='line-number'>10</span>
</pre></td><td class='code'><pre><code class='javascript'><span class='line'> <span class="c1">// Tack this code on after Listing GAN-2 in your console window</span>
</span><span class='line'> <span class="kd">function</span> <span class="nx">Mammal</span><span class="p">()</span> <span class="p">{}</span>
</span><span class='line'> <span class="nx">Mammal</span><span class="p">.</span><span class="nx">prototype</span><span class="p">.</span><span class="nx">nurture</span> <span class="o">=</span> <span class="kd">function</span><span class="p">()</span> <span class="p">{</span>
</span><span class='line'>     <span class="k">return</span> <span class="s2">&quot;Care&quot;</span><span class="p">;</span>
</span><span class='line'> <span class="p">};</span>
</span><span class='line'>
</span><span class='line'> <span class="c1">// this is where we rearrange the pointer</span>
</span><span class='line'> <span class="nx">Person</span><span class="p">.</span><span class="nx">prototype</span><span class="p">.</span><span class="nx">__proto__</span> <span class="o">=</span> <span class="nx">Mammal</span><span class="p">.</span><span class="nx">prototype</span><span class="p">;</span>
</span><span class='line'>
</span><span class='line'> <span class="nx">console</span><span class="p">.</span><span class="nx">log</span><span class="p">(</span><span class="nx">brendan</span><span class="p">.</span><span class="nx">nurture</span><span class="p">);</span> <span class="c1">// evaluates to &quot;Care&quot;</span>
</span></code></pre></td></tr></table></div></figure>


<p>Here, <code>Mammal</code> is just another function, and has its own <code>prototype</code>. This <code>prototype</code> object is no different than <code>Person.prototype</code> in that it has a <code>__proto__</code> property that points to <code>Object.prototype</code>. We then change <code>Person.prototype</code> to point to <code>Mammal.prototype</code>. See <a href="#Figure_GAN-3">Figure 3</a> to see how this lays itself out (I have left out <code>Object.prototype</code>, but <code>Mammal.prototype.__proto__</code> points to <code>Object.prototype</code> as indicated by the dashed arrow).</p>

<p><a name="Figure_GAN-3"></a>
<img src="http://looselytyped.com/images/2012-08-22-on-prototypal-inheritance-part-ii/Figure_GAN-8_Color.png" width="504" height="370" title="Intercepting the prototype chain" ></p>

<p>The key thing to understand and remember here is that the constructor functions are mere facilitators allowing us to create objects with their <code>__proto__</code> property set correctly. It&rsquo;s the objects themselves that intrinsically know who their &ldquo;prototypes&rdquo; are and enable function (and property) lookup.</p>

<p>There is another way to set the inheritance chain properly without resorting to what we did in <a href="#Listing_GAN-3">Listing 3</a>. In <a href="http://looselytyped.com/blog/2012/08/18/on-prototypal-inheritance/">Part I</a> of this series, we used <code>Object.create</code> to create a new object, but we passed <code>null</code> as an argument. Now that we know how we can use prototypes to enrich the inheritance chain, we will see how to use it. This argument, if supplied, tells JavaScript what the newly created object&rsquo;s prototype should be. Consider <a href="#Listing_GAN-4">Listing 4</a> to see how you can use this.</p>

<p><a name="Listing_GAN-4"></a></p>

<figure class='code'><figcaption><span>Listing 4 - Using `Object.create` with a prototype </span></figcaption>
 <div class="highlight"><table><tr><td class="gutter"><pre class="line-numbers"><span class='line-number'>1</span>
<span class='line-number'>2</span>
<span class='line-number'>3</span>
<span class='line-number'>4</span>
<span class='line-number'>5</span>
<span class='line-number'>6</span>
<span class='line-number'>7</span>
<span class='line-number'>8</span>
<span class='line-number'>9</span>
<span class='line-number'>10</span>
<span class='line-number'>11</span>
<span class='line-number'>12</span>
<span class='line-number'>13</span>
<span class='line-number'>14</span>
</pre></td><td class='code'><pre><code class='javascript'><span class='line'><span class="kd">var</span> <span class="nx">person</span> <span class="o">=</span> <span class="p">{</span>
</span><span class='line'>    <span class="nx">sayHello</span><span class="o">:</span> <span class="kd">function</span><span class="p">(</span><span class="nx">other</span><span class="p">)</span> <span class="p">{</span>
</span><span class='line'>        <span class="k">return</span> <span class="s2">&quot;Hello &quot;</span> <span class="o">+</span> <span class="nx">other</span><span class="p">.</span><span class="nx">name</span> <span class="o">+</span> <span class="s2">&quot;! My name is &quot;</span> <span class="o">+</span> <span class="k">this</span><span class="p">.</span><span class="nx">name</span><span class="p">;</span>
</span><span class='line'>    <span class="p">}</span>
</span><span class='line'><span class="p">};</span>
</span><span class='line'>
</span><span class='line'><span class="c1">// here &#39;person&#39; becomes the &#39;prototype&#39; for &#39;brendan&#39;</span>
</span><span class='line'><span class="kd">var</span> <span class="nx">brendan</span> <span class="o">=</span> <span class="nb">Object</span><span class="p">.</span><span class="nx">create</span><span class="p">(</span><span class="nx">person</span><span class="p">);</span>
</span><span class='line'><span class="nx">brendan</span><span class="p">.</span><span class="nx">name</span> <span class="o">=</span> <span class="s2">&quot;Brendan&quot;</span><span class="p">;</span>
</span><span class='line'>
</span><span class='line'><span class="kd">var</span> <span class="nx">douglas</span> <span class="o">=</span> <span class="nb">Object</span><span class="p">.</span><span class="nx">create</span><span class="p">(</span><span class="nx">person</span><span class="p">);</span>
</span><span class='line'><span class="nx">douglas</span><span class="p">.</span><span class="nx">name</span> <span class="o">=</span> <span class="s2">&quot;Douglas&quot;</span><span class="p">;</span>
</span><span class='line'>
</span><span class='line'><span class="nx">brendan</span><span class="p">.</span><span class="nx">sayHello</span><span class="p">(</span><span class="nx">douglas</span><span class="p">);</span> <span class="c1">// evaluates to &quot;Hello Douglas! My name is Brendan&quot;</span>
</span></code></pre></td></tr></table></div></figure>


<p>We create a <code>person</code> object that happens to have a <code>sayHello</code> method. We then use this <em>object</em> (Note: Not a function) as an argument to <code>Object.create</code>. Fundamentally, this is the same as <a href="#Listing_GAN-2">Listing 2</a>. The newly created <code>person</code> object serves as <code>brendan</code>&rsquo;s <code>__proto__</code> and everything works just as we saw earlier. This just gives us another way to correctly set the prototype chain up of any object.</p>

<h2>Putting It To Good Use</h2>

<p>Let&rsquo;s consider two distinct problems and see if we can apply our new found knowledge to solve them. In functional programming, the <code>map</code> function is amongst the most used functions, not only in and of itself, but also as a building block for other higher-order functions. <code>map</code> allows you to apply a function to <em>every</em> element in a collection, thereby <em>transforming</em> each element in the collection, and returns a new collection with the transformed elements. Needless to say, the returned collection has the same size as the one supplied to <code>map</code>. We do this all the time in our Java code, albeit in an iterative fashion. Consider the Java code listed in <a href="#Listing_GAN-5">Listing 5</a>.</p>

<p><a name="Listing_GAN-5"></a></p>

<figure class='code'><figcaption><span>Listing 5 - Java map implementation </span></figcaption>
 <div class="highlight"><table><tr><td class="gutter"><pre class="line-numbers"><span class='line-number'>1</span>
<span class='line-number'>2</span>
<span class='line-number'>3</span>
<span class='line-number'>4</span>
<span class='line-number'>5</span>
<span class='line-number'>6</span>
<span class='line-number'>7</span>
<span class='line-number'>8</span>
<span class='line-number'>9</span>
<span class='line-number'>10</span>
<span class='line-number'>11</span>
</pre></td><td class='code'><pre><code class='java'><span class='line'><span class="n">List</span><span class="o">&lt;</span><span class="n">String</span><span class="o">&gt;</span> <span class="n">names</span> <span class="o">=</span> <span class="k">new</span> <span class="n">ArrayList</span><span class="o">&lt;</span><span class="n">String</span><span class="o">&gt;(</span><span class="mi">3</span><span class="o">)</span> <span class="o">{</span> <span class="o">{</span>
</span><span class='line'>    <span class="n">add</span><span class="o">(</span><span class="s">&quot;brendan&quot;</span><span class="o">);</span>
</span><span class='line'>    <span class="n">add</span><span class="o">(</span><span class="s">&quot;douglas&quot;</span><span class="o">);</span>
</span><span class='line'>    <span class="n">add</span><span class="o">(</span><span class="s">&quot;john&quot;</span><span class="o">);</span>
</span><span class='line'><span class="o">}</span> <span class="o">};</span>
</span><span class='line'><span class="n">List</span><span class="o">&lt;</span><span class="n">String</span><span class="o">&gt;</span> <span class="n">transformedNames</span> <span class="o">=</span> <span class="k">new</span> <span class="n">ArrayList</span><span class="o">&lt;</span><span class="n">String</span><span class="o">&gt;(</span><span class="n">names</span><span class="o">.</span><span class="na">size</span><span class="o">());</span>
</span><span class='line'>
</span><span class='line'><span class="c1">// perform the map</span>
</span><span class='line'><span class="k">for</span><span class="o">(</span><span class="n">String</span> <span class="nl">name:</span> <span class="n">names</span><span class="o">)</span> <span class="o">{</span>
</span><span class='line'>    <span class="n">transformedNames</span><span class="o">.</span><span class="na">add</span><span class="o">(</span><span class="n">name</span><span class="o">.</span><span class="na">toUpperCase</span><span class="o">());</span>
</span><span class='line'><span class="o">}</span>
</span></code></pre></td></tr></table></div></figure>


<p>As you can see from <a href="#Listing_GAN-5">Listing 5</a>, we apply the <code>toUpperCase</code> method to every element in the <code>names</code> list and add it to <code>transformedNames</code>. After this code executes, <code>transformedNames</code> will contain exactly three names, all uppercase.</p>

<p>If we were to do this in JavaScript, one approach would be to do as shown in <a href="#Listing_GAN-6">Listing 6</a>.</p>

<p><a name="Listing_GAN-6"></a></p>

<figure class='code'><figcaption><span>Listing 6 - Naive JavaScript map implementation </span></figcaption>
 <div class="highlight"><table><tr><td class="gutter"><pre class="line-numbers"><span class='line-number'>1</span>
<span class='line-number'>2</span>
<span class='line-number'>3</span>
<span class='line-number'>4</span>
<span class='line-number'>5</span>
<span class='line-number'>6</span>
<span class='line-number'>7</span>
<span class='line-number'>8</span>
<span class='line-number'>9</span>
<span class='line-number'>10</span>
<span class='line-number'>11</span>
<span class='line-number'>12</span>
<span class='line-number'>13</span>
</pre></td><td class='code'><pre><code class='javascript'><span class='line'> <span class="kd">function</span> <span class="nx">map</span><span class="p">(</span><span class="nx">coll</span><span class="p">,</span> <span class="nx">f</span><span class="p">)</span> <span class="p">{</span>
</span><span class='line'>     <span class="kd">var</span> <span class="nx">ret</span> <span class="o">=</span> <span class="p">[];</span>
</span><span class='line'>     <span class="k">for</span><span class="p">(</span><span class="kd">var</span> <span class="nx">i</span> <span class="o">=</span> <span class="mi">0</span><span class="p">;</span> <span class="nx">i</span> <span class="o">&lt;</span> <span class="nx">coll</span><span class="p">.</span><span class="nx">length</span><span class="p">;</span> <span class="nx">i</span><span class="o">++</span><span class="p">)</span> <span class="p">{</span>
</span><span class='line'>         <span class="nx">ret</span><span class="p">.</span><span class="nx">push</span><span class="p">(</span><span class="nx">f</span><span class="p">(</span><span class="nx">coll</span><span class="p">[</span><span class="nx">i</span><span class="p">]));</span>
</span><span class='line'>     <span class="p">}</span>
</span><span class='line'>     <span class="k">return</span> <span class="nx">ret</span><span class="p">;</span>
</span><span class='line'> <span class="p">}</span>
</span><span class='line'>
</span><span class='line'> <span class="c1">// just wrap toUpperCase. You can use apply instead</span>
</span><span class='line'> <span class="c1">// takes a single arg and uppercases it</span>
</span><span class='line'> <span class="kd">function</span> <span class="nx">myToUpperCase</span><span class="p">(</span><span class="nx">str</span><span class="p">)</span> <span class="p">{</span> <span class="k">return</span> <span class="nx">str</span><span class="p">.</span><span class="nx">toUpperCase</span><span class="p">();</span> <span class="p">}</span>
</span><span class='line'>
</span><span class='line'> <span class="nx">map</span><span class="p">([</span><span class="s2">&quot;brendan&quot;</span><span class="p">,</span> <span class="s2">&quot;douglas&quot;</span><span class="p">,</span> <span class="s2">&quot;john&quot;</span><span class="p">],</span> <span class="nx">myToUpperCase</span><span class="p">);</span> <span class="c1">// return [&quot;BRENDAN&quot;, &quot;DOUGLAS&quot;, &quot;JOHN&quot;]</span>
</span></code></pre></td></tr></table></div></figure>


<p>Notice that our <code>map</code> function takes another function as its argument, and for each element calls <code>f</code> passing it the n<sup>th</sup> element of the array and pushes it on to the return array.</p>

<p>If you were to deliberate on this for a moment, it does seem a little smelly. The fact that <code>map</code> has to iterate over the collection seems like feature envy. What if we could have a <code>map</code> method on <code>Array</code> that would accept the <em>transforming</em> function and we could invoke that on any array? That would get rid of the smell. Think about what we discussed and see if you can figure out how to pull that off (before taking a look at <a href="#Listing_GAN-7">Listing 7</a>).</p>

<p><a name="Listing_GAN-7"></a></p>

<figure class='code'><figcaption><span>Listing 7 - map implementation using prototype </span></figcaption>
 <div class="highlight"><table><tr><td class="gutter"><pre class="line-numbers"><span class='line-number'>1</span>
<span class='line-number'>2</span>
<span class='line-number'>3</span>
<span class='line-number'>4</span>
<span class='line-number'>5</span>
<span class='line-number'>6</span>
<span class='line-number'>7</span>
<span class='line-number'>8</span>
<span class='line-number'>9</span>
<span class='line-number'>10</span>
<span class='line-number'>11</span>
</pre></td><td class='code'><pre><code class='javascript'><span class='line'><span class="nb">Array</span><span class="p">.</span><span class="nx">prototype</span><span class="p">.</span><span class="nx">map</span> <span class="o">=</span> <span class="kd">function</span><span class="p">(</span><span class="nx">f</span><span class="p">)</span> <span class="p">{</span>
</span><span class='line'>    <span class="kd">var</span> <span class="nx">ret</span> <span class="o">=</span> <span class="p">[];</span>
</span><span class='line'>    <span class="k">for</span><span class="p">(</span><span class="kd">var</span> <span class="nx">i</span> <span class="o">=</span> <span class="mi">0</span><span class="p">;</span> <span class="nx">i</span> <span class="o">&lt;</span> <span class="k">this</span><span class="p">.</span><span class="nx">length</span><span class="p">;</span> <span class="nx">i</span><span class="o">++</span><span class="p">)</span> <span class="p">{</span>
</span><span class='line'>        <span class="nx">ret</span><span class="p">.</span><span class="nx">push</span><span class="p">(</span><span class="nx">f</span><span class="p">(</span><span class="k">this</span><span class="p">[</span><span class="nx">i</span><span class="p">]));</span>
</span><span class='line'>    <span class="p">}</span>
</span><span class='line'>    <span class="k">return</span> <span class="nx">ret</span><span class="p">;</span>
</span><span class='line'><span class="p">}</span>
</span><span class='line'>
</span><span class='line'><span class="kd">function</span> <span class="nx">myToUpperCase</span><span class="p">(</span><span class="nx">str</span><span class="p">)</span> <span class="p">{</span> <span class="k">return</span> <span class="nx">str</span><span class="p">.</span><span class="nx">toUpperCase</span><span class="p">();</span> <span class="p">}</span>
</span><span class='line'>
</span><span class='line'><span class="p">[</span><span class="s2">&quot;brendan&quot;</span><span class="p">,</span> <span class="s2">&quot;douglas&quot;</span><span class="p">,</span> <span class="s2">&quot;john&quot;</span><span class="p">].</span><span class="nx">map</span><span class="p">(</span><span class="nx">myToUpperCase</span><span class="p">);</span> <span class="c1">// return [&quot;BRENDAN&quot;, &quot;DOUGLAS&quot;, &quot;JOHN&quot;]</span>
</span></code></pre></td></tr></table></div></figure>


<p>We know that when we invoke a method on any object, JavaScript will go through the prototype chain until it finds the method or reaches <code>Object.prototype</code> and bails. All we have to do is get a handle to the <code>__proto__</code> object of <code>Array</code>, or alternatively ask the <code>Array</code> function for its <code>prototype</code> (which is what we are doing in <a href="#Listing_GAN-7">Listing 7</a>) and tack on the <code>map</code> method. Done! Now, <em>all</em> arrays are blessed with a <code>map</code> method. Note that within the <code>for</code> loop in <a href="#Listing_GAN-7">Listing 7</a>, we refer to the array itself as <code>this</code> &ndash; since JavaScript is invoking the <code>map</code> method on an instance of an <code>Array</code>.</p>

<p>Consider another scenario. Let&rsquo;s say that you wanted to keep tabs on how many <code>Person</code> objects were created during the execution of a program. The way we would do this in Java would be to create a <code>static</code> variable &ndash; essentially a variable that is the same across all instances of a class and increment it every time the <code>Person</code> constructor was invoked. How can we do that in JavaScript? Well, we know we have constructor functions that would be invoked with the <code>new</code> keyword, so that would be a good place to increment the counter. But where do we store the variable itself? Doing a <code>this.counter++</code> won&rsquo;t work because <code>this</code> within the context of a constructor function refers to the newly created object, making <code>counter</code> an <em>instance</em> variable.</p>

<p>But we also know that functions themselves are objects and can have properties of their own. Can we somehow make use of that? Take a look at <a href="#Listing_GAN-8">Listing 8</a>.</p>

<p><a name="Listing_GAN-8"></a></p>

<figure class='code'><figcaption><span>Listing 8 - Implementing static variables </span></figcaption>
 <div class="highlight"><table><tr><td class="gutter"><pre class="line-numbers"><span class='line-number'>1</span>
<span class='line-number'>2</span>
<span class='line-number'>3</span>
<span class='line-number'>4</span>
<span class='line-number'>5</span>
<span class='line-number'>6</span>
<span class='line-number'>7</span>
<span class='line-number'>8</span>
<span class='line-number'>9</span>
<span class='line-number'>10</span>
<span class='line-number'>11</span>
<span class='line-number'>12</span>
<span class='line-number'>13</span>
<span class='line-number'>14</span>
<span class='line-number'>15</span>
<span class='line-number'>16</span>
</pre></td><td class='code'><pre><code class='javascript'><span class='line'><span class="kd">function</span> <span class="nx">Person</span><span class="p">(</span><span class="nx">name</span><span class="p">)</span> <span class="p">{</span>
</span><span class='line'>    <span class="c1">// equivalent to saying Person.counter</span>
</span><span class='line'>    <span class="c1">// but more idiomatic</span>
</span><span class='line'>    <span class="k">this</span><span class="p">.</span><span class="nx">constructor</span><span class="p">.</span><span class="nx">counter</span><span class="o">++</span><span class="p">;</span>
</span><span class='line'>    <span class="k">this</span><span class="p">.</span><span class="nx">name</span> <span class="o">=</span> <span class="nx">name</span><span class="p">;</span>
</span><span class='line'><span class="p">}</span>
</span><span class='line'>
</span><span class='line'><span class="c1">// init &#39;static&#39; variable</span>
</span><span class='line'><span class="nx">Person</span><span class="p">.</span><span class="nx">counter</span> <span class="o">=</span> <span class="mi">0</span><span class="p">;</span>
</span><span class='line'>
</span><span class='line'><span class="nx">brendan</span> <span class="o">=</span> <span class="k">new</span> <span class="nx">Person</span><span class="p">(</span><span class="s2">&quot;Brendan&quot;</span><span class="p">);</span>
</span><span class='line'><span class="nx">douglas</span> <span class="o">=</span> <span class="k">new</span> <span class="nx">Person</span><span class="p">(</span><span class="s2">&quot;Douglas&quot;</span><span class="p">);</span>
</span><span class='line'>
</span><span class='line'><span class="nx">console</span><span class="p">.</span><span class="nx">log</span><span class="p">(</span><span class="nx">Person</span><span class="p">.</span><span class="nx">counter</span><span class="p">);</span> <span class="c1">// returns 2</span>
</span><span class='line'><span class="c1">// alternatively</span>
</span><span class='line'><span class="nx">console</span><span class="p">.</span><span class="nx">log</span><span class="p">(</span><span class="nx">brendan</span><span class="p">.</span><span class="nx">constructor</span><span class="p">.</span><span class="nx">counter</span><span class="p">);</span> <span class="c1">// also returns 2</span>
</span></code></pre></td></tr></table></div></figure>


<p>If we were to glance back at <a href="#Figure_GAN-3">Figure 3</a> you will notice that <code>Person.prototype</code> has a <code>constructor</code> property that points back to the its function (<code>Person</code> in this case). Within the context of the constructor function, <code>this</code> is the newly created object, which has its <code>__proto__</code> property set to <code>Person.prototype</code>. When we ask <code>this</code> (for e.g <code>brendan</code>) for its <code>constructor</code> property, JavaScript looks within the newly created object and does not find it. So it does what we expect it to do &ndash; follow the <code>__proto__</code> property up to <code>Person.prototype</code> which <em>does</em> have it, and hands us back a pointer to the <code>Person</code> function. We then ask it for its <code>counter</code> property (which we have initialized to 0) and increment it. <a href="#Figure_GAN-4">Figure 4</a> illustrates the state of the system after <a href="#Listing_GAN-8">Listing 8</a> finishes execution (note that I am not showing <code>Object</code> only to avoid clutter &ndash; that hierarchy remains the same in previous illustrations).</p>

<p><a name="Figure_GAN-4"></a>
<img src="http://looselytyped.com/images/2012-08-22-on-prototypal-inheritance-part-ii/Figure_GAN-9_Color.png" width="632" height="227" title="Intercepting the prototype chain" ></p>

<p>Of course, we could have just said <code>Person.counter</code> and get the same desired effect, but there might be times when you don&rsquo;t know the constructor function of an object, so having the <code>constructor</code> property can prove to be really handy.</p>

<h2>Words of Caution</h2>

<p>Before we conclude this article, I would like to emphasize a few key points. Although <code>__proto__</code> is a handy property for testing and illustrative purposes (in FireFox and Chrome), by no means should you rely on using (or even assume the existence of) this property in production code. The idiomatic way to getting the prototype of any object is to use the <code>getPrototypeOf</code> method supplied on <code>Object</code> (e.g. <code>Object.getPrototypeOf(brendan)</code>).</p>

<p>We have been naming our <em>constructor</em> functions (like <code>Person</code> and <code>Mammal</code>) with a capital &ldquo;P&rdquo; (and &ldquo;M&rdquo;) vs. lowercase letters as is the norm for function names in JavaScript. The reason for this is to <em>highlight</em> the fact that they are, in fact, constructors, and <strong>should</strong> be invoked with the <code>new</code> keyword. Not doing so will result in bad things happening. Constructor functions, as we noted earlier, do not have an explicit &ldquo;return&rdquo;, and often refer to <code>this</code> in their function bodies. What does it mean if we were to invoke a constructor function without the new keyword? Let&rsquo;s take a quick look in <a href="#Listing_GAN-9">Listing 9</a>.</p>

<p><a name="Listing_GAN-9"></a></p>

<figure class='code'><figcaption><span>Listing 9 - Calling a constructor function improperly </span></figcaption>
 <div class="highlight"><table><tr><td class="gutter"><pre class="line-numbers"><span class='line-number'>1</span>
<span class='line-number'>2</span>
<span class='line-number'>3</span>
<span class='line-number'>4</span>
<span class='line-number'>5</span>
<span class='line-number'>6</span>
<span class='line-number'>7</span>
<span class='line-number'>8</span>
<span class='line-number'>9</span>
<span class='line-number'>10</span>
<span class='line-number'>11</span>
<span class='line-number'>12</span>
<span class='line-number'>13</span>
</pre></td><td class='code'><pre><code class='javascript'><span class='line'> <span class="kd">function</span> <span class="nx">BadlyInvoked</span><span class="p">(</span><span class="nx">villianiousArg</span><span class="p">)</span> <span class="p">{</span>
</span><span class='line'>     <span class="k">this</span><span class="p">.</span><span class="nx">villianiousProperty</span> <span class="o">=</span> <span class="nx">villianiousArg</span><span class="p">;</span>
</span><span class='line'>     <span class="k">this</span><span class="p">.</span><span class="nx">villianiousFunction</span> <span class="o">=</span> <span class="kd">function</span><span class="p">()</span> <span class="p">{</span>
</span><span class='line'>         <span class="k">return</span> <span class="s2">&quot;Muhhhaahaa!&quot;</span><span class="p">;</span>
</span><span class='line'>     <span class="p">}</span>
</span><span class='line'> <span class="p">}</span>
</span><span class='line'>
</span><span class='line'> <span class="kd">var</span> <span class="nx">villian</span> <span class="o">=</span> <span class="nx">BadlyInvoked</span><span class="p">(</span><span class="s2">&quot;The Joker&quot;</span><span class="p">);</span>
</span><span class='line'> <span class="nx">console</span><span class="p">.</span><span class="nx">log</span><span class="p">(</span><span class="nx">villian</span><span class="p">);</span>  <span class="c1">// evaluates to undefined</span>
</span><span class='line'>
</span><span class='line'> <span class="c1">// global domination</span>
</span><span class='line'> <span class="nx">console</span><span class="p">.</span><span class="nx">log</span><span class="p">(</span><span class="nx">villianiousProperty</span><span class="p">);</span> <span class="c1">// no object context</span>
</span><span class='line'> <span class="nx">console</span><span class="p">.</span><span class="nx">log</span><span class="p">(</span><span class="nx">villianiousFunction</span><span class="p">());</span> <span class="c1">// nor here</span>
</span></code></pre></td></tr></table></div></figure>


<p>Notice that you get no errors whatsoever. JavaScript happily chugs along executing the code, but <code>villian</code> evaluates to <code>undefined</code>. This is because <code>BadlyInvoked</code> does not <code>return</code> anything (and by default, JavaScript returns <code>undefined</code>). Furthermore, you can get to <code>villianiousProperty</code> and <code>villianousFunction</code> without a reference to <code>villian</code>. What happened was upon invocation, <code>BadlyInvoked</code>, did <strong>not</strong> create a new object, rather it acted as any other function would (remember that if you use the &lsquo;Function Invocation&rsquo; pattern, that is invoke a function as a standalone, <code>this</code> is implicitly the &ldquo;global object&rdquo;). Therefore, <code>this</code> refers to the global object, which in the browser environment happens to be the <code>window</code> object. We just accidentally introduced two new properties on the <code>window</code> object. If these had been named anything less sinister (e.g. <code>name</code>) you would have just clobbered the global <code>name</code> property! Not to mention that your code will begin to throw all kinds of errors if you attempt to use <code>villian</code> in any way, because it <em>is</em> <code>undefined</code>!</p>

<p>Naming constructor functions with capital letters <em>is</em> idiomatic JavaScript, and the preferred convention as a way to alert fellow developers that this function <em>should</em> be invoked with the <code>new</code> keyword.</p>

<h2>Conclusion</h2>

<p>Phew! We have reached the end of our whirlwind tour inside JavaScript&rsquo;s inheritance strategy and implementation. Although some of this may seem a little overwhelming, and perhaps even a tad confusing, hopefully I have given you a clear guide to a better &ldquo;understanding&rdquo; of the language and implementing your own inheritance hierarchies within JavaScript.</p>

<p>JavaScript is truly a powerful language. Couple prototypal inheritance with JavaScript&rsquo;s dynamic nature (adding, removing and changing behavior at runtime), and you are one step closer to unleashing the power to write idiomatic, clean and most importantly, re-usable code within your applications.</p>
]]></content>
  </entry>
  
  <entry>
    <title type="html"><![CDATA[On Prototypal Inheritance]]></title>
    <link href="http://looselytyped.com/blog/2012/08/18/on-prototypal-inheritance/"/>
    <updated>2012-08-18T16:44:00-04:00</updated>
    <id>http://looselytyped.com/blog/2012/08/18/on-prototypal-inheritance</id>
    <content type="html"><![CDATA[<p><em>This is an article I wrote for <a href="https://www.nofluffjuststuff.com/home/magazine_subscribe">NFJS, The Magazine</a>&rsquo;s <a href="https://www.nofluffjuststuff.com/home/magazine_subscribe?id=32">April, 2012</a> issue. This is a 2-part series, this being the first one &ndash; I will post the second one soon. Stay tuned!</em></p>

<p><em>Douglas Crockford refers to JavaScript as &ldquo;Lisp in C&rsquo;s Clothing&rdquo; which gives us an inkling to it&rsquo;s true power. Unfortunately JavaScript, a language that started with a rushed schedule to production and a specification that was buffeted with strong political winds at it&rsquo;s inception, has often been &ldquo;misunderstood&rdquo;. Despite it&rsquo;s unfortunate beginnings, amidst the stormy waters laden with many a pitfall like global variables, lie a few pearls of wisdom, of which one is prototypal inheritance. In this 2-series article we will take a deep dive into JavaScript&rsquo;s prototypal nature &ndash; we will see how it works, and more importantly how we, as JavaScript developers can leverage it. We will take it a step further to see how we can develop our own hierarchies to model our code, allowing for better reuse.</em></p>

<!-- more -->


<p>Gone are the days when we, as developers could get away with one-off scripts to power our web pages. With the advent of rich, and highly interactive web applications, more and more &ldquo;core&rdquo; logic has shifted to the client making JavaScript a first class language within our code-base, and warranting such treatment. Developers are constantly looking to better model their code to reflect real world scenarios, in which inheritance plays an important role. JavaScript does provides you the ability to create rich and deep hierarchies, but does so in a fashion that is not immediately obvious to those coming from a language like Java. To better understand how we can use this valuable feature, we will need to dig past the surface, and examine how JavaScript uses objects, and functions in unison to make this happen.</p>

<p>I highly recommend that you play along with the code as you read this, but you will need a runtime. I suggest you use Google Chrome since it comes bundled with a JavaScript console. Just open up a new tab, right-click anywhere on the page, and select &ldquo;Inspect Element&rdquo;. A small window should pop open at the bottom of the page, with it&rsquo;s own toolbar. The last item on the tool bar is called &ldquo;Console&rdquo; &ndash; Clicking that will bring you in to the JavaScript console. Another suggestion is to click the icon at the bottom-left of the JavaScript console (The tooltip says &ldquo;Undock into a seperate window.&rdquo;) &ndash; This detaches the Code Inspector from the main window and should give you much more real estate to play with^<a href="#Footnote-1">1</a>  .</p>

<p>Ready? Let&rsquo;s go find us some inheritance &hellip;</p>

<h2>Objects</h2>

<p>   Fundamentally, JavaScript is object-oriented. To those familiar with JavaScript this should come as no surprise. JavaScript comes bundled with a few &ldquo;global&rdquo; objects, such as Array, Boolean, Date, String and Object. Instances of these are adorned with certain properties, and behaviors, such as <code>[].length</code> (which is the <code>length</code> of an array), or <code>"javascript".toUpperCase()</code> (which returns <code>"JAVASCRIPT"</code>).
   There is a subtle, yet important distinction here that I must highlight, and that will be relevant to our on-going discussion. <code>[].length</code> is merely looking up the value of a property in the array, much like you would look up a <code>public</code> variable on an object in Java. On the other hand, <code>"javascript".toUpperCase()</code> is <em>invoking</em> a method on the <code>String</code> instance. This pattern of invocation is referred to as the &ldquo;Method Invocation Pattern&rdquo;, in that you are invoking a function as a method of an object^<a href="#Footnote-2">2</a>  .</p>

<p>   There is another way to invoke a function, as shown in <a href="#Listing_GAN-1">Listing 1</a></p>

<p><a name="Listing_GAN-1"></a></p>

<figure class='code'><figcaption><span>Listing 1 - Function invocation pattern </span></figcaption>
 <div class="highlight"><table><tr><td class="gutter"><pre class="line-numbers"><span class='line-number'>1</span>
<span class='line-number'>2</span>
<span class='line-number'>3</span>
<span class='line-number'>4</span>
<span class='line-number'>5</span>
</pre></td><td class='code'><pre><code class='javascript'><span class='line'><span class="kd">function</span> <span class="nx">sayHelloTo</span><span class="p">(</span><span class="nx">name</span><span class="p">)</span> <span class="p">{</span>
</span><span class='line'>    <span class="k">return</span> <span class="s2">&quot;Hello &quot;</span> <span class="o">+</span> <span class="nx">name</span> <span class="o">+</span> <span class="s2">&quot;!&quot;</span><span class="p">;</span>
</span><span class='line'><span class="p">}</span>
</span><span class='line'>
</span><span class='line'><span class="nx">console</span><span class="p">.</span><span class="nx">log</span><span class="p">(</span><span class="nx">sayHelloTo</span><span class="p">(</span><span class="s2">&quot;Brendan&quot;</span><span class="p">));</span> <span class="c1">// evaluates to &quot;Hello Brendan!&quot;</span>
</span></code></pre></td></tr></table></div></figure>


<p>   This is called the &ldquo;Function Invocation&rdquo; pattern because it seems that you are invoking a stand-alone function. This is not entirely correct &ndash; rather the function is attached to the &ldquo;global&rdquo; object, which in the case of your browser is the <code>window</code> object.</p>

<h3>Creating your own objects</h3>

<p>  JavaScript allows you to create your own objects, and offers several ways to do so, each offering something subtly different. The easiest way to create an object is to use the literal syntax &ndash; <code>{}</code>. You can then attach properties and methods to this object, and you can look these properties up, or invoke these methods just like you would any JavaScript supplied object. Let&rsquo;s take a look in <a href="#Listing_GAN-2">Listing 2</a></p>

<p><a name="Listing_GAN-2"></a></p>

<figure class='code'><figcaption><span>Listing 2 - Adorning objects </span></figcaption>
 <div class="highlight"><table><tr><td class="gutter"><pre class="line-numbers"><span class='line-number'>1</span>
<span class='line-number'>2</span>
<span class='line-number'>3</span>
<span class='line-number'>4</span>
<span class='line-number'>5</span>
<span class='line-number'>6</span>
<span class='line-number'>7</span>
<span class='line-number'>8</span>
<span class='line-number'>9</span>
<span class='line-number'>10</span>
</pre></td><td class='code'><pre><code class='javascript'><span class='line'> <span class="kd">var</span> <span class="nx">myObj</span> <span class="o">=</span> <span class="p">{};</span>
</span><span class='line'>
</span><span class='line'> <span class="nx">myObj</span><span class="p">.</span><span class="nx">name</span> <span class="o">=</span> <span class="s2">&quot;Some Object&quot;</span><span class="p">;</span>
</span><span class='line'> <span class="nx">myObj</span><span class="p">.</span><span class="nx">sayHello</span> <span class="o">=</span> <span class="kd">function</span><span class="p">(</span><span class="nx">name</span><span class="p">)</span> <span class="p">{</span>
</span><span class='line'>     <span class="k">return</span> <span class="s2">&quot;Hello &quot;</span> <span class="o">+</span> <span class="nx">name</span> <span class="o">+</span> <span class="s2">&quot;!&quot;</span><span class="p">;</span>
</span><span class='line'> <span class="p">};</span>
</span><span class='line'>
</span><span class='line'> <span class="c1">// try it out</span>
</span><span class='line'> <span class="nx">console</span><span class="p">.</span><span class="nx">log</span><span class="p">(</span><span class="nx">myObj</span><span class="p">.</span><span class="nx">name</span><span class="p">);</span> <span class="c1">// evaluates to &quot;Some Object&quot;</span>
</span><span class='line'> <span class="nx">console</span><span class="p">.</span><span class="nx">log</span><span class="p">(</span><span class="nx">myObj</span><span class="p">.</span><span class="nx">sayHello</span><span class="p">(</span><span class="s2">&quot;Brendan&quot;</span><span class="p">));</span> <span class="c1">// evaluates to &quot;Hello Brendan!&quot;</span>
</span></code></pre></td></tr></table></div></figure>


<p>  You can declare an object, and all the properties and methods in one fell swoop, as shown in <a href="#Listing_GAN-3">Listing 3</a></p>

<p><a name="Listing_GAN-3"></a></p>

<figure class='code'><figcaption><span>Listing 3 - Adorning objects in one step </span></figcaption>
 <div class="highlight"><table><tr><td class="gutter"><pre class="line-numbers"><span class='line-number'>1</span>
<span class='line-number'>2</span>
<span class='line-number'>3</span>
<span class='line-number'>4</span>
<span class='line-number'>5</span>
<span class='line-number'>6</span>
<span class='line-number'>7</span>
<span class='line-number'>8</span>
<span class='line-number'>9</span>
<span class='line-number'>10</span>
</pre></td><td class='code'><pre><code class='javascript'><span class='line'><span class="kd">var</span> <span class="nx">myObj</span> <span class="o">=</span> <span class="p">{</span>
</span><span class='line'>    <span class="nx">name</span> <span class="o">:</span> <span class="s2">&quot;Some Object&quot;</span><span class="p">,</span> <span class="c1">// NOTE the : and the &#39;,&#39; at the end</span>
</span><span class='line'>    <span class="nx">sayHello</span> <span class="o">:</span> <span class="kd">function</span><span class="p">(</span><span class="nx">name</span><span class="p">)</span> <span class="p">{</span>
</span><span class='line'>        <span class="k">return</span> <span class="s2">&quot;Hello &quot;</span> <span class="o">+</span> <span class="nx">name</span> <span class="o">+</span> <span class="s2">&quot;!&quot;</span><span class="p">;</span>
</span><span class='line'>    <span class="p">}</span>
</span><span class='line'><span class="p">};</span>
</span><span class='line'>
</span><span class='line'><span class="c1">// try it out ... again</span>
</span><span class='line'><span class="nx">console</span><span class="p">.</span><span class="nx">log</span><span class="p">(</span><span class="nx">myObj</span><span class="p">.</span><span class="nx">name</span><span class="p">);</span> <span class="c1">// evaluates to &quot;Some Object&quot;</span>
</span><span class='line'><span class="nx">console</span><span class="p">.</span><span class="nx">log</span><span class="p">(</span><span class="nx">myObj</span><span class="p">.</span><span class="nx">sayHello</span><span class="p">(</span><span class="s2">&quot;Brendan&quot;</span><span class="p">));</span> <span class="c1">// evaluates to &quot;Hello Brendan!&quot;</span>
</span></code></pre></td></tr></table></div></figure>


<p>  Notice that instead of <code>=</code> signs we use <code>:</code>, and we separate the property definitions using commas as delimiters.</p>

<p>  There is another way to define your own object, and that is to use the <code>create</code> method defined on <code>Object</code>. This method takes an argument, the meaning of which we will see in Part II of this series, but for now a <code>null</code> will suffice^<a href="#Footnote-3">3</a>  . Let&rsquo;s take a look (<a href="#Listing_GAN-4">Listing 4</a>)</p>

<p><a name="Listing_GAN-4"></a></p>

<figure class='code'><figcaption><span>Listing 4 - Using Object.create </span></figcaption>
 <div class="highlight"><table><tr><td class="gutter"><pre class="line-numbers"><span class='line-number'>1</span>
<span class='line-number'>2</span>
<span class='line-number'>3</span>
<span class='line-number'>4</span>
<span class='line-number'>5</span>
<span class='line-number'>6</span>
<span class='line-number'>7</span>
<span class='line-number'>8</span>
<span class='line-number'>9</span>
<span class='line-number'>10</span>
</pre></td><td class='code'><pre><code class='javascript'><span class='line'><span class="kd">var</span> <span class="nx">myObj</span> <span class="o">=</span> <span class="nb">Object</span><span class="p">.</span><span class="nx">create</span><span class="p">(</span><span class="kc">null</span><span class="p">);</span>
</span><span class='line'>
</span><span class='line'><span class="nx">myObj</span><span class="p">.</span><span class="nx">name</span> <span class="o">=</span> <span class="s2">&quot;Some Object&quot;</span><span class="p">;</span>
</span><span class='line'><span class="nx">myObj</span><span class="p">.</span><span class="nx">sayHello</span> <span class="o">=</span> <span class="kd">function</span><span class="p">(</span><span class="nx">name</span><span class="p">)</span> <span class="p">{</span>
</span><span class='line'>    <span class="k">return</span> <span class="s2">&quot;Hello &quot;</span> <span class="o">+</span> <span class="nx">name</span> <span class="o">+</span> <span class="s2">&quot;!&quot;</span><span class="p">;</span>
</span><span class='line'><span class="p">};</span>
</span><span class='line'>
</span><span class='line'><span class="c1">// try it out</span>
</span><span class='line'><span class="nx">console</span><span class="p">.</span><span class="nx">log</span><span class="p">(</span><span class="nx">myObj</span><span class="p">.</span><span class="nx">name</span><span class="p">);</span> <span class="c1">// returns &quot;Some Object&quot;</span>
</span><span class='line'><span class="nx">console</span><span class="p">.</span><span class="nx">log</span><span class="p">(</span><span class="nx">myObj</span><span class="p">.</span><span class="nx">sayHello</span><span class="p">(</span><span class="s2">&quot;Brendan&quot;</span><span class="p">));</span> <span class="c1">// returns &quot;Hello Brendan!&quot;</span>
</span></code></pre></td></tr></table></div></figure>


<p>  Now that we know how to create our own objects, it should be noted that even plain vanilla objects that you create, say with the literal <code>{}</code> syntax, have a few properties and functions magically defined on them. See <a href="#Listing_GAN-5">Listing 5</a>.</p>

<p><a name="Listing_GAN-5"></a></p>

<figure class='code'><figcaption><span>Listing 5 - Viewing hidden properties on objects </span></figcaption>
 <div class="highlight"><table><tr><td class="gutter"><pre class="line-numbers"><span class='line-number'>1</span>
<span class='line-number'>2</span>
<span class='line-number'>3</span>
<span class='line-number'>4</span>
<span class='line-number'>5</span>
<span class='line-number'>6</span>
<span class='line-number'>7</span>
<span class='line-number'>8</span>
<span class='line-number'>9</span>
<span class='line-number'>10</span>
<span class='line-number'>11</span>
<span class='line-number'>12</span>
</pre></td><td class='code'><pre><code class='javascript'><span class='line'> <span class="kd">var</span> <span class="nx">obj</span> <span class="o">=</span> <span class="p">{};</span>
</span><span class='line'>
</span><span class='line'> <span class="nx">console</span><span class="p">.</span><span class="nx">log</span><span class="p">(</span><span class="nx">obj</span><span class="p">.</span><span class="nx">constructor</span><span class="p">);</span> <span class="c1">// returns function Object()</span>
</span><span class='line'> <span class="nx">console</span><span class="p">.</span><span class="nx">log</span><span class="p">(</span><span class="nx">obj</span><span class="p">.</span><span class="nx">toString</span><span class="p">());</span> <span class="c1">// returns [object Object]</span>
</span><span class='line'>
</span><span class='line'> <span class="nx">console</span><span class="p">.</span><span class="nx">log</span><span class="p">(</span><span class="nx">obj</span><span class="p">.</span><span class="nx">nonExistentProperty</span><span class="p">);</span> <span class="c1">// returns undefined</span>
</span><span class='line'>
</span><span class='line'> <span class="k">try</span> <span class="p">{</span>
</span><span class='line'>     <span class="nx">console</span><span class="p">.</span><span class="nx">log</span><span class="p">(</span><span class="nx">obj</span><span class="p">.</span><span class="nx">nonExistingMethod</span><span class="p">());</span>
</span><span class='line'> <span class="p">}</span> <span class="k">catch</span> <span class="p">(</span><span class="nx">e</span><span class="p">)</span> <span class="p">{</span>
</span><span class='line'>     <span class="nx">console</span><span class="p">.</span><span class="nx">log</span><span class="p">(</span><span class="s2">&quot;Oops! &quot;</span> <span class="o">+</span> <span class="nx">e</span><span class="p">);</span>
</span><span class='line'> <span class="p">}</span>
</span></code></pre></td></tr></table></div></figure>


<p>  As you can see, somehow our <code>obj</code> has a <code>constructor</code> property and a <code>toString</code> function. Just to prove they are the real deal, I invoked the <code>nonExistingProperty</code> property (which returns <code>undefined</code>) and invoked a <code>nonExistingMethod</code> that simply blows up! Where did the <code>toString</code> property come from? To answer that, add one more line to the script you ran in <a href="#Listing_GAN-5">Listing 5</a>, as shown in <a href="#Listing_GAN-6">Listing 6</a></p>

<p><a name="Listing_GAN-6"></a></p>

<figure class='code'><figcaption><span>Listing 6 - Seeing the __proto__ object </span></figcaption>
 <div class="highlight"><table><tr><td class="gutter"><pre class="line-numbers"><span class='line-number'>1</span>
</pre></td><td class='code'><pre><code class='javascript'><span class='line'><span class="nx">console</span><span class="p">.</span><span class="nx">log</span><span class="p">(</span><span class="nx">obj</span><span class="p">.</span><span class="nx">__proto__</span><span class="p">);</span>
</span></code></pre></td></tr></table></div></figure>


<p><caption>Listing 6 &ndash; Seeing the <code>__proto__</code> object</caption></p>

<p>  Within Chrome, the final line should show look like the screenshot I have captured in <a href="#Figure_GAN-1">Figure 1</a> (Unfortunately there isn&rsquo;t a way to show the properties of the object within Firefox &ndash; All you will see is <code>Object { ... }</code>)</p>

<p><a name="Figure_GAN-1"></a>
<img src="http://looselytyped.com/images/2012-08-18-on-prototypal-inheritance/Figure_GAN-1_Color.png" title="Figure 1 - Object.prototype" ></p>

<p>  What just happened here, and what is that <code>Object</code> being shown in the console? Well, let&rsquo;s take it a step at a time. <code>obj.__proto__</code> is (in our example) equivalent to <code>{}.__proto__</code>. Essentially, we are asking our object for its <code>__proto__</code> object. You see, <em>every</em> object in JavaScript has a &ldquo;prototype&rdquo; &ndash; This &ldquo;prototype&rdquo;, is just that &ndash; an object that acts as the template for any new object. This is where the &ldquo;prototypal&rdquo; in &ldquo;prototypal inheritance&rdquo; comes from &ndash; rather than having classes, we simply &ldquo;clone&rdquo; existing objects to allow for behavior reuse and inheritance.</p>

<p>  This is very different from the class based inheritance many of us are used to in languages like Java. Rather than have two completely different animals &ndash; classes and objects like you do in Java (Yes, although classes are objects at runtime you have to admit they have special powers :D), within JavaScript you have only objects. Objects point to other objects and share behavior!</p>

<p>  The relationship between our <code>obj</code> from <a href="#Listing_GAN-5">Listing 5</a> and it&rsquo;s <code>__proto__</code> is illustrated in <a href="#Figure_GAN-2">Figure 2</a></p>

<p><a name="Figure_GAN-2"></a>
<img src="http://looselytyped.com/images/2012-08-18-on-prototypal-inheritance/Figure_GAN-2_Color.png" width="476" height="504" title="Figure 2 - {}.__proto__" ></p>

<p>  Notice that I have marked our <code>obj</code>&rsquo;s <code>__proto__</code> as <code>Object.prototype</code>. The reason for this will become clear after we finish the section on Functions, but from now on, I will refer to it as just that &ndash; <code>Object.prototype</code>.</p>

<p>  Perhaps you see how method look up works in JavaScript now. When you asked for <code>obj.toString()</code>, JavaScript looked in our newly created object to see if it had a <code>toString</code> function. Needless to say, it didn&rsquo;t find it, so then JavaScript follows the <code>__proto__</code> pointer up to the <code>Object</code> which does have a <code>toString</code>! So JavaScript merely calls that function and returns. This is also the same reason why <code>nonExistentProperty</code> and <code>nonExistentMethod</code> both failed &ndash; neither our object nor it&rsquo;s <code>__proto__</code> have any such properties. Furthermore, <code>Object.prototype</code> does not have a <code>__proto__</code> property &ndash; so this is the end of the line for the lookup chain.</p>

<p>  Now that you know this, can you think of a way to write a custom <code>toString</code> method for our newly created <code>obj</code> object? It&rsquo;s not that much different from Java. See <a href="#Listing_GAN-7">Listing 7</a></p>

<p><a name="Listing_GAN-7"></a></p>

<figure class='code'><figcaption><span>Listing 7 - Adding a instance toString method </span></figcaption>
 <div class="highlight"><table><tr><td class="gutter"><pre class="line-numbers"><span class='line-number'>1</span>
<span class='line-number'>2</span>
<span class='line-number'>3</span>
<span class='line-number'>4</span>
<span class='line-number'>5</span>
<span class='line-number'>6</span>
<span class='line-number'>7</span>
<span class='line-number'>8</span>
<span class='line-number'>9</span>
</pre></td><td class='code'><pre><code class='javascript'><span class='line'> <span class="c1">// declaring a new object because it&#39;s convenient</span>
</span><span class='line'> <span class="c1">// you can use the code in Listing GAN-5 and Listing GAN-6 if you like</span>
</span><span class='line'> <span class="kd">var</span> <span class="nx">obj</span> <span class="o">=</span> <span class="p">{};</span>
</span><span class='line'>
</span><span class='line'> <span class="nx">obj</span><span class="p">.</span><span class="nx">toString</span> <span class="o">=</span> <span class="kd">function</span><span class="p">()</span> <span class="p">{</span>
</span><span class='line'>     <span class="k">return</span> <span class="s2">&quot;I am a newly created object!&quot;</span><span class="p">;</span>
</span><span class='line'> <span class="p">}</span>
</span><span class='line'>
</span><span class='line'> <span class="nx">console</span><span class="p">.</span><span class="nx">log</span><span class="p">(</span><span class="nx">obj</span><span class="p">.</span><span class="nx">toString</span><span class="p">());</span> <span class="c1">// evaluates to &quot;I am a newly created object!&quot;</span>
</span></code></pre></td></tr></table></div></figure>


<p>  What did we just do? We introduced a new property in our object, namely <code>toString</code>. Now when we ask our <code>obj</code> to evaluate <code>toString</code> JavaScript does not have to go far &ndash; it merely executes the first found implementation of the function, which happens to be the one owned by our newly created object. To see how this works, see <a href="#Figure_GAN-3">Figure 3</a></p>

<p><a name="Figure_GAN-3"></a>
<img src="http://looselytyped.com/images/2012-08-18-on-prototypal-inheritance/Figure_GAN-3_Color.png" width="528" height="486" title="Figure 3 - instance toString implementation" ></p>

<p>  Notice that I have merely stricken out the <code>toString</code> in <code>Object.prototype</code> and not removed it. By putting a newer definition in <code>obj</code> you don&rsquo;t wipe out the one in <code>Object</code>, you merely <em>shadow</em> it. Furthermore, any new objects that are created without a <code>toString</code> implementation still use the one in <code>Object.prototype</code>. This is no different than inheritance you might be used to in Java-land. The top level (Java) <code>Object</code> class offers a default implementation of <code>toString</code>, and sub-classes can override it to suit their needs. The only difference being that in JavaScript there is no <code>class</code> construct &ndash; rather you deal with objects only.</p>

<p>  The discussion so far works out fabulously if you need only one object to work with &ndash; you can add as many custom methods on to this object as you need and be on your merry way. But classes (like those in Java), and inheritance in general buys us a lot more than that &ndash; the ability to have a way to define certain functions and properties for a <em>class</em> of objects in one place, and have all &ldquo;instances&rdquo; automatically be adorned with certain behavior.</p>

<p>  What if we wanted all of our objects to be able to greet other objects? If we were to put a <code>sayHello</code> method in <code>Object.prototype</code>, then any object created with it&rsquo;s <code>__proto__</code> property pointing to <code>Object.prototype</code> will automatically inherit that method. Let&rsquo;s give it a whirl in <a href="#Listing_GAN-8">Listing 8</a></p>

<p><a name="Listing_GAN-8"></a></p>

<figure class='code'><figcaption><span>Listing 8 - Global sayHello method </span></figcaption>
 <div class="highlight"><table><tr><td class="gutter"><pre class="line-numbers"><span class='line-number'>1</span>
<span class='line-number'>2</span>
<span class='line-number'>3</span>
<span class='line-number'>4</span>
<span class='line-number'>5</span>
<span class='line-number'>6</span>
<span class='line-number'>7</span>
<span class='line-number'>8</span>
<span class='line-number'>9</span>
<span class='line-number'>10</span>
<span class='line-number'>11</span>
<span class='line-number'>12</span>
<span class='line-number'>13</span>
<span class='line-number'>14</span>
</pre></td><td class='code'><pre><code class='javascript'><span class='line'> <span class="c1">// declaring a new object because it&#39;s convenient</span>
</span><span class='line'> <span class="c1">// you can use the code in Listing GAN-5 if you like</span>
</span><span class='line'> <span class="kd">var</span> <span class="nx">obj</span> <span class="o">=</span> <span class="p">{};</span>
</span><span class='line'>
</span><span class='line'> <span class="nx">obj</span><span class="p">.</span><span class="nx">__proto__</span><span class="p">.</span><span class="nx">sayHello</span> <span class="o">=</span> <span class="kd">function</span><span class="p">(</span><span class="nx">name</span><span class="p">)</span> <span class="p">{</span>
</span><span class='line'>     <span class="k">return</span> <span class="s2">&quot;Hello &quot;</span> <span class="o">+</span> <span class="nx">name</span> <span class="o">+</span> <span class="s2">&quot;!&quot;</span><span class="p">;</span>
</span><span class='line'> <span class="p">}</span>
</span><span class='line'>
</span><span class='line'> <span class="nx">console</span><span class="p">.</span><span class="nx">log</span><span class="p">(</span><span class="nx">obj</span><span class="p">.</span><span class="nx">sayHello</span><span class="p">(</span><span class="s2">&quot;Brendan&quot;</span><span class="p">));</span> <span class="c1">// evaluates to &quot;Hello Brendan!&quot;</span>
</span><span class='line'>
</span><span class='line'> <span class="c1">// create another object</span>
</span><span class='line'> <span class="kd">var</span> <span class="nx">anotherObj</span> <span class="o">=</span> <span class="p">{};</span>
</span><span class='line'>
</span><span class='line'><span class="nx">console</span><span class="p">.</span><span class="nx">log</span><span class="p">(</span><span class="nx">anotherObj</span><span class="p">.</span><span class="nx">sayHello</span><span class="p">(</span><span class="s2">&quot;Douglas&quot;</span><span class="p">));</span> <span class="c1">// evaluates to &quot;Hello Douglas!&quot;</span>
</span></code></pre></td></tr></table></div></figure>


<p>  When we say <code>obj.__proto__</code> we are getting a reference to the <code>Object.prototype</code> object. We then tack on <code>sayHello</code> on to it, and then create <code>anotherObject</code> (which automatically inherits from <code>Object.prototype</code>). This is illustrated in <a href="#Figure_GAN-4">Figure 4</a></p>

<p><a name="Figure_GAN-4"></a>
<img src="http://looselytyped.com/images/2012-08-18-on-prototypal-inheritance/Figure_GAN-4_Color.png" width="602" height="459" title="Figure GAN-4 - Global sayHello implementation visualized" ></p>

<p>  Problem solved? Not quite. Most of us would be frowning right about now. By introducing the <code>sayHello</code> in the <code>Object.prototype</code> <em>every</em> object has the ability to speak! This includes the objects that JavaScript ships with. Try <code>[].sayHello("John")</code> in your console. Needless to say, this is a sledge-hammer approach to inheritance. No Java programmer worth his salt would (if they could) put this method in <code>Object</code>.</p>

<p>  So what we need is a way to introduce newly created objects with their own &ldquo;prototype&rdquo; object &ndash; one that adorns them with specific behaviors that are applicable to <em>any</em> object of that <em>class</em> or genre. How do we do that?</p>

<p>  That is the question we are here to answer. Before we go there, we need to talk at about functions in JavaScript^<a href="#Footnote-4">4</a>  .</p>

<h2>Functions</h2>

<p>   JavaScript is a functional programming language, in that functions are first-class citizens. This is a departure from languages like Java where functions, or rather methods are underlings to the objects to which they belong. What this means is that functions are like any other datatype in JavaScript &ndash; Just as you would assign a <code>String</code> to a variable or return a <code>Number</code> from a function (or a method), you can do the same with functions. You can assign a function to a variable (and you saw an example of this in <a href="#Listing_GAN-2">Listing 2</a> where we assigned <code>sayHello</code> to a function) and you can pass functions as arguments to other functions. You can even have functions create other functions a.k.a higher-order functions. Let&rsquo;s explore some of these concepts really quickly in <a href="#Listing_GAN-9">Listing 9</a></p>

<p><a name="Listing_GAN-9"></a></p>

<figure class='code'><figcaption><span>Listing 9 - Functional programming </span></figcaption>
 <div class="highlight"><table><tr><td class="gutter"><pre class="line-numbers"><span class='line-number'>1</span>
<span class='line-number'>2</span>
<span class='line-number'>3</span>
<span class='line-number'>4</span>
<span class='line-number'>5</span>
<span class='line-number'>6</span>
<span class='line-number'>7</span>
<span class='line-number'>8</span>
<span class='line-number'>9</span>
<span class='line-number'>10</span>
<span class='line-number'>11</span>
<span class='line-number'>12</span>
<span class='line-number'>13</span>
<span class='line-number'>14</span>
<span class='line-number'>15</span>
<span class='line-number'>16</span>
<span class='line-number'>17</span>
<span class='line-number'>18</span>
<span class='line-number'>19</span>
<span class='line-number'>20</span>
<span class='line-number'>21</span>
<span class='line-number'>22</span>
<span class='line-number'>23</span>
<span class='line-number'>24</span>
</pre></td><td class='code'><pre><code class='javascript'><span class='line'><span class="c1">// extremely contrived example but math operators in</span>
</span><span class='line'><span class="c1">// JS are baked into the language. This is just a simple</span>
</span><span class='line'><span class="c1">// wrapper</span>
</span><span class='line'><span class="c1">// This is NOT idiomatic.</span>
</span><span class='line'><span class="kd">function</span> <span class="nx">add</span><span class="p">(</span><span class="nx">a</span><span class="p">,</span> <span class="nx">b</span><span class="p">)</span> <span class="p">{</span>
</span><span class='line'>    <span class="k">return</span> <span class="nx">a</span> <span class="o">+</span> <span class="nx">b</span><span class="p">;</span>
</span><span class='line'><span class="p">}</span>
</span><span class='line'>
</span><span class='line'><span class="kd">var</span> <span class="nx">somePointerToAdd</span> <span class="o">=</span> <span class="nx">add</span><span class="p">;</span> <span class="c1">// have a variable point to the &#39;add&#39; function</span>
</span><span class='line'><span class="c1">// You can invoke add in multiple ways</span>
</span><span class='line'><span class="nx">console</span><span class="p">.</span><span class="nx">log</span><span class="p">(</span><span class="nx">add</span><span class="p">(</span><span class="mi">2</span><span class="p">,</span> <span class="mi">3</span><span class="p">));</span> <span class="c1">// returns 5</span>
</span><span class='line'><span class="nx">console</span><span class="p">.</span><span class="nx">log</span><span class="p">(</span><span class="nx">somePointerToAdd</span><span class="p">(</span><span class="mi">2</span><span class="p">,</span> <span class="mi">3</span><span class="p">));</span> <span class="c1">// returns 5</span>
</span><span class='line'><span class="nx">console</span><span class="p">.</span><span class="nx">log</span><span class="p">(</span><span class="nx">somePointerToAdd</span> <span class="o">==</span> <span class="nx">add</span><span class="p">);</span> <span class="c1">// prove they are the same - returns true</span>
</span><span class='line'><span class="c1">// This function creates an &quot;incrementor&quot; function and returns.</span>
</span><span class='line'><span class="c1">// Given an arg &#39;n&#39; it returns a NEW function</span>
</span><span class='line'><span class="c1">// that takes just one argument and upon invocation returns &#39;add(n, arg)&#39;</span>
</span><span class='line'><span class="kd">function</span> <span class="nx">incrementBy</span><span class="p">(</span><span class="nx">n</span><span class="p">)</span> <span class="p">{</span>
</span><span class='line'>    <span class="k">return</span> <span class="kd">function</span><span class="p">(</span><span class="nx">arg</span><span class="p">)</span> <span class="p">{</span> <span class="k">return</span> <span class="nx">add</span><span class="p">(</span><span class="nx">n</span><span class="p">,</span> <span class="nx">arg</span><span class="p">)</span> <span class="p">};</span>
</span><span class='line'><span class="p">}</span>
</span><span class='line'>
</span><span class='line'><span class="c1">// essentially returns function(arg) { return add(3, arg); }</span>
</span><span class='line'><span class="kd">var</span> <span class="nx">incrementor</span> <span class="o">=</span> <span class="nx">incrementBy</span><span class="p">(</span><span class="mi">3</span><span class="p">);</span>
</span><span class='line'><span class="c1">// invoke function(arg) { return add(3, arg); } giving it 5 as an arg, returns 8</span>
</span><span class='line'><span class="nx">console</span><span class="p">.</span><span class="nx">log</span><span class="p">(</span><span class="nx">incrementor</span><span class="p">(</span><span class="mi">5</span><span class="p">));</span>
</span></code></pre></td></tr></table></div></figure>


<p>Note that <code>incrementor</code> is a pointer to the newly created function that gets returned by <code>incrementBy(3)</code>. This is no different than saying <code>var x = "Brendan"</code>.</p>

<p>This is a powerful construct, and makes JavaScript an incredibly powerful language, but as is always the case with JavaScript, there is more to it than meets the eye. JavaScript functions, are in fact, full blown objects in and of themselves! If you were to glance at the JavaScript documentation you will see that there is a &lsquo;<a href="https://developer.mozilla.org/en/JavaScript/Reference/Global_Objects/Function">Function</a>&rsquo; object, it succinctly says</p>

<blockquote><p>Every function in JavaScript is actually a Function object.</p><footer><strong>MDN</strong> <cite><a href='https://developer.mozilla.org/en-US/docs/JavaScript/Reference/Global_Objects/Function#Summary'>Function</a></cite></footer></blockquote>


<p> Our earlier discussion about objects, properties, and methods applies to functions as well. That is, functions can have properties and methods assigned to them! Whenever you declare a new function in JavaScript, a new <code>Function</code> object is created, and much like other objects (like we saw in <a href="#Listing_GAN-5">Listing 5</a>) are blessed auto-magically with a few properties, of which, <code>constructor</code> and <code>prototype</code> being the ones relevant to our discussion at hand. You can get a handle to the function object simply by using it&rsquo;s name, like <code>Person</code>. (I noted in <a href="#Footnote-2">2</a> that you could do <code>"javascript".toUpperCase</code>, without the parentheses &ndash; that is essentially giving you back a handle to the <code>toUpperCase</code> function). See <a href="#Listing_GAN-10">Listing 10</a>^<a href="#Footnote-5">5</a></p>

<p><a name="Listing_GAN-10"></a></p>

<figure class='code'><figcaption><span>Listing 10 - Function.prototype </span></figcaption>
 <div class="highlight"><table><tr><td class="gutter"><pre class="line-numbers"><span class='line-number'>1</span>
<span class='line-number'>2</span>
<span class='line-number'>3</span>
<span class='line-number'>4</span>
<span class='line-number'>5</span>
<span class='line-number'>6</span>
</pre></td><td class='code'><pre><code class='javascript'><span class='line'> <span class="kd">function</span> <span class="nx">Person</span><span class="p">(</span><span class="nx">name</span><span class="p">)</span> <span class="p">{</span>
</span><span class='line'>     <span class="c1">// ignore the argument for this listing</span>
</span><span class='line'> <span class="p">}</span>
</span><span class='line'>
</span><span class='line'> <span class="nx">console</span><span class="p">.</span><span class="nx">log</span><span class="p">(</span><span class="nx">Person</span><span class="p">.</span><span class="nx">prototype</span><span class="p">);</span> <span class="c1">// evaluates to Person { }</span>
</span><span class='line'> <span class="nx">console</span><span class="p">.</span><span class="nx">log</span><span class="p">(</span><span class="k">typeof</span> <span class="nx">Person</span><span class="p">.</span><span class="nx">prototype</span><span class="p">);</span> <span class="c1">// evaluates to &quot;object&quot;</span>
</span></code></pre></td></tr></table></div></figure>


<p>  Notice that the <code>prototype</code> property evaluates to an object, which happens to be just another plain vanilla JavaScript object. See <a href="#Figure_GAN-5">Figure 5</a> for a (simplistic) illustration on how this works.</p>

<p><a name="Figure_GAN-5"></a>
<img src="http://looselytyped.com/images/2012-08-18-on-prototypal-inheritance/Figure_GAN-5_Color.png" width="739" height="472" title="Figure 5 - Function.prototype visualized" ></p>

<p>  The object on the left is the <code>Person</code> <em>function</em> object, and the object to the right is it&rsquo;s &ldquo;prototype&rdquo;, which happens to be a plain JavaScript object. Two things of note here &ndash; Functions get a special &ldquo;prototype&rdquo; property (different from the <code>__proto__</code> property we saw earlier), and the functions prototype object (which happens to be a simple JavaScript object) gets a <code>__proto__</code> property just like we saw in the earlier section. The prototype object&rsquo;s <code>constructor</code> property points back to the function object itself. The way you refer to a function&rsquo;s (in this case <code>Person</code>) <code>prototype</code> is just like you would any other property &ndash; <code>&lt;name_of_object&gt;.&lt;name_of_property&gt;</code> &ndash; so to reference <code>Person</code>&rsquo;s prototype we say <code>Person.prototype</code>.</p>

<p>  If this looks familiar, it&rsquo;s because it is. Earlier (in <a href="#Figure_GAN-2">Figure 2</a>) I marked our <code>obj</code>&rsquo;s <code>__proto__</code> as <code>Object.prototype</code>! Looking at <a href="#Listing_GAN-10">Listing 10</a> what can we infer? Simply put, there is a function object called <code>Object</code>, in that there is a function declared with the name Object (like so <code>function Object() { ... }</code>), for which JavaScript creates a (<code>Function</code>) object, and this object points to another (plain vanilla JavaScript) object that happens to be it&rsquo;s &ldquo;prototype&rdquo;. See <a href="#Figure_GAN-6">Figure 6</a> to get an idea of how this lays itself out.</p>

<p><a name="Figure_GAN-6"></a>
<img src="http://looselytyped.com/images/2012-08-18-on-prototypal-inheritance/Figure_GAN-6_Color.png" width="618" height="388" title="Figure 6 - Object.prototype visualized" ></p>

<p>  To summarize, functions in JavaScript are just objects, albeit a little special because they all have a <code>prototype</code> property. This <code>prototype</code> property is just another object, which like any other object that you create, is part of an inheritance chain with <code>Object.prototype</code> at the end.</p>

<p>  Now that we know all this, how can we use this to properly adorn our &ldquo;person&rdquo; objects with a predefined set of behaviors? Well, we still have one more hurdle to surmount before we get there, but unfortunately, we have reached the end of our journey for this installment. In part 2, we will first see how functions can be used as constructors, and use this to build our own inheritance hierarchies in JavaScript.</p>

<h2>A minor digression</h2>

<p>So far in this article, I have used the word &ldquo;object&rdquo;, and &ldquo;instance&rdquo;. This can be a contentious issue in JavaScript&rsquo;s terminology due to the fact that JavaScript does not have the traditional notion of a &ldquo;class&rdquo; &ndash; so what exactly constitutes an &ldquo;instance&rdquo;? Well traditionally, in languages like Java, an instance <em>is</em> an object. But in JavaScript, as we have noted, constructs like the function <code>Object</code>, and our own <code>Person</code> (which as we will see in our next installment are the conduits to creating &ldquo;instances&rdquo;) are also objects.</p>

<p>One way to define an &ldquo;instance&rdquo; is like so &ndash; &ldquo;instances&rdquo; are created when we use the <code>new</code> keyword along-with a function name. Consider <code>var myString = new String("Douglas")</code> &ndash; in this case, <code>myString</code> is an &ldquo;instance&rdquo; of <code>String</code>. We should note that sometimes this is not so obvious. IN <a href="#Listing_GAN-2">Listing 2</a> we used the literal syntax (<code>{}</code>) for creating a new object &ndash; Under the covers this (roughly) translates to <code>new Object()</code> &ndash; so once again, we have created an &ldquo;instance&rdquo; of <code>Object</code>. Other examples include <code>[1, 2, 3]</code> (which happens to be an <code>Array</code>) instance, and <code>/+d/</code> which is an instance of <code>RegExp</code>.</p>

<p>The thing to take away here is that in a language like JavaScript, the distinction between &ldquo;instances&rdquo; and &ldquo;objects&rdquo; boils down to being borderline academic. As long as you understand how to use objects, and (in Part 2) how functions can be used as &ldquo;instance factories&rdquo;, in my opinion there is no reason to fret over the terminology.</p>

<h2>Conclusion</h2>

<p>JavaScript is inherently an object-oriented language. More so than many languages that we may have encountered, in that in JavaScript functions are also objects, albeit with a difference. Objects are adorned with properties, including a magical property called <code>__proto__</code>, which allows the object to participate in an inheritance, and look-up chain. Functions, on the other hand, are a tad special, and have an additional property called <code>prototype</code> and we will see how we can use this to build our own hierarchies in Part 2.</p>

<hr />

<p><a name="Footnote-1">1</a>. You can also use FireFox along with <a href="http://getfirebug.com/">Firebug</a>. Once you have that installed, create a new HTML file with the contents <code>&lt;!DOCTYPE html&gt;&lt;title&gt;On Protoypal Inheritance&lt;/title&gt;</code> and save it anywhere on your computer. Then open the newly created file in Firefox, right-click anywhere on the page, and select &ldquo;Inspect Element with Firebug&rdquo;. This too has a menu bar of it&rsquo;s own that has a &ldquo;Console&rdquo; option. Clicking that will bring you into the JavaScript console. If you are on a Mac, Safari (Version 5.1.3) also has a console similar to that of Chrome&rsquo;s.</p>

<p><a name="Footnote-2">2</a>. You could even do <code>"javascript".toUpperCase</code> (note that there are no parenthesis following the method name) but this does something very different, and which hopefully will become more apparent as we go forward.</p>

<p><a name="Footnote-3">3</a>. You can create the object, along with certain properties and methods in one fell swoop like we did in <a href="#Listing_GAN-2">Listing 2</a> but I left that out for two reasons &ndash; It does not help our discussion here, and the syntax can be a tad confusing. For details see this <a href="https://developer.mozilla.org/en/JavaScript/Reference/Global_Objects/Object/create">link</a>.</p>

<p><a name="Footnote-4">4</a>. I realize that this section may have left you with more questions than answers, but I promise that it will all become clear soon.</p>

<p><a name="Footnote-5">5</a>. I realize that the function has a name starting with a capital &ldquo;P&rdquo; vs. a lowercase as is usually the case. I can tell you that there&rsquo;s a good reason :)</p>
]]></content>
  </entry>
  
</feed>
