<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:wfw="http://wellformedweb.org/CommentAPI/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
	xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
	>

<channel>
	<title>I does Javascript! &#187; OO</title>
	<atom:link href="http://blog.stchur.com/category/oo/feed/" rel="self" type="application/rss+xml" />
	<link>http://blog.stchur.com</link>
	<description>If you don&#039;t expect too much from me, you might not be let down.</description>
	<lastBuildDate>Tue, 03 Jan 2012 16:01:10 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.3</generator>
		<item>
		<title>Simple Javascript Inheritance</title>
		<link>http://blog.stchur.com/2007/01/09/simple-javascript-inheritance/</link>
		<comments>http://blog.stchur.com/2007/01/09/simple-javascript-inheritance/#comments</comments>
		<pubDate>Tue, 09 Jan 2007 19:21:58 +0000</pubDate>
		<dc:creator>sstchur</dc:creator>
				<category><![CDATA[General]]></category>
		<category><![CDATA[OO]]></category>

		<guid isPermaLink="false">http://stchur.com/blog2/2007/01/09/simple-javascript-inheritance/</guid>
		<description><![CDATA[There are lots of articles out there on the web explaining how to mimic the type of inheritance many of us programmers who come from Java or C# are accustomed to. Dean Edwards, Kevin Lindsay, and Douglas Crockford all come to mind. Each has done some very impressive work in with regards to Javascript inheritance. [...]]]></description>
			<content:encoded><![CDATA[	<p>There are lots of articles out there on the web explaining how to mimic the type of inheritance many of us programmers who come from Java or C# are accustomed to.  Dean Edwards, Kevin Lindsay, and Douglas Crockford all come to mind.  Each has done some very impressive work in with regards to Javascript inheritance.
	<p>Each of their approaches has its own advantages and disadvantages, and each approach is (at least in my mind) a little bit confusing.  Heck, Douglass Crockford even talks about kinds of inheritance I've never even heard of!</p>
	<p>I don't hesitate for a second to admit that each of these guys is probably a more skilled Javascripter than I am, but to be perfectly honest, well... I didn't completely understand their approaches.  That doesn't mean they aren't good.  On the contrary, it probably only serves to illustrate how much I still have to learn.</p>
	<p>Regardless... I'm not completely comfortable using code I don't understand, so I took a crack at my own solution -- something really simple that I <em>did</em> understand.  Perhaps my approach may not be as robust, but it's pretty darn simple to implement (and to use), and so far I've found it to be quite effective.  So enough babbling... on to the technique.</p>
	<p><span id="more-24"></span></p>
	<p>I had just a few goals that I wanted my inheritance model to accomplish:</p>
	<ol>
	<li>Ability to extend any custom object using something like <code>customObject.extends(baseObject);</code></li>
	<li>Ability to invoke the base object's constructor</li>
	<li>Ability to invoke an arbitrary base object's function</li>
	<li>Ability to override a base object's function</li>
	</ol>
	<p>That's pretty much all I was after.  I'm not tying to mimic multiple inheritance or anything like that -- just wanted to keep things short and simple.  Here's what I came up with.</p>
	<div class="dean_ch" style="white-space: nowrap;">
<span class="kw2">Function</span>.<span class="me1">prototype</span>.<span class="kw2">extends</span> = <span class="kw2">function</span><span class="br0">&#40;</span>_baseObject<span class="br0">&#41;</span><br />
<span class="br0">&#123;</span><br />
&nbsp; &nbsp;<span class="co1">// create an instance of the base object</span><br />
&nbsp; &nbsp;<span class="kw2">var</span> baseInst = <span class="kw2">new</span> _baseObject<span class="br0">&#40;</span><span class="br0">&#41;</span>;</p>
	<p>&nbsp; &nbsp;<span class="co1">// copy all properties/functions defined on the base object with .prototype</span><br />
&nbsp; &nbsp;<span class="kw2">var</span> key;<br />
&nbsp; &nbsp;<span class="kw1">for</span> <span class="br0">&#40;</span>key <span class="kw1">in</span> _baseObject.<span class="me1">prototype</span><span class="br0">&#41;</span><br />
&nbsp; &nbsp;<span class="br0">&#123;</span><br />
&nbsp; &nbsp;&nbsp; &nbsp;<span class="kw1">this</span>.<span class="me1">prototype</span><span class="br0">&#91;</span>key<span class="br0">&#93;</span> = _baseObject.<span class="me1">prototype</span><span class="br0">&#91;</span>key<span class="br0">&#93;</span>;<br />
&nbsp; &nbsp;<span class="br0">&#125;</span></p>
	<p>&nbsp; &nbsp;<span class="co1">// expose the base object's constructor</span><br />
&nbsp; &nbsp;<span class="kw1">this</span>.<span class="me1">prototype</span>.<span class="me1">base</span> = _baseObject;</p>
	<p>&nbsp; &nbsp;<span class="co1">// expose an .invoke(..) to make it easy to invoke the base object's functions</span><br />
&nbsp; &nbsp;<span class="kw1">this</span>.<span class="me1">prototype</span>.<span class="me1">base</span>.<span class="me1">invoke</span> = <span class="kw2">function</span><span class="br0">&#40;</span>fn<span class="br0">&#41;</span><br />
&nbsp; &nbsp;<span class="br0">&#123;</span><br />
&nbsp; &nbsp;&nbsp; &nbsp;baseInst<span class="br0">&#91;</span>fn<span class="br0">&#93;</span>.<span class="me1">apply</span><span class="br0">&#40;</span><span class="kw1">this</span>, Array.<span class="me1">prototype</span>.<span class="me1">slice</span>.<span class="me1">apply</span><span class="br0">&#40;</span>arguments, <span class="br0">&#91;</span><span class="nu0">1</span><span class="br0">&#93;</span><span class="br0">&#41;</span><span class="br0">&#41;</span>;<br />
&nbsp; &nbsp;<span class="br0">&#125;</span>;<br />
<span class="br0">&#125;</span></div>
	<h4>Explanation:</h4>
	<p>We begin by extending the prototype of the native Function object so that all custom classes you defined will automatically get the function, <code>.extends(..)</code>.</p>
	<div class="note">Extending Function (and not Object) is important.  If we added to Object's prototype, then <em>every</em> object would have a .extends property (even objects created with <code>new Object()</code> or <code>{}</code>).  This can be problematic, as developers should be able to expect that a new, empty Object has no properties (this is important, for instance, when iterating over properties using for/in).</div>
	<p>Next, we create a new instance of the base object, and stuff it into the variable <code>baseInst</code>.  This baseInst will be how we enable the programmer to manually invoke a function of the base object (similarly to the way Java uses <code>super.someMethod(..)</code>, albeit with a different syntax in our case).</p>
	<p>Following the creation of the baseInst, we loop through the prototype of <code>_baseObj</code> and for each property of the base object's prototype, we create a corresponding property (with the same name/key) for the inheriting object.  I could <em>almost</em> say this step is optional, but I won't (more on that later).</p>
	<p> Next, we add a <code>.base</code> property to each object that is extended via our <code>.extends(..)</code> function which points to <code>_baseObj</code>.  This will serve as a convenient way to invoke the base constructor.  Once the <code>.base</code> property is created, we go one step further and expose a <code>.invoke(..)</code> function which will enable programmers to invoke any arbitrary function of the base object.</p>
	<div class="note">The <code>.invoke(..)</code> function isn't strictly necessary.  All it really does is wrap up the native <code>.apply(..)</code> function, but to me it reads much cleaner when you're actually working with the code of your inherited object.</div>
	<p>One final thing to note here is the use of <code>Array.prototype.slice.apply(arguments, [1])</code>.  Credit totally goes to Douglas Crockford for this one.  I knew that the arguments array was not a true array, but it never occurred to me to use <code>.apply(..)</code> <em>again</em> to get around that problem.  In the past, I had always created a new array, <code>args</code>, and copied the values I needed from <code>arguments</code> into it.  Douglas' approach though, is way more clever.</p>
	<h4>Usage:</h4>
	<p>Using this inheritance model really couldn't be easier.  Here's how it works:</p>
	<div class="dean_ch" style="white-space: nowrap;">
<span class="coMULTI">/* Shape object */</span><br />
<span class="kw2">function</span> Shape<span class="br0">&#40;</span>_color<span class="br0">&#41;</span><br />
<span class="br0">&#123;</span><br />
&nbsp; &nbsp;<span class="kw2">var</span> color = _color;<br />
&nbsp; &nbsp;<span class="kw1">this</span>.<span class="me1">getColor</span> = <span class="kw2">function</span><span class="br0">&#40;</span><span class="br0">&#41;</span> <span class="br0">&#123;</span> <span class="kw1">return</span> color; <span class="br0">&#125;</span>;<br />
&nbsp; &nbsp;<span class="kw1">this</span>.<span class="me1">draw</span> = <span class="kw2">function</span><span class="br0">&#40;</span><span class="br0">&#41;</span> <span class="br0">&#123;</span> <span class="kw3">alert</span><span class="br0">&#40;</span><span class="st0">'drawing shape...'</span><span class="br0">&#41;</span>; <span class="br0">&#125;</span>;<br />
&nbsp; &nbsp;<span class="kw1">this</span>.<span class="me1">calcArea</span> = <span class="kw2">function</span><span class="br0">&#40;</span><span class="br0">&#41;</span> <span class="br0">&#123;</span> <span class="kw3">alert</span><span class="br0">&#40;</span><span class="st0">'But I don<span class="es0">\'</span>t know what kind of shape I am yet!'</span><span class="br0">&#41;</span>; <span class="br0">&#125;</span>;<br />
&nbsp; &nbsp;<span class="kw1">this</span>.<span class="me1">talk</span> = <span class="kw2">function</span><span class="br0">&#40;</span>msg<span class="br0">&#41;</span> <span class="br0">&#123;</span> <span class="kw3">alert</span><span class="br0">&#40;</span>msg<span class="br0">&#41;</span>; <span class="br0">&#125;</span>;<br />
<span class="br0">&#125;</span><br />
Shape.<span class="me1">prototype</span>.<span class="me1">specialFn</span> = <span class="kw2">function</span><span class="br0">&#40;</span><span class="br0">&#41;</span> <span class="br0">&#123;</span> <span class="kw3">alert</span><span class="br0">&#40;</span><span class="st0">'I<span class="es0">\'</span>m a special function!'</span><span class="br0">&#41;</span>; <span class="br0">&#125;</span>;</p>
	<p><span class="coMULTI">/* Square object */</span><br />
<span class="kw2">function</span> Square<span class="br0">&#40;</span>_color<span class="br0">&#41;</span><br />
<span class="br0">&#123;</span><br />
&nbsp; &nbsp;<span class="kw1">this</span>.<span class="me1">base</span><span class="br0">&#40;</span>_color<span class="br0">&#41;</span>; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span class="co1">// invoke the base constructor</span><br />
&nbsp; &nbsp;<span class="kw1">this</span>.<span class="me1">draw</span> = <span class="kw2">function</span><span class="br0">&#40;</span><span class="br0">&#41;</span> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span class="co1">// override the .draw function</span><br />
&nbsp; &nbsp;<span class="br0">&#123;</span><br />
&nbsp; &nbsp; &nbsp; <span class="kw1">this</span>.<span class="me1">base</span>.<span class="me1">invoke</span><span class="br0">&#40;</span><span class="st0">'draw'</span><span class="br0">&#41;</span>; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span class="co1">// invoke the base object's .draw function</span><br />
&nbsp; &nbsp; &nbsp; <span class="kw3">alert</span><span class="br0">&#40;</span><span class="st0">'drawing square...'</span><span class="br0">&#41;</span>;<br />
&nbsp; &nbsp;<span class="br0">&#125;</span>;</p>
	<p>&nbsp; &nbsp;<span class="kw1">this</span>.<span class="me1">calcArea</span> = <span class="kw2">function</span><span class="br0">&#40;</span><span class="br0">&#41;</span> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span class="co1">// override the base object's .calcArea function</span><br />
&nbsp; &nbsp;<span class="br0">&#123;</span><br />
&nbsp; &nbsp; &nbsp; <span class="kw3">alert</span><span class="br0">&#40;</span><span class="st0">'Length X Width'</span><span class="br0">&#41;</span>;<br />
&nbsp; &nbsp;<span class="br0">&#125;</span>;<br />
<span class="br0">&#125;</span><br />
Square.<span class="kw2">extends</span><span class="br0">&#40;</span>Shape<span class="br0">&#41;</span>; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;<span class="co1">// Square extends the Shape object</span></p>
	<p><span class="coMULTI">/* Circle object */</span><br />
<span class="kw2">function</span> Circle<span class="br0">&#40;</span>_color<span class="br0">&#41;</span><br />
<span class="br0">&#123;</span><br />
&nbsp; &nbsp;<span class="kw1">this</span>.<span class="me1">base</span><span class="br0">&#40;</span>_color<span class="br0">&#41;</span>; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span class="co1">// invoke the base constructor</span><br />
&nbsp; &nbsp;<span class="kw1">this</span>.<span class="me1">draw</span> = <span class="kw2">function</span><span class="br0">&#40;</span><span class="br0">&#41;</span> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span class="co1">// override the .draw function()</span><br />
&nbsp; &nbsp;<span class="br0">&#123;</span><br />
&nbsp; &nbsp; &nbsp; <span class="kw1">this</span>.<span class="me1">base</span>.<span class="me1">invoke</span><span class="br0">&#40;</span><span class="st0">'talk'</span>, <span class="st0">'in the draw function'</span><span class="br0">&#41;</span>;<br />
&nbsp; &nbsp; &nbsp; <span class="kw3">alert</span><span class="br0">&#40;</span><span class="st0">'drawing circle...'</span><span class="br0">&#41;</span>;<br />
&nbsp; &nbsp;<span class="br0">&#125;</span><br />
<span class="br0">&#125;</span><br />
Circle.<span class="kw2">extends</span><span class="br0">&#40;</span>Shape<span class="br0">&#41;</span>;</p>
	<p><span class="kw2">var</span> s = <span class="kw2">new</span> Square<span class="br0">&#40;</span><span class="st0">'blue'</span><span class="br0">&#41;</span>;<br />
s.<span class="me1">draw</span><span class="br0">&#40;</span><span class="br0">&#41;</span>; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span class="co1">// alerts &quot;drawing shape...&quot; followed by &quot;drawing square...&quot;</span><br />
s.<span class="me1">calcArea</span><span class="br0">&#40;</span><span class="br0">&#41;</span>; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span class="co1">// alerts &quot;Length X Width&quot;</span><br />
<span class="kw3">alert</span><span class="br0">&#40;</span>s.<span class="me1">getColor</span><span class="br0">&#40;</span><span class="br0">&#41;</span><span class="br0">&#41;</span>; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;<span class="co1">// alerts &quot;blue&quot;</span></p>
	<p><span class="kw2">var</span> c = <span class="kw2">new</span> Circle<span class="br0">&#40;</span><span class="st0">'green'</span><span class="br0">&#41;</span>;<br />
c.<span class="me1">draw</span><span class="br0">&#40;</span><span class="br0">&#41;</span>; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span class="co1">// alerts &quot;in the draw function&quot; followed by &quot;drawing circle...&quot;</span><br />
c.<span class="me1">calcArea</span><span class="br0">&#40;</span><span class="br0">&#41;</span>; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span class="co1">// alerrts &quot;But I don't know what shape I am yet!&quot;</span><br />
<span class="kw3">alert</span><span class="br0">&#40;</span>c.<span class="me1">getColor</span><span class="br0">&#40;</span><span class="br0">&#41;</span><span class="br0">&#41;</span>; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;<span class="co1">// alerts &quot;green&quot;</span><br />
c.<span class="me1">specialFn</span><span class="br0">&#40;</span><span class="br0">&#41;</span>; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;<span class="co1">// alerts &quot;I'm a special function&quot;</span></div>
	<p>Most of the above sample should be pretty simple to follow.  Obviously the most important part of using our inheritance code is the part that invoke the <code>.extends(..)</code> function.  In the preceding example, we see it twice:</p>
	<ul>
	<li><code>Square.extends(Shape);</code></li>
	<li><code>Circle.extends(Shape);</code></li>
	</ul>
	<p>Of course, that's only half of the equation.  After indicating that both <code>Square</code> and <code>Circle</code> should extend the <code>Shape</code> object, we also need to make sure that we invoke the base constructor whenever we create a new instance of either a Square or a Circle.  We do this first thing inside the functions that declare said objects.</p>
	<div class="dean_ch" style="white-space: nowrap;">
<span class="kw2">function</span> Square<span class="br0">&#40;</span>_color<span class="br0">&#41;</span><br />
<span class="br0">&#123;</span><br />
&nbsp; &nbsp;<span class="kw1">this</span>.<span class="me1">base</span><span class="br0">&#40;</span>_color<span class="br0">&#41;</span> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;<span class="co1">// invoke base constructor</span><br />
&nbsp; &nbsp;...<br />
<span class="br0">&#125;</span></p>
	<p><span class="kw2">function</span> Circle<span class="br0">&#40;</span>_color<span class="br0">&#41;</span><br />
<span class="br0">&#123;</span><br />
&nbsp; &nbsp;<span class="kw1">this</span>.<span class="me1">base</span><span class="br0">&#40;</span>_color<span class="br0">&#41;</span> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;<span class="co1">// invoke base constructor</span><br />
&nbsp; &nbsp;...<br />
<span class="br0">&#125;</span></div>
	<p>At this point, Square and Circle have both successfully "extended" the Shape object's prototype, meaning that they have all of the same public properties and functions as the Shape object.</p>
	<p>At this point, we can go about overriding any functions of Shape that need their own specific logic, which would be functions like <code>.draw(..)</code> and <code>.calcArea(..)</code>.  Of course, in my sample, I didn't get into that much detail -- just some alert statements to illustrate the point, but I trust you get the idea.</p>
	<p>Now, there are times when you may want to not only want to override a function, but also invoke the original logic of a base function.  This can be achieved by calling <code>this.base.invoke(..)</code>.  The <code>.invoke(..)</code> function takes at least one parameter, and optionally, N more parameters.</p>
	<p>The first parameter is always the name of the base function you want to execute (as a string, in quotes).  Any additional parameters (and there can be an arbitrary number) will be passed into the base function when invoked.</p>
	<p>I included a simple example of this in my sample code:</p>
	<div class="dean_ch" style="white-space: nowrap;">
<span class="kw1">this</span>.<span class="me1">base</span>.<span class="me1">invoke</span><span class="br0">&#40;</span><span class="st0">'talk'</span>, <span class="st0">'in the draw function'</span><span class="br0">&#41;</span>;</div>
	<p>This code tells the inheriting object to call the base object's "talk" function and pass in one parameter, a string, "in the draw function".</p>
	<p>Pretty straight forward right?  At least I think so.</p>
	<h4>Final Thoughts:</h4>
	<p>Before wrapping this up, I want to touch on something I mentioned earlier and said I'd come back to:</p>
	<div class="dean_ch" style="white-space: nowrap;">
<span class="kw1">for</span> <span class="br0">&#40;</span><span class="kw2">var</span> key <span class="kw1">in</span> _baseObject.<span class="me1">prototype</span><span class="br0">&#41;</span><br />
&nbsp; &nbsp;<span class="br0">&#123;</span> <span class="kw1">this</span>.<span class="me1">prototype</span><span class="br0">&#91;</span>key<span class="br0">&#93;</span> = _baseObject.<span class="me1">prototype</span><span class="br0">&#91;</span>key<span class="br0">&#93;</span>; <span class="br0">&#125;</span></div>
	<p>The above code copies all of the properties (including functions) from the base object into the inheriting object.  I said before that this <em>almost</em> wasn't necessary.  Well it is necessary, specifically for those properties that are declared on the base object using <code>.prototype.someFunction =</code>.</p>
	<p>Personally, I rarely ever do this when I write ECMAScript.  I almost always keep my function encapsulated inside the object that "owns" them; that's just my style.  But I recognize that the <code>.prototype</code> style is a common and viable approach, so we need to support it, and that's what the above for loop will do for us.</p>
	<h4>So there ya go...</h4>
	<p>This is my first attempt at writing any kind of encapsulated, reusable inheritance model in ECMAScript.  I'm not necessarily claiming it's the best, or that it doesn't have its draw-backs, but it is pretty simple, and it does accomplish pretty much everything that I wanted.  Hopefully some of you will find it useful as well.</p>
	<p>Comments welcome.</p>
]]></content:encoded>
			<wfw:commentRss>http://blog.stchur.com/2007/01/09/simple-javascript-inheritance/feed/</wfw:commentRss>
		<slash:comments>2</slash:comments>
		</item>
	</channel>
</rss>

