<?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>unscriptable.com &#187; Inheritance</title>
	<atom:link href="http://unscriptable.com/category/javascript/inheritance/feed/" rel="self" type="application/rss+xml" />
	<link>http://unscriptable.com</link>
	<description>Just another WordPress site</description>
	<lastBuildDate>Thu, 02 Feb 2012 03:05:33 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.3.1</generator>
		<item>
		<title>cujo.js — OOJS, OOCSS, and OOHTML — Part 1 (OOCSS for Engineers)</title>
		<link>http://unscriptable.com/2010/08/31/cujo-js-oojs-oocss-and-oohtml-part-1/</link>
		<comments>http://unscriptable.com/2010/08/31/cujo-js-oojs-oocss-and-oohtml-part-1/#comments</comments>
		<pubDate>Tue, 31 Aug 2010 11:47:07 +0000</pubDate>
		<dc:creator>John Hann</dc:creator>
				<category><![CDATA[CSS]]></category>
		<category><![CDATA[cujo.js]]></category>
		<category><![CDATA[In-browser MVC]]></category>
		<category><![CDATA[Inheritance]]></category>
		<category><![CDATA[Javascript]]></category>
		<category><![CDATA[OOCSS]]></category>

		<guid isPermaLink="false">http://unscriptable.com/?p=654</guid>
		<description><![CDATA[I've seen several engineer's eyes glaze over when I mention OOCSS, but don't dismiss it as just a rehashed retelling of how to structure your CSS.  OOCSS is a very powerful and efficient tool, especially when used in conjunction with Object-Oriented JavaScript and inheritable HTML templates. ]]></description>
			<content:encoded><![CDATA[<p>One of the coolest concepts in <a href="http://cujojs.com/">cujo.js</a> is the introduction of Object-Oriented CSS into the Views and Widgets.  I&#8217;ve seen several engineer&#8217;s eyes glaze over when I mention OOCSS, but don&#8217;t dismiss it as just a rehashed retelling of how to structure your CSS.  OOCSS is a very powerful and efficient tool, especially when used in conjunction with Object-Oriented JavaScript and inheritable HTML templates.  </p>
<p>I&#8217;d love to dive right in and show you how it all works in cujo.js, but you&#8217;ve got to have a firm understanding of OOCSS for any of it to make sense.  Let&#8217;s get started&#8230;</p>
<p><span id="more-654"></span></p>
<h2 id="basic-principles">Basic principles of OOCSS</h2>
<p>Where better to learn the basic principles of Nicole Sullivan&#8217;s OOCSS than from the <a href="http://wiki.github.com/stubbornella/oocss/">source</a>?  I highly recommend you watch the slideshow and read the FAQ.  Here are some of the main points:</p>
<p>An &#8220;object&#8221; in OOCSS is an HTML fragment and its associated CSS classes*.<br />
You extend objects by applying multiple classes to its root element.<br />
You must follow some basic guidelines:<br />
	Separate structure from skin (styling).<br />
	Separate container from content.<br />
	Don&#8217;t use ID attributes to define CSS rules.<br />
	Use CSS &#8220;hacks&#8221; for targeting and fixing browser quirks.</p>
<p>*Nicole alludes to some JavaScript associated with these OOCSS &#8220;objects&#8221; (a hilariously surprising assessment from the perspective of a JavaScript engineer who might think of JavaScript as the primary language and of CSS as secondary).  </p>
<p>Nicole includes several other guidelines, as well.  All the guidelines can be summarized into three major categories: 1) guidelines to help reuse objects and CSS classes, 2) guidelines to prevent unnecessary specificity increases, and 3) guidelines to allow OOCSS principles to work in ancient browsers.</p>
<p>The ultimate benefit of following OOCSS principles is optimal reuse of CSS rules and OOCSS objects.</p>
<p>For an excellent and detailed example of OOCSS in action (and a very slick use of CSS3), jump over to Brian Cavalier&#8217;s blog post, <a href="http://briancavalier.posterous.com/building-a-digital-clock-with-oocss-and-mvc">Building a digital clock with OOCSS and MVC </a>and come back.  </p>
<h2 id="oocss-inheritance">OOCSS inheritance</h2>
<p>If code reuse were the end of the story about OOCSS, it would certainly be no small achievement, to say the least.  Nicole has done an amazing service to the web industry, especially to those whose main job is to wrangle CSS.  But little did she know, she also planted a seed of inspiration in several JavaScript engineers&#8217; heads, mine included.  I had been already trying to form solid ideas around very similar CSS patterns, but couldn&#8217;t find the right words or evidence.  After watching her speak live at TXJS 2010, things started to become crystal clear &#8212; especially when she started talking about reusable objects.</p>
<p>One of the most common methods to reuse code is through inheritance.  In classical object-oriented languages, classes inherit attributes and behavior from ancestor classes.  The classes are then used to construct objects.  In prototypal languages (such as JavaScript), objects inherit attributes and behavior from ancestor objects.  However, in OOCSS, objects inherit attributes and <em>run-time</em> state from ancestor objects.</p>
<p>Read that one more time: &#8220;&#8230;objects inherit attributes and <em>run-time</em> state&#8230;&#8221;  We&#8217;re not talking about some static, compile-time concept as with classical inheritance.  And we&#8217;re not talking about inheritance that happens during object construction.  We&#8217;re talking about run-time, baby!  </p>
<p>As an object changes at run time from one state to another, so too do it&#8217;s descendants.  Or, conversely, as an object&#8217;s ancestors states change, so too does the object.  Brian and I talked about this concept and dubbed the term, &#8220;whole state&#8221;, to describe the object&#8217;s current state combined with the states of its ancestors.</p>
<p>Let&#8217;s jump right into an example to better grasp this.  Here&#8217;s a simple HTML snippet from a facetious &#8220;club&#8221; project:</p>

<div class="wp_syntax"><div class="code"><pre class="html" style="font-family:monospace;">&lt;section class=&quot;club-view club-hawt-view&quot;&gt;
    &lt;ul class=&quot;club-list&quot;&gt;
        &lt;li class=&quot;club-item club-rock-item&quot;&gt;1st Item&lt;/li&gt;
        &lt;li class=&quot;club-item club-disco-item&quot;&gt;2nd Item&lt;/li&gt;
        &lt;li class=&quot;club-item club-pop-item&quot;&gt;3rd Item&lt;/li&gt;
    &lt;/ul&gt;
    &lt;button class=&quot;dance-mode-selector&quot;&gt;Let's do Disco!&lt;/button&gt;
&lt;/section&gt;</pre></div></div>

<p>Notice the name-spaced css classes (using dash-notation to be friendly to HTML4 strict documents).  While not important to this discussion, name-spaces are important to protect your code from name conflicts in large projects, especially if not all of the CSS uses OOCSS principles!</p>
<p>More importantly, notice the classes on the root element of our topmost &#8220;object&#8221;.  <code>club-view</code> and <code>club-hawt-view</code> describe static classes in the classical or prototypal sense.  <code>club-hawt-view</code> is a specialization of <code>club-view</code>. Underneath this <code>club-view</code> object (as we&#8217;ll call it for simplicity&#8217;s sake), we have other objects: <code>club-list</code> and <code>club-item</code>.  <code>club-item</code> has three different specializations, as well: <code>club-rock-item</code>, <code>club-disco-item</code>, and <code>club-pop-item</code>.</p>
<p>Note that we must specify both the base (ancestor) and specialization (descendant) classes on the element.  This is because CSS does not specify a way to declare the inheritance relationship in the stylesheet.  This is not a limitation of CSS, in my opinion, just an implementation detail.  </p>
<p>Interestingly, Nicole calls for declarative inheritance in her slides.  This would allow the designer to specify the link to the ancestor class in the stylesheet, rather than in the HTML.  Thus, only <code>club-hawt-view</code> would need to be specified on the object since the stylesheet would declare the link to the ancestor, <code>club-view</code>.  </p>
<p>(Some other <a href="http://lesscss.org/">projects</a> have implemented compile-time solutions to provide declarative inheritance.  However, declarative inheritance in the stylesheet may do little except save some bulk in the HTML, imho.  I&#8217;m on the fence about it.  It feels like it&#8217;s an idea invented out of basic misunderstanding of CSS inheritance, but I can&#8217;t really see any significant down-sides at the moment, either.  In fact, if used correctly, it may help clarify the difference between specialization CSS classes and stateful CSS classes in the HTML.)</p>
<h2 id="oocss-state">OOCSS state</h2>
<p>Now, imagine that we want to place our <code>club-view</code> object into disco mode when the user clicks the button.  In disco mode, we want to highlight certain objects and de-emphasize &#8212; or even hide &#8212; others.  For instance, we don&#8217;t want to show any <code>club-rock-item</code> objects. (Cuz rockers don&#8217;t do disco.)  We could set them to display:none, in this case.  </p>
<p>In a noobish implementation of this behavior, we would simply catch the button&#8217;s click event, query the <code>club-rock-item</code> elements, and hide them.  This is seductively easy (and dangerous) in jQuery:</p>

<div class="wp_syntax"><div class="code"><pre class="javascript" style="font-family:monospace;">$<span style="color: #009900;">&#40;</span><span style="color: #3366CC;">'.dance-mode-selector'</span><span style="color: #009900;">&#41;</span>.<span style="color: #660066;">click</span><span style="color: #009900;">&#40;</span><span style="color: #003366; font-weight: bold;">function</span> <span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
    $<span style="color: #009900;">&#40;</span><span style="color: #3366CC;">'.club-rock-item'</span><span style="color: #009900;">&#41;</span>.<span style="color: #660066;">css</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">'display'</span><span style="color: #339933;">,</span> <span style="color: #3366CC;">'none'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #009900;">&#125;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span></pre></div></div>

<p>or dojo:</p>

<div class="wp_syntax"><div class="code"><pre class="javascript" style="font-family:monospace;">dojo.<span style="color: #660066;">query</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">'.dance-mode-selector'</span><span style="color: #009900;">&#41;</span>.<span style="color: #660066;">onclick</span><span style="color: #009900;">&#40;</span><span style="color: #003366; font-weight: bold;">function</span> <span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
    dojo.<span style="color: #660066;">query</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">'.club-rock-item'</span><span style="color: #009900;">&#41;</span>.<span style="color: #660066;">style</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">'display'</span><span style="color: #339933;">,</span> <span style="color: #3366CC;">'none'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #009900;">&#125;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span></pre></div></div>

<p>The less noobish of us know it&#8217;s a lot more cpu-efficient and flexible if we did this using CSS classes instead of manipulating the nodes&#8217; styles directly.  Let&#8217;s add the following CSS rule and change the click event handler to use it:</p>

<div class="wp_syntax"><div class="code"><pre class="css" style="font-family:monospace;"><span style="color: #6666ff;">.club-disco-mode</span> <span style="color: #6666ff;">.club-rock-item</span> <span style="color: #00AA00;">&#123;</span> <span style="color: #000000; font-weight: bold;">display</span><span style="color: #00AA00;">:</span> <span style="color: #993333;">none</span><span style="color: #00AA00;">;</span> <span style="color: #00AA00;">&#125;</span></pre></div></div>


<div class="wp_syntax"><div class="code"><pre class="javascript" style="font-family:monospace;">dojo.<span style="color: #660066;">query</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">'.dance-mode-selector'</span><span style="color: #009900;">&#41;</span>.<span style="color: #660066;">onclick</span><span style="color: #009900;">&#40;</span><span style="color: #003366; font-weight: bold;">function</span> <span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
    dojo.<span style="color: #660066;">query</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">'.club-view'</span><span style="color: #009900;">&#41;</span>.<span style="color: #660066;">addClass</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">'club-disco-mode'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #009900;">&#125;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span></pre></div></div>

<p>The resulting HTML would look like this:</p>

<div class="wp_syntax"><div class="code"><pre class="html" style="font-family:monospace;">&lt;section class=&quot;club-view club-hawt-view club-disco-mode&quot;&gt;
    &lt;ul class=&quot;club-list&quot;&gt;
        &lt;li class=&quot;club-item club-rock-item&quot;&gt;1st Item&lt;/li&gt;
        &lt;li class=&quot;club-item club-disco-item&quot;&gt;2nd Item&lt;/li&gt;
        &lt;li class=&quot;club-item club-pop-item&quot;&gt;3rd Item&lt;/li&gt;
    &lt;/ul&gt;
    &lt;button class=&quot;dance-mode-selector&quot;&gt;Let's do Disco!&lt;/button&gt;
&lt;/section&gt;</pre></div></div>

<p>Notice I put the <code>club-disco-mode</code> class on the root object, not the <code>club-rock-item</code> objects (more about this in the next section).  Changing the CSS class instead of direct DOM styling puts the task of deciding how to handle <code>club-disco-mode</code> in the hands of the CSS designer where it belongs.  For instance, once she&#8217;s seen the button jolt upwards (as it would when the <code>club-rock-item</code> object disappeared), she&#8217;d likely throw up a little in her mouth or have a mild seizure induced by this severe UX <em>faux pas</em>!  Then she might mute the colors instead of removing the element from the layout.  </p>
<p>The beauty of this is that there&#8217;s no need to mess with the JavaScript to make this change.  The task of deciding styling and layout for this state change is defined entirely in the CSS.  In fact, the only dependency between the CSS and the JavaScript is the coordination of a single class name, <code>club-disco-mode</code>, on the root element of our object.  You can think of this class name as a message from the JavaScript to the CSS, telling the OOCSS object that it is in a new state.  We&#8217;ve essentially just decoupled the JavaScript from the CSS using a classic method used in object-oriented programming: <em>message passing</em>.  </p>
<p>Not impressed so far?  This is just good CSS practices cast in the light of classical OO, you say?  Ha ha, don&#8217;t worry.  We&#8217;ve only scratched the surface.  In a subsequent blog post, I&#8217;ll show you how cujo.js abstracts the concept of state changes, allowing both CSS (style and layout) and JavaScript (behavioral) state changes to execute in parallel with a single line of code.  </p>
<p>But first, let&#8217;s dig a little deeper into run-time state inheritance&#8230;</p>
<h2 id="oocss-run-time-state">OOCSS run-time state inheritance</h2>
<p>In our &#8220;club&#8221; app, we can also treat the repeated <code>club-item</code> elements as objects, too.  When we change the state of the root <code>club-view</code> object to <code>club-disco-mode</code>, we&#8217;ve also changed the state of all of the <code>club-item</code> objects.  We can take advantage of this by declaring OOCSS rules like the one below.  This rule is just like the earlier rule, but the style declarations have been changed to something less seizure-inducing to somebody with UX sensitivities:</p>

<div class="wp_syntax"><div class="code"><pre class="css" style="font-family:monospace;"><span style="color: #6666ff;">.club-disco-mode</span> <span style="color: #6666ff;">.club-rock-item</span> <span style="color: #00AA00;">&#123;</span> <span style="color: #000000; font-weight: bold;">background-color</span><span style="color: #00AA00;">:</span> <span style="color: #cc00cc;">#ccc</span><span style="color: #00AA00;">;</span> <span style="color: #000000; font-weight: bold;">color</span><span style="color: #00AA00;">:</span> <span style="color: #cc00cc;">#777</span><span style="color: #00AA00;">;</span> <span style="color: #00AA00;">&#125;</span> <span style="color: #808080; font-style: italic;">/* grayed-out */</span></pre></div></div>

<p>You could read this as <em><code>club-rock-item</code> objects under the state of <code>club-disco-mode</code></em>.  If we wanted to be even more explicit, we could write it like this:</p>

<div class="wp_syntax"><div class="code"><pre class="css" style="font-family:monospace;"><span style="color: #6666ff;">.club-view</span><span style="color: #6666ff;">.club-disco-mode</span> <span style="color: #6666ff;">.club-rock-item</span> <span style="color: #00AA00;">&#123;</span> <span style="color: #000000; font-weight: bold;">background-color</span><span style="color: #00AA00;">:</span> <span style="color: #cc00cc;">#ccc</span><span style="color: #00AA00;">;</span> <span style="color: #000000; font-weight: bold;">color</span><span style="color: #00AA00;">:</span> <span style="color: #cc00cc;">#777</span><span style="color: #00AA00;">;</span> <span style="color: #00AA00;">&#125;</span> <span style="color: #808080; font-style: italic;">/* grayed-out */</span></pre></div></div>

<p>This translates to <em><code>club-rock-item</code> objects under <code>club-view</code> objects in the <code>club-disco-mode</code> state</em>.  Here we see how the <code>club-rock-item</code> objects inherit the state of their ancestor(s) and how we can define the style attributes of those objects in response to that state.  In the more explicit variant, we&#8217;ve also increased the specificity, which may not be desirable in all situations, so the shorter variant is also acceptable as long as it can&#8217;t be interpreted ambiguously.  </p>
<p>So, why did I declare the rules this way?  Why didn&#8217;t I change the state on the <code>club-rock-item</code> objects instead of the <code>club-view</code> object?  Because the button&#8217;s stated purpose is to change the state of the entire object, not of only the <code>club-item</code> objects!  Too often, we JavaScript engineers concentrate too much on the task at hand (manipulating the <code>club-rock-item</code> elements), not the semantic meaning of the user&#8217;s action (to change the state of the <code>club-view</code> object).  </p>
<p>Let&#8217;s revisit the <code>club-hawt-view</code> specialization class to better illustrate an example of why this is so important.  When this class is applied to our object, we potentially inherit a whole new set of CSS rules.  One of these rules could define an entirely different layout or a different styling.  Maybe in a <code>club-hawt-view</code> we&#8217;ve turned up the heat a bit:</p>

<div class="wp_syntax"><div class="code"><pre class="css" style="font-family:monospace;"><span style="color: #6666ff;">.club-hawt-view</span> <span style="color: #00AA00;">&#123;</span> <span style="color: #000000; font-weight: bold;">background-color</span><span style="color: #00AA00;">:</span> <span style="color: #cc00cc;">#fa7</span><span style="color: #00AA00;">;</span> <span style="color: #000000; font-weight: bold;">color</span><span style="color: #00AA00;">:</span> <span style="color: #cc00cc;">#500</span><span style="color: #00AA00;">;</span> <span style="color: #00AA00;">&#125;</span> <span style="color: #808080; font-style: italic;">/* red on orange! HAWT! */</span></pre></div></div>

<p>It just wouldn&#8217;t fit to de-emphasized the <code>club-rock-item</code> objects with a gray background, anymore.  The gray and orange clash.  Instead, when we&#8217;re in the <code>club-disco-mode</code> state, let&#8217;s embolden the font on the emphasized objects and just change the font color on the de-emphasized <code>club-rock-item</code>, not the background.  Let&#8217;s also add a dotted border around the whole object (yah, yah, I know this is getting really damn ugly, but hey, I&#8217;m an engineer):</p>

<div class="wp_syntax"><div class="code"><pre class="css" style="font-family:monospace;"><span style="color: #6666ff;">.club-hawt-view</span><span style="color: #6666ff;">.club-disco-mode</span> .club-disco-item<span style="color: #00AA00;">,</span>
<span style="color: #6666ff;">.club-hawt-view</span><span style="color: #6666ff;">.club-disco-mode</span> <span style="color: #6666ff;">.club-pop-item</span> <span style="color: #00AA00;">&#123;</span> 
    <span style="color: #000000; font-weight: bold;">font-weight</span><span style="color: #00AA00;">:</span> <span style="color: #993333;">bold</span><span style="color: #00AA00;">;</span> 
<span style="color: #00AA00;">&#125;</span> 
<span style="color: #6666ff;">.club-hawt-view</span><span style="color: #6666ff;">.club-disco-mode</span> <span style="color: #6666ff;">.club-rock-item</span> <span style="color: #00AA00;">&#123;</span> <span style="color: #000000; font-weight: bold;">background-color</span><span style="color: #00AA00;">:</span> <span style="color: #993333;">transparent</span><span style="color: #00AA00;">;</span> <span style="color: #000000; font-weight: bold;">color</span><span style="color: #00AA00;">:</span> <span style="color: #cc00cc;">#777</span><span style="color: #00AA00;">;</span> <span style="color: #00AA00;">&#125;</span> 
<span style="color: #6666ff;">.club-hawt-view</span><span style="color: #6666ff;">.club-disco-mode</span> <span style="color: #00AA00;">&#123;</span> <span style="color: #000000; font-weight: bold;">border</span><span style="color: #00AA00;">:</span> <span style="color: #933;">1px</span> <span style="color: #993333;">dotted</span> pink<span style="color: #00AA00;">;</span> <span style="color: #00AA00;">&#125;</span></pre></div></div>

<p>The key point is that we didn&#8217;t have to change the JavaScript to deal with the specialized, <code>club-hawt-view</code> object.  We still just passed the same <code>club-disco-mode</code> message to the OOCSS <code>club-view</code> object from the JavaScript in response to a button click:</p>

<div class="wp_syntax"><div class="code"><pre class="javascript" style="font-family:monospace;">dojo.<span style="color: #660066;">query</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">'.dance-mode-selector'</span><span style="color: #009900;">&#41;</span>.<span style="color: #660066;">onclick</span><span style="color: #009900;">&#40;</span><span style="color: #003366; font-weight: bold;">function</span> <span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
    dojo.<span style="color: #660066;">query</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">'.club-view'</span><span style="color: #009900;">&#41;</span>.<span style="color: #660066;">addClass</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">'club-disco-mode'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span> <span style="color: #006600; font-style: italic;">// still the same exact code!</span>
<span style="color: #009900;">&#125;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span></pre></div></div>

<h2 id="oocss-decreases-risk">OOCSS decreases risk</h2>
<p>This has been a simple example, but you can start to imagine why using JavaScript to programmatically change styling starts to get ugly.  Here&#8217;s what the code might look like if we used JavaScript instead of CSS:</p>

<div class="wp_syntax"><div class="code"><pre class="javascript" style="font-family:monospace;">dojo.<span style="color: #660066;">query</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">'.dance-mode-selector'</span><span style="color: #009900;">&#41;</span>.<span style="color: #660066;">onclick</span><span style="color: #009900;">&#40;</span><span style="color: #003366; font-weight: bold;">function</span> <span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
    dojo.<span style="color: #660066;">query</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">'.club-item'</span><span style="color: #009900;">&#41;</span>.<span style="color: #660066;">forEach</span><span style="color: #009900;">&#40;</span><span style="color: #003366; font-weight: bold;">function</span> <span style="color: #009900;">&#40;</span><span style="color: #000066; font-weight: bold;">item</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
        <span style="color: #000066; font-weight: bold;">if</span> <span style="color: #009900;">&#40;</span>dojo.<span style="color: #660066;">hasClass</span><span style="color: #009900;">&#40;</span><span style="color: #000066; font-weight: bold;">item</span><span style="color: #339933;">,</span> <span style="color: #3366CC;">'club-disco-item'</span><span style="color: #009900;">&#41;</span> <span style="color: #339933;">||</span> dojo.<span style="color: #660066;">hasClass</span><span style="color: #009900;">&#40;</span><span style="color: #000066; font-weight: bold;">item</span><span style="color: #339933;">,</span> <span style="color: #3366CC;">'club-pop-item'</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
            dojo.<span style="color: #660066;">style</span><span style="color: #009900;">&#40;</span><span style="color: #000066; font-weight: bold;">item</span><span style="color: #339933;">,</span> <span style="color: #3366CC;">'fontWeight'</span><span style="color: #339933;">,</span> <span style="color: #3366CC;">'bold'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
        <span style="color: #009900;">&#125;</span>
        <span style="color: #000066; font-weight: bold;">else</span> <span style="color: #009900;">&#123;</span>
            dojo.<span style="color: #660066;">style</span><span style="color: #009900;">&#40;</span><span style="color: #000066; font-weight: bold;">item</span><span style="color: #339933;">,</span> <span style="color: #009900;">&#123;</span> backgroundColor<span style="color: #339933;">:</span> <span style="color: #3366CC;">'transparent'</span><span style="color: #339933;">,</span> color<span style="color: #339933;">:</span> <span style="color: #3366CC;">'#777'</span> <span style="color: #009900;">&#125;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
        <span style="color: #009900;">&#125;</span>
    <span style="color: #009900;">&#125;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
    dojo.<span style="color: #660066;">style</span><span style="color: #009900;">&#40;</span><span style="color: #000066; font-weight: bold;">this</span><span style="color: #339933;">,</span> <span style="color: #3366CC;">'border'</span><span style="color: #339933;">,</span> <span style="color: #3366CC;">'1px dotted pink'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #009900;">&#125;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span></pre></div></div>

<p>This is not reusable code, nor is it very maintainable once you start getting into more complex, real-life projects.  Brian pointed out to me that the general complexity of the situation can be summarized as O(n^m), where n == the total number of CSS classes on a node and m is the number of ancestry levels we may need to manipulate.  If we&#8217;re handling just 3 CSS classes on 3 levels, we&#8217;ve got up to 27 separate cases to consider in code.  </p>
<p>Worst of all, we&#8217;ve inextricably tied our button and our <code>club-item</code> elements together.  If we were to suddenly need a variation of the <code>club-view</code> objects without the <code>club-item</code> objects at all, we&#8217;d be forced to branch yet again.  </p>
<p>As Brian said in his post, declarative code &#8212; such as CSS &#8212; is inherently less risky to modify than procedural code.  </p>
<h2 id="oocss-for-ie">OOCSS in ancient browsers</h2>
<p>The astute CSS developer will have identified several problems with all of the CSS I&#8217;ve shown.  Specifically, none of it will work in IE6 at all!  Combination class selectors (<code>.club-hawt-view.club-disco-mode</code>, for instance) and transparency weren&#8217;t supported in IE until version 7.  Not to worry, cujo.js fixes those automatically.  It also fixes several other CSS2.1 and CSS3 shortcomings in IE.  cujo.js doesn&#8217;t come close to bringing IE up to CSS3 specifications, though, so don&#8217;t get too excited.  cujo.js only fixes IE enough to allow OOCSS to work to its fullest potential while also removing the dependencies between CSS, HTML, and JavaScript.  Separation of concerns has always been a major goal of CSS, but only CSS3 finally fulfills it.  More on that in yet another future blog post.</p>
<p><strong>[update]<br />
For years I thought IE6 didn&#8217;t support combination class selectors!  People are telling me that they work as long as you order the classes correctly (<code>.bar.foo {}</code> versus <code>.foo.bar {}</code>).  I must always order them the way that doesn&#8217;t work.  I&#8217;ll do some further testing and verify.<br />
[/update]</strong></p>
<p>You likely also noticed that Brian is using CSS3 transitions in his project.  Transitions are a critically powerful tool in any UX designers toolkit.  Unfortunately, IE and Firefox  still don&#8217;t support transitions (as of September 2010).  No biggie, cujo.js fixes that, too.  We&#8217;ve even extended transitions to include more popular timing functions such as bounce, elastic, sine, etc.  We&#8217;re also investigating other, more advanced types of transitions to emulate common use cases that don&#8217;t fit the CSS3 transitions model at all.  </p>
<p>For instance, transitions alone can&#8217;t animate an element being tossed into the trash.  Too often the element being deleted and the trash icon don&#8217;t share the same offsetParent or there&#8217;s an ancestor with overflow:hidden somewhere between.  For situations like this, you&#8217;d typically have to append the node to some common ancestor (likely the body element) and then animate it from the original location to the trash icon.  That kind of sequence comes in handy for accessibility-friendly alternatives to drag-and-drop &#8212; as well as a few other use cases.  So, we&#8217;ll be investigating ways to allow the CSS designer to take control of complex transitions like these without tinkering with the engineer&#8217;s JavaScript.</p>
<p>Still not convinced of the benefits of OOCSS?  Next we&#8217;ll see how to apply it in a client-side MVC architecture.  OOCSS becomes even more powerful when you combine it with view-controllers based on OOJS and ultra-simple, inheritable templates I&#8217;ve dubbed OOHTML.</p>
<p>What do you think so far?  Does OOCSS make sense?  Have you already used it in an MVC-based environment?  Let me know!</p>
]]></content:encoded>
			<wfw:commentRss>http://unscriptable.com/2010/08/31/cujo-js-oojs-oocss-and-oohtml-part-1/feed/</wfw:commentRss>
		<slash:comments>23</slash:comments>
		</item>
		<item>
		<title>Javascript inheritance explained 2</title>
		<link>http://unscriptable.com/2009/03/10/javascript-inheritance-explained-2/</link>
		<comments>http://unscriptable.com/2009/03/10/javascript-inheritance-explained-2/#comments</comments>
		<pubDate>Wed, 11 Mar 2009 03:42:06 +0000</pubDate>
		<dc:creator>John Hann</dc:creator>
				<category><![CDATA[Inheritance]]></category>
		<category><![CDATA[Javascript]]></category>
		<category><![CDATA[prototype.js]]></category>

		<guid isPermaLink="false">http://unscriptable.com/?p=27</guid>
		<description><![CDATA[Wow. It&#8217;s been almost 2 years since I&#8217;ve finished a technical blog post! I&#8217;ve started plenty, but have just never quite finished any. I figure it&#8217;s either because I try to tackle too much in a single post and/or because I am way too picky about the topic. Anyways, here&#8217;s a new blog post to [...]]]></description>
			<content:encoded><![CDATA[<p>Wow.  It&#8217;s been almost 2 years since I&#8217;ve finished a technical blog post!  I&#8217;ve started plenty, but have just never quite finished any.  I figure it&#8217;s either because I try to tackle too much in a single post and/or because I am way too picky about the topic.  Anyways, here&#8217;s a new blog post to prove I didn&#8217;t actually fall off the face of the Earth!</p>
<p>In my previous post, I laid out the basic Javascript inheritance mechanism, <em>prototype chaining</em>.  In this post, I&#8217;ll elaborate on it a bit more by showing how I took this basic concept and created something workable for some very bright, but prototype-phobic programmers: Oracle Life Sciences division&#8217;s Java developers!  (Java and Javascript couldn&#8217;t be more different in almost every regard, especially concerning inheritance.)<br />
<span id="more-27"></span><br />
Even though I had been using Javascript quite heavily since 1996, I was clueless to its object-oriented capabilities until 2004.  That&#8217;s when I discovered prototype.js, one of the first Javascript frameworks &#8212; and still one of the best, imho. One of the reasons I love prototype.js is because it extends the native Javascript objects, rather than wraps them.  (This, also, btw, is one of the aspects of prototype.js that people don&#8217;t like.)  But to me, it just feels so much more powerful, natural, and expressive.  <em>This is the way that Javascript was meant to be used!</em></p>
<p>Well, that said, I plan to talk about how I gleaned some knowledge from an exception to prototoype.js&#8217;s extend-rather-than-wrap rule in version 1.3.  The Class.create() method breaks from prototype.js&#8217;s usual m.o. and hides the native, natural prototypal inheritance of Javascript with a more classical-looking inheritance mechanism (e.g. more Java-like).  At first, I hated that prototype.js did this &#8212; but so did every other major framework.  </p>
<p>However, I warmed up to it when I was tasked to create a new Javascript framework from scratch at Oracle.  The reason for my change-of-heart is simple: wrapping the inheritance mechanism creates an opportunity to do <em>magical</em> things and reduces the overall code size at the same time.</p>
<p>Primer / reminder: prototypal inheritance uses slightly different concepts from classical OO:</p>
<ul>
<li>constructor <==> class</li>
<li>prototype members <==> class members</li>
</ul>
<p>OK.  Time for some code.  Our wrapper function looked a lot like this:</p>

<div class="wp_syntax"><div class="code"><pre class="javascript" style="font-family:monospace;">Object.<span style="color: #660066;">Extend</span> <span style="color: #339933;">=</span> <span style="color: #003366; font-weight: bold;">function</span> <span style="color: #009900;">&#40;</span><span style="color: #009966; font-style: italic;">/* Function|Object */</span> base<span style="color: #339933;">,</span> <span style="color: #009966; font-style: italic;">/* Function */</span> construct<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
  <span style="color: #003366; font-weight: bold;">var</span> baseIsObj <span style="color: #339933;">=</span> <span style="color: #000066; font-weight: bold;">typeof</span> base <span style="color: #339933;">==</span> <span style="color: #3366CC;">'object'</span><span style="color: #339933;">;</span>
  construct.<span style="color: #660066;">prototype</span> <span style="color: #339933;">=</span> baseIsObj <span style="color: #339933;">?</span> base <span style="color: #339933;">:</span> <span style="color: #003366; font-weight: bold;">new</span> base<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
  construct.<span style="color: #660066;">prototype</span>.<span style="color: #660066;">superclass</span> <span style="color: #339933;">=</span> baseIsObj <span style="color: #339933;">?</span> base.<span style="color: #660066;">constructor</span>.<span style="color: #660066;">prototype</span> <span style="color: #339933;">:</span> base.<span style="color: #660066;">prototype</span><span style="color: #339933;">;</span>
  <span style="color: #000066; font-weight: bold;">return</span> construct<span style="color: #339933;">;</span>
<span style="color: #009900;">&#125;</span></pre></div></div>

<p>In summary, Object.Extend takes a base constructor function or prototype object (base) and a new constructor function (construct).  As you would expect, it chains the prototype of the new constructor function to the base constructor function, which allows the objects created with the new constructor to inherit members from the old constructor.  Nothing new here.</p>

<div class="wp_syntax"><div class="code"><pre class="javascript" style="font-family:monospace;"><span style="color: #003366; font-weight: bold;">var</span> MySubClass <span style="color: #339933;">=</span> Object.<span style="color: #660066;">Extend</span><span style="color: #009900;">&#40;</span>MyBaseClass<span style="color: #339933;">,</span> <span style="color: #003366; font-weight: bold;">function</span> <span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span> <span style="color: #009966; font-style: italic;">/* do stuff here */</span> <span style="color: #009900;">&#125;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span></pre></div></div>

<p>If an object is passed instead of the base constructor, it is used directly as the prototype.  Typically, the developer would pass the prototype from an existing base constructor or an object literal to serve as a prototype.  However, as an interesting twist, the developer could choose to pass an instantiated object from a previously-defined constructor.  This can provide some very interesting object hierarchies, but likely with unintentional consequences.  (This might be a good topic for a later blog post.)</p>
<p>More importantly, our wrapper method gave us the opportunity for some <em>syntactic sugar</em>.  We&#8217;ve added a new property to our new constructor&#8217;s prototype: superclass.  This property allows us to access our object&#8217;s immediate ancestor prototype directly.  By default, Javascript forces the developer to know the ancestor prototype in advance, which can be problematic or simply impossible in polymorphic situations.  </p>
<p>Default Javascript method to access an ancestor prototype:</p>

<div class="wp_syntax"><div class="code"><pre class="javascript" style="font-family:monospace;">MyClass.<span style="color: #660066;">prototype</span>.<span style="color: #660066;">someMethod</span> <span style="color: #339933;">=</span> <span style="color: #003366; font-weight: bold;">function</span> <span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
    AncestorClass.<span style="color: #660066;">prototype</span>.<span style="color: #660066;">someMethod</span>.<span style="color: #660066;">apply</span><span style="color: #009900;">&#40;</span><span style="color: #000066; font-weight: bold;">this</span><span style="color: #339933;">,</span> arguments<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
    <span style="color: #009966; font-style: italic;">/* do other stuff here */</span>
<span style="color: #009900;">&#125;</span><span style="color: #339933;">;</span></pre></div></div>

<p>Our new method to access our immediate ancestor&#8217;s prototype:</p>

<div class="wp_syntax"><div class="code"><pre class="javascript" style="font-family:monospace;">MyClass.<span style="color: #660066;">prototype</span>.<span style="color: #660066;">someMethod</span> <span style="color: #339933;">=</span> <span style="color: #003366; font-weight: bold;">function</span> <span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
    <span style="color: #000066; font-weight: bold;">this</span>.<span style="color: #660066;">superclass</span>.<span style="color: #660066;">someMethod</span><span style="color: #009900;">&#40;</span>arguments<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
    <span style="color: #009966; font-style: italic;">/* do other stuff here */</span>
<span style="color: #009900;">&#125;</span><span style="color: #339933;">;</span></pre></div></div>

<p>Besides being more compact, our new method prevents us from having to know our immediate ancestor!  R0x0r! </p>
<p>Wait a minute, I seem to remember that this isn&#8217;t really a freebie.  I remember several issues that appeared when the ancestor&#8217;s implementation of someMethod called upon other methods or properties.  The superclass property had to be used very carefully or it would result in unpredictable results or infinite loops (ugh, I think I need another blog post for this).  In the Oracle framework, we used the superclass property very sparingly.  </p>
<p>Another side-effect of our Object.Event wrapper is that we must allow our base constructor function to be able to take zero parameters.  This was initially a show-stopper for me.  I&#8217;d been so accustomed to passing initialization parameters to my classes in classical OO.  It&#8217;s good programming practice to force developers to pass parameters that are necessary for proper object instantiation.  At Oracle, we got used to calling an init() method on our objects immediately after instantiating them, passing any necessary parameters to this method.  </p>
<p>This zero parameters problem is easily worked around for the general case by providing an instantiation method in the constructor&#8217;s prototype.  This is precisely the way that prototype.js and some other frameworks do it. A call to</p>

<div class="wp_syntax"><div class="code"><pre class="javascript" style="font-family:monospace;"><span style="color: #000066; font-weight: bold;">this</span>.<span style="color: #660066;">initialize</span>.<span style="color: #660066;">apply</span><span style="color: #009900;">&#40;</span><span style="color: #000066; font-weight: bold;">this</span><span style="color: #339933;">,</span> arguments<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span></pre></div></div>

<p>occurs in the wrapper function.  </p>
<p>It&#8217;s very interesting to note that prototype.jsalso implements a second-level of prototypal inheritance within it&#8217;s wrapper.  It constructs an inner object and places it into the prototype chain.  This solves some of the problems we had at Oracle and also handily solves the problems with the superclass property.  </p>
<p>Wow.  There is so much to talk about!  I think I&#8217;ll take each of the major frameworks, prototype.js, jQuery, and dojo, and dissect their inheritance wrapper functions.  What better way to understand the intricacies of object-oriented Javascript!</p>
]]></content:encoded>
			<wfw:commentRss>http://unscriptable.com/2009/03/10/javascript-inheritance-explained-2/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Javascript inheritance explained</title>
		<link>http://unscriptable.com/2007/04/17/inheritance-explained/</link>
		<comments>http://unscriptable.com/2007/04/17/inheritance-explained/#comments</comments>
		<pubDate>Wed, 18 Apr 2007 04:00:48 +0000</pubDate>
		<dc:creator>John Hann</dc:creator>
				<category><![CDATA[Inheritance]]></category>
		<category><![CDATA[Javascript]]></category>

		<guid isPermaLink="false">http://unscriptable.com/index.php/2007/04/17/inheritance-explained-2/</guid>
		<description><![CDATA[Most of the Javascript I write is object-oriented. While deciding what I would write about in my first posts, I realized that I would probably have to review Javascript OO techniques first. Any source code I included would be unintelligible to the uninitiated. In this post &#8212; and in the subsequent ones &#8212; I will [...]]]></description>
			<content:encoded><![CDATA[<p>Most of the Javascript I write is object-oriented. While deciding what I would write about in my first posts, I realized that I would probably have to review Javascript OO techniques first. Any source code I included would be unintelligible to the uninitiated. In this post &mdash; and in the subsequent ones &mdash; I will review the Javascript inheritance techniques I use and explain why and when I use them.</p>
<p><span id="more-697"></span></p>
<p>This article assumes that you, the reader, have a background in object-oriented programming techniques.  If not, please check out the primer at <a title="Object Oriented Programming at Wikipedia" href="http://en.wikipedia.org/wiki/Object_oriented">Wikipedia</a>.</p>
<p>Javascript is a <span style="font-style: italic">prototypal</span> language.  This means that objects inherit from other objects and that object classes describe their common behavior via prototypes.  Prototypes are akin to member declarations in classical inheritance.  For example, if a Javascript object, A, has a method, doSomething(), in its prototype, then another object of the same &#8220;class&#8221;, say object B, also has a method, doSomething(), in its prototype.  (Notice I quoted the word, &#8220;class&#8221;.  More on that below.)</p>
<p><a title="Prototype Chaining" href="http://unscriptable.dreamhosters.com/wp-content/uploads/2007/05/prototype-chaining.png"><img style="float: right; width: 200px;" src="http://unscriptable.dreamhosters.com/wp-content/uploads/2007/05/prototype-chaining.png" alt="Prototype Chaining" /></a><br />
If objects A and B inherit from another object, C, then they both inherit any members on C&#8217;s prototype.  This is because the prototypes are &#8220;chained&#8221;.  At run-time, when a member is referenced on an object, the prototype chain is scanned.  For example, if C has a method, doSomethingElse(), then calling A.doSomethingElse() is valid.</p>
<p>Like some other languages, Javascript uses the familiar <span style="font-style: italic">new</span> operator.  However, surprisingly, there is no formal method to declare a class.  Instead of using it on a class, the <span style="font-style: italic">new</span> operator is used on a function.  (In fact, it can be used on any function!)  The <span style="font-style: italic">new</span> operator simply interprets the function as an <em>object constructor</em> and pulls the function&#8217;s prototype into the new object&#8217;s prototype chain.</p>
<p>A prototype chain is similar in nature to a classical inheritance heirarchy.  Here is a simple example:</p>

<div class="wp_syntax"><div class="code"><pre class="javascript" style="font-family:monospace;"><span style="color: #003366; font-weight: bold;">var</span> Pet <span style="color: #339933;">=</span> <span style="color: #003366; font-weight: bold;">function</span> <span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span> <span style="color: #009900;">&#125;</span><span style="color: #339933;">;</span>
<span style="color: #006600; font-style: italic;">// using object literal (shorthand) notation:</span>
Pet.<span style="color: #660066;">prototype</span> <span style="color: #339933;">=</span> <span style="color: #009900;">&#123;</span>
  eat<span style="color: #339933;">:</span> <span style="color: #003366; font-weight: bold;">function</span> <span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span> <span style="color: #009966; font-style: italic;">/* do something here */</span> <span style="color: #009900;">&#125;</span><span style="color: #339933;">,</span>
  sleep<span style="color: #339933;">:</span> <span style="color: #003366; font-weight: bold;">function</span> <span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span> <span style="color: #009966; font-style: italic;">/* do something here */</span> <span style="color: #009900;">&#125;</span><span style="color: #339933;">,</span>
  play<span style="color: #339933;">:</span> <span style="color: #003366; font-weight: bold;">function</span> <span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span> <span style="color: #009966; font-style: italic;">/* do something here */</span> <span style="color: #009900;">&#125;</span>
<span style="color: #009900;">&#125;</span><span style="color: #339933;">;</span>
&nbsp;
<span style="color: #003366; font-weight: bold;">var</span> Cat <span style="color: #339933;">=</span> <span style="color: #003366; font-weight: bold;">function</span> <span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span> <span style="color: #009900;">&#125;</span><span style="color: #339933;">;</span>
Cat.<span style="color: #660066;">prototype</span> <span style="color: #339933;">=</span> <span style="color: #003366; font-weight: bold;">new</span> Pet<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span> <span style="color: #006600; font-style: italic;">// chain prototype</span>
Cat.<span style="color: #660066;">prototype</span>.<span style="color: #660066;">technicalName</span> <span style="color: #339933;">=</span> <span style="color: #3366CC;">'feline'</span><span style="color: #339933;">;</span>
Cat.<span style="color: #660066;">prototype</span>.<span style="color: #660066;">scratch</span> <span style="color: #339933;">=</span> <span style="color: #003366; font-weight: bold;">function</span> <span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span> <span style="color: #009966; font-style: italic;">/* do something here */</span> <span style="color: #009900;">&#125;</span><span style="color: #339933;">;</span>
Cat.<span style="color: #660066;">prototype</span>.<span style="color: #660066;">meow</span> <span style="color: #339933;">=</span> <span style="color: #003366; font-weight: bold;">function</span> <span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span> <span style="color: #000066; font-weight: bold;">return</span> <span style="color: #CC0000;">20</span><span style="color: #339933;">;</span> <span style="color: #009966; font-style: italic;">/* decibels */</span> <span style="color: #009900;">&#125;</span><span style="color: #339933;">;</span>
&nbsp;
<span style="color: #003366; font-weight: bold;">var</span> SiameseCat <span style="color: #339933;">=</span> <span style="color: #003366; font-weight: bold;">function</span> <span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span> <span style="color: #009900;">&#125;</span><span style="color: #339933;">;</span>
SiameseCat.<span style="color: #660066;">prototype</span> <span style="color: #339933;">=</span> <span style="color: #003366; font-weight: bold;">new</span> Cat<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span> <span style="color: #006600; font-style: italic;">// chain prototype</span>
SiameseCat.<span style="color: #660066;">prototype</span>.<span style="color: #660066;">breedName</span> <span style="color: #339933;">=</span> <span style="color: #3366CC;">'Siamese'</span><span style="color: #339933;">;</span>
SiameseCat.<span style="color: #660066;">prototype</span>.<span style="color: #660066;">meow</span> <span style="color: #339933;">=</span> <span style="color: #003366; font-weight: bold;">function</span> <span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
  <span style="color: #006600; font-style: italic;">// call ancestor method</span>
  <span style="color: #000066; font-weight: bold;">return</span> Cat.<span style="color: #660066;">prototype</span>.<span style="color: #660066;">meow</span>.<span style="color: #660066;">call</span><span style="color: #009900;">&#40;</span><span style="color: #000066; font-weight: bold;">this</span><span style="color: #009900;">&#41;</span> <span style="color: #339933;">*</span> <span style="color: #CC0000;">3</span><span style="color: #339933;">;</span>
<span style="color: #009900;">&#125;</span><span style="color: #339933;">;</span>
&nbsp;
<span style="color: #003366; font-weight: bold;">var</span> myCat <span style="color: #339933;">=</span> <span style="color: #003366; font-weight: bold;">new</span> SiameseCat<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
myCat.<span style="color: #660066;">play</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span> <span style="color: #006600; font-style: italic;">// inherited from Pet</span>
myCat.<span style="color: #660066;">scratch</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span> <span style="color: #006600; font-style: italic;">// inherited from Cat</span>
myCat.<span style="color: #660066;">meow</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span> <span style="color: #006600; font-style: italic;">// from SiameseCat</span></pre></div></div>

<p>However, since Javascript is so flexible, it lends itself to several variations and alternatives.  This is where the fun begins&#8230;</p>
]]></content:encoded>
			<wfw:commentRss>http://unscriptable.com/2007/04/17/inheritance-explained/feed/</wfw:commentRss>
		<slash:comments>3</slash:comments>
		</item>
	</channel>
</rss>

