<?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>David Walsh Blog</title>
	<atom:link href="http://davidwalsh.name/feed" rel="self" type="application/rss+xml" />
	<link>http://davidwalsh.name</link>
	<description>JavaScript, HTML5, AJAX, PHP, CSS, and ∞.</description>
	<lastBuildDate>Sun, 19 May 2013 05:09:49 +0000</lastBuildDate>
	<language>en-US</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.5.1</generator>
		<item>
		<title>Windows 8 Pin&#160;METAs</title>
		<link>http://davidwalsh.name/windows-8-pin-metas</link>
		<comments>http://davidwalsh.name/windows-8-pin-metas#comments</comments>
		<pubDate>Thu, 16 May 2013 20:18:43 +0000</pubDate>
		<dc:creator>David Walsh</dc:creator>
				<category><![CDATA[Markup]]></category>
		<category><![CDATA[Microsoft]]></category>
		<category><![CDATA[Mobile]]></category>

		<guid isPermaLink="false">http://davidwalsh.name/?p=6434</guid>
		<description><![CDATA[Windows 8 allows for adding websites as apps (or maybe &#8220;bookmarks&#8221; is a better term) to the home screen, much in the vein that iOS allows users to do the same.  Like iOS devices, Windows 8 allows  users to accomplish this same task using custom META tags embedded within the page HTML: &#60;meta name="msapplication-TileColor" content="#FF0000" [...]<p><p>Read the full article at:  <a href="http://davidwalsh.name/windows-8-pin-metas">Windows 8 Pin&nbsp;METAs</a></p>

<p><a href="http://dwf.tw/treehouse"><img src="http://davidwalsh.name/wp-content/themes/jack/images/treehouse-2.png" alt="Treehouse"></a></p>

<p><a href="http://dwf.tw/senchacon"><img src="http://davidwalsh.name/wp-content/themes/jack/images/senchaCon.png" alt="Sencha Conference"></a></p></p>
]]></description>
				<content:encoded><![CDATA[<p>Windows 8 allows for adding websites as apps (or maybe &#8220;bookmarks&#8221; is a better term) to the home screen, much in the vein that <a href="http://davidwalsh.name/set-sites-apple-touch-icon">iOS allows users to do the same</a>.  Like iOS devices, Windows 8 allows  users to accomplish this same task using custom META tags embedded within the page HTML:</p>

<pre class="html">
&lt;meta name="msapplication-TileColor" content="#FF0000" /&gt;
&lt;meta name="msapplication-TileImage" content="/windows8-icon.png" /&gt;
</pre>

<p>The image size should be 144&#215;144 and you&#8217;ll want to define a custom background color with the META tag above.  Tags like these are invaluable &#8212; very little HTML to add but a giant convenience to users.  Remember that the easier it is to get to your site, the more likely they will come back often!</p><p><p>Read the full article at:  <a href="http://davidwalsh.name/windows-8-pin-metas">Windows 8 Pin&nbsp;METAs</a></p>

<p><a href="http://dwf.tw/treehouse"><img src="http://davidwalsh.name/wp-content/themes/jack/images/treehouse-2.png" alt="Treehouse"></a></p>

<p><a href="http://dwf.tw/senchacon"><img src="http://davidwalsh.name/wp-content/themes/jack/images/senchaCon.png" alt="Sencha Conference"></a></p></p>
]]></content:encoded>
			<wfw:commentRss>http://davidwalsh.name/windows-8-pin-metas/feed</wfw:commentRss>
		<slash:comments>3</slash:comments>
		</item>
		<item>
		<title>Firefox Button Height&#160;Fix</title>
		<link>http://davidwalsh.name/firefox-buttons</link>
		<comments>http://davidwalsh.name/firefox-buttons#comments</comments>
		<pubDate>Thu, 16 May 2013 14:53:15 +0000</pubDate>
		<dc:creator>David Walsh</dc:creator>
				<category><![CDATA[CSS]]></category>
		<category><![CDATA[Quick Tips]]></category>

		<guid isPermaLink="false">http://davidwalsh.name/?p=7114</guid>
		<description><![CDATA[One problem I&#8217;ve seen on occasion is that button heights in Firefox are a few pixels larger than in other browsers.  This can be a nightmare when trying to unify the size of buttons with an A elements, as we found out when implementing a new feature on MDN: The middle element is an INPUT [...]<p><p>Read the full article at:  <a href="http://davidwalsh.name/firefox-buttons">Firefox Button Height&nbsp;Fix</a></p>

<p><a href="http://dwf.tw/treehouse"><img src="http://davidwalsh.name/wp-content/themes/jack/images/treehouse-2.png" alt="Treehouse"></a></p>

<p><a href="http://dwf.tw/senchacon"><img src="http://davidwalsh.name/wp-content/themes/jack/images/senchaCon.png" alt="Sencha Conference"></a></p></p>
]]></description>
				<content:encoded><![CDATA[<p>One problem I&#8217;ve seen on occasion is that button heights in Firefox are a few pixels larger than in other browsers.  This can be a nightmare when trying to unify the size of buttons with an <code>A</code> elements, as we found out when implementing a new feature on MDN:</p>

<p><img src="http://davidwalsh.name/demo/firefox-button-size.png" alt="Firefox Button Bug" /></p>

<p>The middle element is an <code>INPUT</code> and the others are your basic <code>A</code> element.  You can see that the <code>INPUT</code> element is just slightly bigger &#8212; enough that it annoys anyone with a sharp eye.  <a href="http://stackoverflow.com/questions/8859908/buttons-too-tall-on-firefox">This StackOverflow post</a> mentions a solution that includes <code>::-moz-focus-inner</code>:</p>

<pre class="css">
input::-moz-focus-inner { 
	border: 0; 
	padding: 0; 
}
</pre>

<p>This snippet helps but doesn&#8217;t completely fix the problem &#8212; the <code>INPUT</code> is still just a tad too big.  The Stackoverflow post mentions <code>box-sizing</code> but that doesn&#8217;t help.  After trying a few different solutions, I finally found one that works:</p>

<pre class="css">
input::-moz-focus-inner { 
	border: 0; 
	padding: 0; 
	margin-top:-2px; 
	margin-bottom: -2px; 
}
</pre>

<p>Setting negative margins within the <code>::-moz-focus-inner</code> brings the buttons down to correct height, and as a bonus, the text sits the proper position:</p>

<p><img src="http://davidwalsh.name/demo/firefox-button-size-fix.png" alt="Firefox Button Bug Fix" /></p>

<p>The ultimate solution is a bit vomit-inducing, and you may need to adjust the negative margins to your specific use case, but the solution works and doesn&#8217;t cause problems for other browsers!</p><p><p>Read the full article at:  <a href="http://davidwalsh.name/firefox-buttons">Firefox Button Height&nbsp;Fix</a></p>

<p><a href="http://dwf.tw/treehouse"><img src="http://davidwalsh.name/wp-content/themes/jack/images/treehouse-2.png" alt="Treehouse"></a></p>

<p><a href="http://dwf.tw/senchacon"><img src="http://davidwalsh.name/wp-content/themes/jack/images/senchaCon.png" alt="Sencha Conference"></a></p></p>
]]></content:encoded>
			<wfw:commentRss>http://davidwalsh.name/firefox-buttons/feed</wfw:commentRss>
		<slash:comments>9</slash:comments>
		</item>
		<item>
		<title>CSS&#160;Counters</title>
		<link>http://davidwalsh.name/css-counters</link>
		<comments>http://davidwalsh.name/css-counters#comments</comments>
		<pubDate>Wed, 15 May 2013 12:01:36 +0000</pubDate>
		<dc:creator>David Walsh</dc:creator>
				<category><![CDATA[CSS]]></category>

		<guid isPermaLink="false">http://davidwalsh.name/?p=6808</guid>
		<description><![CDATA[Counters.  They were a staple of the Geocities / early web scene that many of us &#8220;older&#8221; developers grew up with;  a feature then, the butt of web jokes now.  CSS has implemented its own type of counter, one more sane and straight-forward than the ole &#8220;hit counter.&#8221;  CSS counters allow for simple CSS-based incrementing [...]<p><p>Read the full article at:  <a href="http://davidwalsh.name/css-counters">CSS&nbsp;Counters</a></p>

<p><a href="http://dwf.tw/treehouse"><img src="http://davidwalsh.name/wp-content/themes/jack/images/treehouse-2.png" alt="Treehouse"></a></p>

<p><a href="http://dwf.tw/senchacon"><img src="http://davidwalsh.name/wp-content/themes/jack/images/senchaCon.png" alt="Sencha Conference"></a></p></p>
]]></description>
				<content:encoded><![CDATA[<p>Counters.  They were a staple of the Geocities / early web scene that many of us &#8220;older&#8221; developers grew up with;  a feature then, the butt of web jokes now.  CSS has implemented its own type of counter, one more sane and straight-forward than the ole &#8220;hit counter.&#8221;  CSS counters allow for simple CSS-based incrementing and display of a number for generated content.  Let&#8217;s have a quick look at how CSS counters work!</p>

<div class="actions"><a href="http://davidwalsh.name/demo/css-counters.php" class="demo">View Demo</a><div class="clear"></div></div>

<h2>Initializing the CSS&nbsp;Counter</h2>

<p>For the sake of semantics, let&#8217;s displays a series of slides using <code>&lt;OL&gt;</code> and <code>&lt;LI&gt;</code> elements for our counter example.  The first step is resetting the counter, providing the specific counter a name initializing it to <code>0</code>:</p>

<pre class="css">
ol.slides {
	counter-reset: slideNum;
}
</pre>

<p>The counter name <code>slideNum</code> will be used throughout the example.</p>

<h2>Incrementing the CSS&nbsp;Counter</h2>

<p>To increment the counter, use the counter-increment property and the counter name created in the first step:</p>

<pre class="css">
ol.slides > li {
	counter-increment: slideNum;
}
</pre>

<p>Each counter-increment call continues to increment the counter.  Be aware that you must use <code>&gt;</code> in your selector if you have nested lists so that your list numbering doesn&#8217;t get incremented at the wrong points.</p>

<h2>Using the Counter&nbsp;Value</h2>

<p>Counters aren&#8217;t much good if you can&#8217;t display them (just like in 1995!), so the counter() command will output the counter value when used with the content property:</p>

<pre class="css">
ol.slides li:after {
	content: "[" counter(slideNum) "]";
}
</pre>

<p>Oddly enough, counters can also be nested by using a second argument to counter, which will act as a separater:</p>

<pre class="css">
/* assume this HTML:

&lt;ol class="toc"&gt;
	&lt;li&gt;Intro&lt;/li&gt;
	&lt;li&gt;Topic
		&lt;ol&gt;
			&lt;li&gt;Subtopic&lt;/li&gt;
			&lt;li&gt;Subtopic&lt;/li&gt;
			&lt;li&gt;Subtopic&lt;/li&gt;
		&lt;/ol&gt;
	&lt;/li&gt;
	&lt;li&gt;Topic
		&lt;ol&gt;
			&lt;li&gt;Subtopic&lt;/li&gt;
			&lt;li&gt;Subtopic&lt;/li&gt;
			&lt;li&gt;Subtopic&lt;/li&gt;
		&lt;/ol&gt;
	&lt;/li&gt;
	&lt;li&gt;Closing&lt;/li&gt;		
&lt;/ul&gt;
*/

ol.toc, ol.toc ol {
	counter-reset: toc;
}
ol li {
	counter-increment: toc;
}
.toc li:before {
	content: "(Item " counters(toc, ".") ")";
}

/* visible page "output"

&lt;ol class="toc"&gt;
	&lt;li&gt;(Item 1)Intro&lt;/li&gt;
	&lt;li&gt;(Item 2)Topic
		&lt;ol&gt;
			&lt;li&gt;(Item 2.1)Subtopic&lt;/li&gt;
			&lt;li&gt;(Item 2.2)Subtopic&lt;/li&gt;
			&lt;li&gt;(Item 2.3)Subtopic&lt;/li&gt;
		&lt;/ol&gt;
	&lt;/li&gt;
	&lt;li&gt;(Item 3)Topic
		&lt;ol&gt;
			&lt;li&gt;(Item 3.1)Subtopic&lt;/li&gt;
			&lt;li&gt;(Item 3.2)Subtopic&lt;/li&gt;
			&lt;li&gt;(Item 3.3)Subtopic&lt;/li&gt;
		&lt;/ol&gt;
	&lt;/li&gt;
	&lt;li&gt;(Item 4)Closing&lt;/li&gt;		
&lt;/ul&gt;
*/
</pre>

<p>As you can probably tell, counters could be incredibly helpful when it comes to basic count display up to a table of contents display.</p>

<div class="actions"><a href="http://davidwalsh.name/demo/css-counters.php" class="demo">View Demo</a><div class="clear"></div></div>

<p>After you get over the initial giggle of the the word counter, you start to realize that they can be quite useful.  In most cases, you&#8217;ll use counters with :after and :before pseudo-elements, unless you dedicate empty elements to their contents.  I&#8217;ve seen counters used in broadcast slides, slideshows, and documentation.  Do you use counters for anything else?</p><p><p>Read the full article at:  <a href="http://davidwalsh.name/css-counters">CSS&nbsp;Counters</a></p>

<p><a href="http://dwf.tw/treehouse"><img src="http://davidwalsh.name/wp-content/themes/jack/images/treehouse-2.png" alt="Treehouse"></a></p>

<p><a href="http://dwf.tw/senchacon"><img src="http://davidwalsh.name/wp-content/themes/jack/images/senchaCon.png" alt="Sencha Conference"></a></p></p>
]]></content:encoded>
			<wfw:commentRss>http://davidwalsh.name/css-counters/feed</wfw:commentRss>
		<slash:comments>4</slash:comments>
		</item>
		<item>
		<title>CSS Idea:&#160;/*autoprefix*/</title>
		<link>http://davidwalsh.name/css-idea-autoprefix</link>
		<comments>http://davidwalsh.name/css-idea-autoprefix#comments</comments>
		<pubDate>Tue, 14 May 2013 14:51:57 +0000</pubDate>
		<dc:creator>David Walsh</dc:creator>
				<category><![CDATA[CSS]]></category>
		<category><![CDATA[Theory / Ideas]]></category>

		<guid isPermaLink="false">http://davidwalsh.name/?p=6868</guid>
		<description><![CDATA[Using utilities likes SASS and Compass have made me me think a lot about how we can improve CSS in the short term.  Of course all CSS proposals should go through the W3C standardization process, but the problem is that those processes take a long time.  We also know that there are a lot of [...]<p><p>Read the full article at:  <a href="http://davidwalsh.name/css-idea-autoprefix">CSS Idea:&nbsp;/*autoprefix*/</a></p>

<p><a href="http://dwf.tw/treehouse"><img src="http://davidwalsh.name/wp-content/themes/jack/images/treehouse-2.png" alt="Treehouse"></a></p>

<p><a href="http://dwf.tw/senchacon"><img src="http://davidwalsh.name/wp-content/themes/jack/images/senchaCon.png" alt="Sencha Conference"></a></p></p>
]]></description>
				<content:encoded><![CDATA[<p>Using utilities likes SASS and Compass have made me me think a lot about how we can improve CSS in the short term.  Of course all CSS proposals should go through the W3C standardization process, but the problem is that those processes take a long time.  We also know that there are a lot of things we know we need in CSS:</p>

<ul>
	<li>CSS Variables</li>
	<li>Nested style blocks (a la SASS, LESS, etc.)</li>
	<li>Macros / routines</li>
</ul>
<p>We know we need these things but still don&#8217;t have them yet, and likely wont for years.  That got me to thinking about another problem:  the constant need to do the prefixing of properties for every browser, like this mess:</p>

<pre class="css">
#someElement {
	-moz-animation-name: myAnimation;
	-webkit-animation-name: myAnimation;
	-ms-animation-name: myAnimation;
	animation-name: myAnimation;
}
</pre>

<p>Firefox has un-prefixed a majority of previously prefixed properties, but we still can&#8217;t get away from this mess for other browsers.  So how can we fix this in the short term, besides making browser vendors unprefix?  One idea is doing something <em>kind of</em> like JavaScript&#8217;s <code>use strict</code>;  we start our CSS file with something like this comment:</p>

<pre class="css">
/*autoprefix*/



/* and then somewhere in the CSS file */
#someElement {
	animation-name: myAnimation;
}
</pre>

<p>If a CSS file started with this CSS comment, the browser would automatically prefix properties that required prefixing.  Since browsers know which properties need prefixing, and they only need to account for their prefix, this type of system would reduce code (and thus smaller/quicker file downloads) and, in theory, be much faster to implement.</p>

<p>Now this isn&#8217;t a perfect solution by any means.  It should still be standardized at some point and unfortunately some prefixed properties act differently than unprefixed.  Just a thought that I wanted to share, get some feedback on.  Let me know if you think something like this would work!</p><p><p>Read the full article at:  <a href="http://davidwalsh.name/css-idea-autoprefix">CSS Idea:&nbsp;/*autoprefix*/</a></p>

<p><a href="http://dwf.tw/treehouse"><img src="http://davidwalsh.name/wp-content/themes/jack/images/treehouse-2.png" alt="Treehouse"></a></p>

<p><a href="http://dwf.tw/senchacon"><img src="http://davidwalsh.name/wp-content/themes/jack/images/senchaCon.png" alt="Sencha Conference"></a></p></p>
]]></content:encoded>
			<wfw:commentRss>http://davidwalsh.name/css-idea-autoprefix/feed</wfw:commentRss>
		<slash:comments>14</slash:comments>
		</item>
		<item>
		<title>Multiple Background CSS&#160;Animations</title>
		<link>http://davidwalsh.name/multiple-background-css-animations</link>
		<comments>http://davidwalsh.name/multiple-background-css-animations#comments</comments>
		<pubDate>Mon, 13 May 2013 14:29:37 +0000</pubDate>
		<dc:creator>David Walsh</dc:creator>
				<category><![CDATA[CSS]]></category>
		<category><![CDATA[CSS Animations]]></category>
		<category><![CDATA[Markup]]></category>

		<guid isPermaLink="false">http://davidwalsh.name/?p=6804</guid>
		<description><![CDATA[CSS background animation has been a hot topic for a long time, mostly because they look pretty sweet and don&#8217;t require additional elements.  I was recently asked if it was possible to have multiple background animations on a given element and the answer is yes&#8230;with concessions.  Let&#8217;s take a look at how it&#8217;s done! View [...]<p><p>Read the full article at:  <a href="http://davidwalsh.name/multiple-background-css-animations">Multiple Background CSS&nbsp;Animations</a></p>

<p><a href="http://dwf.tw/treehouse"><img src="http://davidwalsh.name/wp-content/themes/jack/images/treehouse-2.png" alt="Treehouse"></a></p>

<p><a href="http://dwf.tw/senchacon"><img src="http://davidwalsh.name/wp-content/themes/jack/images/senchaCon.png" alt="Sencha Conference"></a></p></p>
]]></description>
				<content:encoded><![CDATA[<p><a href="http://davidwalsh.name/background-animation-css">CSS background animation</a> has been a hot topic for a long time, mostly because they look pretty sweet and don&#8217;t require additional elements.  I was recently asked if it was possible to have multiple background animations on a given element and the answer is yes&#8230;with concessions.  Let&#8217;s take a look at how it&#8217;s done!</p>

<div class="actions"><a href="http://davidwalsh.name/demo/multiple-background-css-animations.php" class="demo">View Demo</a><div class="clear"></div></div>

<h2>The&nbsp;CSS</h2>

<p>So multiple background images on an element is something we&#8217;ve been able to do for quite a while now, simply separate them with commas:</p>

<pre class="css">
.animate-area	{ 
	background-image: url(twitter-logo-bird.png), url(treehouseFrog.png), url(bg-clouds.png);
	background-position: 20px -90px, 30px 80px, 0px 0px;
	background-repeat: no-repeat, no-repeat, repeat-x;
}
</pre>

<p>Note that the background image that you want at the top of the stack should go first in the image list.  Animating the backgrounds requires moving the background-position, also separated by commas:</p>

<pre class="css">
@keyframes animatedBird {
	from { background-position: 20px 20px, 30px 80px, 0 0; }
	to { background-position: 300px -90px, 30px 20px, 100% 0; }
}
		
.animate-area	{ 
	animation: animatedBird 4s linear infinite;
}
</pre>

<p>The result is three moving pieces inside one element!</p>

<div class="actions"><a href="http://davidwalsh.name/demo/multiple-background-css-animations.php" class="demo">View Demo</a><div class="clear"></div></div>

<p>Of course this isn&#8217;t an ideal case because you can&#8217;t separately change background-positions and thus you need to work with the same duration for each background image.  Multiple animations can be set on selectors with CSS, but since it&#8217;s the one property being changed, we&#8217;re just out of luck!</p><p><p>Read the full article at:  <a href="http://davidwalsh.name/multiple-background-css-animations">Multiple Background CSS&nbsp;Animations</a></p>

<p><a href="http://dwf.tw/treehouse"><img src="http://davidwalsh.name/wp-content/themes/jack/images/treehouse-2.png" alt="Treehouse"></a></p>

<p><a href="http://dwf.tw/senchacon"><img src="http://davidwalsh.name/wp-content/themes/jack/images/senchaCon.png" alt="Sencha Conference"></a></p></p>
]]></content:encoded>
			<wfw:commentRss>http://davidwalsh.name/multiple-background-css-animations/feed</wfw:commentRss>
		<slash:comments>3</slash:comments>
		</item>
		<item>
		<title>Referencing a Script&#8217;s Own&#160;Tag</title>
		<link>http://davidwalsh.name/script-tag</link>
		<comments>http://davidwalsh.name/script-tag#comments</comments>
		<pubDate>Wed, 08 May 2013 13:06:43 +0000</pubDate>
		<dc:creator>David Walsh</dc:creator>
				<category><![CDATA[JavaScript]]></category>
		<category><![CDATA[Markup]]></category>

		<guid isPermaLink="false">http://davidwalsh.name/?p=6433</guid>
		<description><![CDATA[There are times when the contents of an external script may want to reference its own SCRIPT tag.  There are times that developers may want to detect attributes of the script tag which act as options for the script; this is a practice that&#8217;s been done by the Dojo Toolkit for years.  Lea Verou&#8217;s Prism [...]<p><p>Read the full article at:  <a href="http://davidwalsh.name/script-tag">Referencing a Script&#8217;s Own&nbsp;Tag</a></p>

<p><a href="http://dwf.tw/treehouse"><img src="http://davidwalsh.name/wp-content/themes/jack/images/treehouse-2.png" alt="Treehouse"></a></p>

<p><a href="http://dwf.tw/senchacon"><img src="http://davidwalsh.name/wp-content/themes/jack/images/senchaCon.png" alt="Sencha Conference"></a></p></p>
]]></description>
				<content:encoded><![CDATA[<p>There are times when the contents of an external script may want to reference its own <code>SCRIPT</code> tag.  There are times that developers may want to detect attributes of the script tag which act as options for the script; this is a practice that&#8217;s been done by the Dojo Toolkit for years.  Lea Verou&#8217;s Prism syntax highlighter also uses this practice:</p>

<pre class="html">
&lt;!-- Traditional Dojo config --&gt;
&lt;script src="//ajax.googleapis.com/ajax/libs/dojo/1.8.3/dojo/dojo.js"
               data-dojo-config="async: true"&gt;&lt;/script&gt;

&lt;!-- Sample Prism config --&gt;
&lt;script src="prism.js" data-default-language="markup" data-manual&gt;&lt;/script&gt;
</pre>

<p>So how are these projects getting the correct SCRIPT tag with which to look for attributes?  Assuming a top-down (<em>not</em> async) load of scripts, the following will get a script its own tag:</p>

<pre class="js">
/* From in side the script.... */

// Reliably grab my script tag
var script = document.getElementsByTagName("script");
script = script[script.length - 1];

// Check for an attribute/config
if(script.hasAttribute('data-something')) {
	// Do something!
}
</pre>

<p>Collect the SCRIPT elements up to that point and reference the last one &#8212; that&#8217;s all you need to do.  Of course we&#8217;re living in a mostly async world right now, so to accommodate for those cases, you may need to stick an <code>ID</code> on the <code>SCRIPT</code> element, take a chance matching the <code>SCRIPT</code> by path (<em>a really bad idea)</em>, or use a different type of configuration system.</p><p><p>Read the full article at:  <a href="http://davidwalsh.name/script-tag">Referencing a Script&#8217;s Own&nbsp;Tag</a></p>

<p><a href="http://dwf.tw/treehouse"><img src="http://davidwalsh.name/wp-content/themes/jack/images/treehouse-2.png" alt="Treehouse"></a></p>

<p><a href="http://dwf.tw/senchacon"><img src="http://davidwalsh.name/wp-content/themes/jack/images/senchaCon.png" alt="Sencha Conference"></a></p></p>
]]></content:encoded>
			<wfw:commentRss>http://davidwalsh.name/script-tag/feed</wfw:commentRss>
		<slash:comments>8</slash:comments>
		</item>
		<item>
		<title>GitHub and Mozilla Work&#160;Process</title>
		<link>http://davidwalsh.name/mozilla-github</link>
		<comments>http://davidwalsh.name/mozilla-github#comments</comments>
		<pubDate>Tue, 07 May 2013 11:53:57 +0000</pubDate>
		<dc:creator>David Walsh</dc:creator>
				<category><![CDATA[Blog]]></category>

		<guid isPermaLink="false">http://davidwalsh.name/?p=6658</guid>
		<description><![CDATA[Nicholas Zakas sent out a call to Twitter asking developers who use GitHub as a primary CVS to share their working process.  Mozilla uses GitHub for many (if not most, but I can&#8217;t confirm that) mission critical web applications so I chimed in on how the Mozilla Developer Network team uses GitHub.  I got many [...]<p><p>Read the full article at:  <a href="http://davidwalsh.name/mozilla-github">GitHub and Mozilla Work&nbsp;Process</a></p>

<p><a href="http://dwf.tw/treehouse"><img src="http://davidwalsh.name/wp-content/themes/jack/images/treehouse-2.png" alt="Treehouse"></a></p>

<p><a href="http://dwf.tw/senchacon"><img src="http://davidwalsh.name/wp-content/themes/jack/images/senchaCon.png" alt="Sencha Conference"></a></p></p>
]]></description>
				<content:encoded><![CDATA[<p><a href="https://twitter.com/slicknet" rel="nofollow">Nicholas Zakas</a> sent out a call to Twitter asking developers who use GitHub as a primary CVS to share their working process.  Mozilla uses GitHub for many (if not most, but I can&#8217;t confirm that) mission critical web applications so I chimed in on how the Mozilla Developer Network team uses GitHub.  I got many questions about the process from different sources so I thought I would document our process here.</p>

<h2>Working&nbsp;Setup</h2>

<p>The working process starts with each developer cloning <a href="http://github.com/mozilla/kuma" rel="nofollow">Mozilla&#8217;s kuma repository</a>.  The kuma repository includes developer instructions for getting setup with vagrant (recommended) or without.  Kuma is a Python/Django app with a MySQL database backend.  The kuma repository doesn&#8217;t include all of MDN&#8217;s documentation but that can be provided to developers who ask for it.  The MDN team resides in the #mdn IRC room for questions and planning.  Filed bugs for MDN can be found <a href="https://bugzilla.mozilla.org/buglist.cgi?product=Mozilla%20Developer%20Network&amp;list_id=6467030" rel="nofollow">on Bugzilla</a>.  This information is all you need to know to get started.</p>

<h2>Working on&nbsp;Bugs/Enhancements</h2>

<p>Let&#8217;s say you&#8217;re set up and it&#8217;s time to work on a bug or enhancement.  The first step is to create a separate feature branch for working on the bug:</p>

<pre class="shell">
# Ensure we're on master
git checkout master

# Create and check out feature branch
git checkout -b bug-title-123456
</pre>

<p>Putting the bug number in the branch name is especially useful if there are long periods between working on the bug.  With the branch created and checked out, make your updates, additions, and removals.  It&#8217;s also important to run tests as you work on the bug.</p>

<h2>Submitting Pull&nbsp;Requests</h2>

<p>Before submitting a pull request, it&#8217;s important to squash commits and rebase on master branch:</p>

<pre class="shell">
# Ensure master is up to date with Mozilla's remote
git checkout master
git pull mozilla master

# Go back to feature branch, squash commits
git checkout bug-title-123456
git merge master
git rebase -i master
git push origin bug-123456
</pre>

<p class="note"><em>(Note:  I&#8217;m not a git legend so there&#8217;s a good possibility I&#8217;m taking the long route to accomplish this task &#8212; if so, let me know)</em></p>

<p>The code above turns all commits into one commit, so it&#8217;s easier to back out if necessary.  We also start commit messages with &#8220;fix bug {bug number}&#8221; &#8212; this allows out post-merge hook to automatically <code>RESOLVED:FIXED</code> the bug in Bugzilla.  If all looks good, submit the pull request via GitHub.</p>

<h2>PR and Code&nbsp;Review</h2>

<p>Upon submission of the pull request, the code is automatically run through Mozilla&#8217;s Jenkins test suite.  If the tests pass, the pull request is technically able to be merged.  At this point, however, a different member or members of the team review the submitted code, making comments on possible improvements or problems that may be come up along the way.  If the submitter needs to make more changes, they should make their commits, squash and rebase again, and push:</p>

<pre class="shell">
git push -f
</pre>

<p>So that&#8217;s the MDN team&#8217;s process for managing code on GitHub.  It all seems pretty standard and straight-forward but let me know if you have suggestions or questions.  I like the way things work and the rest of the team seems to as well!</p><p><p>Read the full article at:  <a href="http://davidwalsh.name/mozilla-github">GitHub and Mozilla Work&nbsp;Process</a></p>

<p><a href="http://dwf.tw/treehouse"><img src="http://davidwalsh.name/wp-content/themes/jack/images/treehouse-2.png" alt="Treehouse"></a></p>

<p><a href="http://dwf.tw/senchacon"><img src="http://davidwalsh.name/wp-content/themes/jack/images/senchaCon.png" alt="Sencha Conference"></a></p></p>
]]></content:encoded>
			<wfw:commentRss>http://davidwalsh.name/mozilla-github/feed</wfw:commentRss>
		<slash:comments>3</slash:comments>
		</item>
		<item>
		<title>Multiple Facebook Accounts on&#160;iOS</title>
		<link>http://davidwalsh.name/multiple-facebook-accounts</link>
		<comments>http://davidwalsh.name/multiple-facebook-accounts#comments</comments>
		<pubDate>Mon, 06 May 2013 11:54:44 +0000</pubDate>
		<dc:creator>David Walsh</dc:creator>
				<category><![CDATA[Mobile]]></category>

		<guid isPermaLink="false">http://davidwalsh.name/?p=6670</guid>
		<description><![CDATA[When little Jackson London Walsh was born, I pondered whether we should give him his own Facebook account or simply have my wife and I post to our personal accounts.  We decided to get Jack his own account because: we want to be able to have photos all go to the same account we want [...]<p><p>Read the full article at:  <a href="http://davidwalsh.name/multiple-facebook-accounts">Multiple Facebook Accounts on&nbsp;iOS</a></p>

<p><a href="http://dwf.tw/treehouse"><img src="http://davidwalsh.name/wp-content/themes/jack/images/treehouse-2.png" alt="Treehouse"></a></p>

<p><a href="http://dwf.tw/senchacon"><img src="http://davidwalsh.name/wp-content/themes/jack/images/senchaCon.png" alt="Sencha Conference"></a></p></p>
]]></description>
				<content:encoded><![CDATA[<img src="http://davidwalsh.name/demo/jackson-london.jpg" alt="Jackson London Walsh" class="image" />

<p>When little Jackson London Walsh was born, I pondered whether we should give him his own Facebook account or simply have my wife and I post to our personal accounts.  We decided to get Jack his own account because:</p>
<ol>
	<li>we want to be able to have photos all go to the same account</li>
	<li>we want all of his stuff to be private</li>
	<li>we didn&#8217;t want to add a bunch of noise to our private accounts for those <del>heartless bastards</del> who don&#8217;t care about seeing baby photos all the time</li>
	<li>we hope to hand the account over to him one day</li>
</ol>
<p>Facebook is now baked into iOS&#8217; settings but iOS presently only allows for one Facebook account to be stored.  This initially presented a problem, as we had hoped to quickly take photos and upload to Facebook.  I did, however, find a method to manage multiple Facebook accounts within the app itself.</p>

<p>The key is opening the Facebook app and swiping to the main menu on the left.  Scroll down to the bottom and there&#8217;s a &#8220;Log Out&#8221; item.</p>

<p><img src="http://davidwalsh.name/demo/facebook-process-1.png" alt="Facebook Multiple Accounts" /></p>

<p>Tap that, confirm, and you&#8217;ll be presented with a screen that has a &#8220;Continue&#8221; button (to log in as the Facebook account tied to your iOS install) an a &#8220;(Not You?)&#8221; link;  tap that.</p>

<p><img src="http://davidwalsh.name/demo/facebook-process-2.png" alt="Facebook Multiple Accounts" /></p>

<p>You can then sign in with a different email address and password:</p>

<p><img src="http://davidwalsh.name/demo/facebook-process-3.png" alt="Facebook Multiple Accounts" /></p>

<p>What a relief that signing in with multiple accounts is possible.  Now we get to post photos of our little prince whenever we&#8217;d like!</p><p><p>Read the full article at:  <a href="http://davidwalsh.name/multiple-facebook-accounts">Multiple Facebook Accounts on&nbsp;iOS</a></p>

<p><a href="http://dwf.tw/treehouse"><img src="http://davidwalsh.name/wp-content/themes/jack/images/treehouse-2.png" alt="Treehouse"></a></p>

<p><a href="http://dwf.tw/senchacon"><img src="http://davidwalsh.name/wp-content/themes/jack/images/senchaCon.png" alt="Sencha Conference"></a></p></p>
]]></content:encoded>
			<wfw:commentRss>http://davidwalsh.name/multiple-facebook-accounts/feed</wfw:commentRss>
		<slash:comments>2</slash:comments>
		</item>
		<item>
		<title>Prepend and Append Files with&#160;.htaccess</title>
		<link>http://davidwalsh.name/prepend-append-files-htaccess</link>
		<comments>http://davidwalsh.name/prepend-append-files-htaccess#comments</comments>
		<pubDate>Fri, 03 May 2013 12:10:28 +0000</pubDate>
		<dc:creator>David Walsh</dc:creator>
				<category><![CDATA[.htaccess]]></category>
		<category><![CDATA[Apache / Server]]></category>
		<category><![CDATA[PHP]]></category>
		<category><![CDATA[Quick Tips]]></category>

		<guid isPermaLink="false">http://davidwalsh.name/?p=6394</guid>
		<description><![CDATA[One of the lessor known and used capabilities of .htaccess files is the ability to prepend and append includes to every page request.  Doing so avoids needing to code &#60;?php require('footer.php'); ?&#62; in every template file you wat to use them in.  Here&#8217;s the .htaccess code: # Prepend the file php_value auto_prepend_file "/dir/path/utilities.php" # Append [...]<p><p>Read the full article at:  <a href="http://davidwalsh.name/prepend-append-files-htaccess">Prepend and Append Files with&nbsp;.htaccess</a></p>

<p><a href="http://dwf.tw/treehouse"><img src="http://davidwalsh.name/wp-content/themes/jack/images/treehouse-2.png" alt="Treehouse"></a></p>

<p><a href="http://dwf.tw/senchacon"><img src="http://davidwalsh.name/wp-content/themes/jack/images/senchaCon.png" alt="Sencha Conference"></a></p></p>
]]></description>
				<content:encoded><![CDATA[<p>One of the lessor known and used capabilities of .htaccess files is the ability to prepend and append includes to every page request.  Doing so avoids needing to code <code>&lt;?php require('footer.php'); ?&gt;</code> in every template file you wat to use them in.  Here&#8217;s the .htaccess code:</p>

<pre class="htaccess">
# Prepend the file
php_value auto_prepend_file "/dir/path/utilities.php"

# Append file to bottom of page
php_value auto_append_file "/dir/path/templates/footer.php"
</pre>

<p>Now don&#8217;t mistake this post as me telling you to use this strategy;  using this functionality creates a layer of indirection that could confuse a team of developers if they don&#8217;t all have a grasp of where automatically included files are coming from.  Just wanted to let you know this was possible!</p><p><p>Read the full article at:  <a href="http://davidwalsh.name/prepend-append-files-htaccess">Prepend and Append Files with&nbsp;.htaccess</a></p>

<p><a href="http://dwf.tw/treehouse"><img src="http://davidwalsh.name/wp-content/themes/jack/images/treehouse-2.png" alt="Treehouse"></a></p>

<p><a href="http://dwf.tw/senchacon"><img src="http://davidwalsh.name/wp-content/themes/jack/images/senchaCon.png" alt="Sencha Conference"></a></p></p>
]]></content:encoded>
			<wfw:commentRss>http://davidwalsh.name/prepend-append-files-htaccess/feed</wfw:commentRss>
		<slash:comments>4</slash:comments>
		</item>
		<item>
		<title>Kickstart Kyle&#160;Simpson</title>
		<link>http://davidwalsh.name/kickstart-kyle-simpson</link>
		<comments>http://davidwalsh.name/kickstart-kyle-simpson#comments</comments>
		<pubDate>Wed, 01 May 2013 23:12:54 +0000</pubDate>
		<dc:creator>David Walsh</dc:creator>
				<category><![CDATA[JavaScript]]></category>

		<guid isPermaLink="false">http://davidwalsh.name/?p=6618</guid>
		<description><![CDATA[If you didn&#8217;t already know, I&#8217;m a massive fan of Kyle Simpson, also known as @getify.  He&#8217;s one of those JavaScript legends you don&#8217;t hear nearly enough about.  He&#8217;s a pioneer of JavaScript loaders and all around JavaScript badass.  He recently did a series of guest posts for me, centered around JavaScript objects, prototypes, and inheritance: [...]<p><p>Read the full article at:  <a href="http://davidwalsh.name/kickstart-kyle-simpson">Kickstart Kyle&nbsp;Simpson</a></p>

<p><a href="http://dwf.tw/treehouse"><img src="http://davidwalsh.name/wp-content/themes/jack/images/treehouse-2.png" alt="Treehouse"></a></p>

<p><a href="http://dwf.tw/senchacon"><img src="http://davidwalsh.name/wp-content/themes/jack/images/senchaCon.png" alt="Sencha Conference"></a></p></p>
]]></description>
				<content:encoded><![CDATA[<img src="http://davidwalsh.name/demo/kyle-simpson.jpg" alt="Kyle Simpson getify" class="image" />

<p>If you didn&#8217;t already know, I&#8217;m a massive fan of <a href="http://blog.getify.com/getify-on-javascript/" rel="nofollow">Kyle Simpson</a>, also known as <a href="http://twitter.com/getify" rel="nofollow">@getify</a>.  He&#8217;s one of those JavaScript legends you don&#8217;t hear nearly enough about.  He&#8217;s a pioneer of JavaScript loaders and all around JavaScript badass.  He recently did a series of guest posts for me, centered around JavaScript objects, prototypes, and inheritance:</p>

<ul>
	<li><a href="http://davidwalsh.name/javascript-objects">Part 1: JS Objects: Inherited a Mess</a></li>
	<li><a href="http://davidwalsh.name/javascript-objects-distractions">Part 2: JS Objects: Distractions</a></li>
	<li><a href="http://davidwalsh.name/javascript-objects-deconstruction">Part 3: JS Objects: De&#8221;construct&#8221;ion</a></li>
</ul>

<p>His posts got loads of attention, feedback, and praise, and that can be nailed down to one reason:  Kyle knows his shit.  Like&#8230;he&#8217;s really, really good.  Like&#8230;if he were a doctor, he could do that can&#8217;t-breathe-poke-a-hole-in-their-neck-with-a-hollow-pen-casing thing.  Anyways, Kyle has started a <a href="http://www.kickstarter.com/projects/getify/you-dont-know-js-book-series" rel="nofollow">You Don&#8217;t Know JS</a> Kickstarter project in which he&#8217;ll create a series of awesome JavaScript books.  Let him explain:</p>

<blockquote>
<p>&#8220;You Don&#8217;t Know JS&#8221; is an exploration of the mysterious, confusing, complex, and controversial parts of JavaScript.</p>

<p>If you write JavaScript for your primary job, odds are, you&#8217;re pretty good at it. But honestly, how well do you really know the language? Most of us, myself included, spend years writing JS and never really going beyond a surface understanding. And then we blame our WTF moments on &#8220;the bad parts&#8221;.</p>

<p>This book series will examine the things that trip up or confound even the most seasoned of JS devs. And I was one of them until (recently) I spent enough time poking at the tough parts to understand them. Now I want to help others see the light, too.</p>

<p>What if you could really deeply know how JS works? Would that change how you view the language? I think so. Really, I do. A big part of why I love JS is that I finally &#8220;get it&#8221;.</p>
</blockquote>

<p>If you liked his awesome JS Objects posts, consider <a href="http://www.kickstarter.com/projects/getify/you-dont-know-js-book-series" rel="nofollow">backing his Kickstarter</a>.  You&#8217;re guaranteed to learn a lot and every bit helps toward making his books a reality!</p><p><p>Read the full article at:  <a href="http://davidwalsh.name/kickstart-kyle-simpson">Kickstart Kyle&nbsp;Simpson</a></p>

<p><a href="http://dwf.tw/treehouse"><img src="http://davidwalsh.name/wp-content/themes/jack/images/treehouse-2.png" alt="Treehouse"></a></p>

<p><a href="http://dwf.tw/senchacon"><img src="http://davidwalsh.name/wp-content/themes/jack/images/senchaCon.png" alt="Sencha Conference"></a></p></p>
]]></content:encoded>
			<wfw:commentRss>http://davidwalsh.name/kickstart-kyle-simpson/feed</wfw:commentRss>
		<slash:comments>2</slash:comments>
		</item>
		<item>
		<title>Unicode JavaScript Variables and&#160;Functions</title>
		<link>http://davidwalsh.name/unicode-javascript</link>
		<comments>http://davidwalsh.name/unicode-javascript#comments</comments>
		<pubDate>Wed, 01 May 2013 16:36:31 +0000</pubDate>
		<dc:creator>David Walsh</dc:creator>
				<category><![CDATA[JavaScript]]></category>

		<guid isPermaLink="false">http://davidwalsh.name/?p=6543</guid>
		<description><![CDATA[We all get a kick out of unicode symbols, as we all saw in my Unicode CSS Classes post, and just as we can use them for CSS classes, we can use them for JavaScript variables and functions as well!  Let&#8217;s say you want to shave a few bytes of post-minified JavaScript byes by setting [...]<p><p>Read the full article at:  <a href="http://davidwalsh.name/unicode-javascript">Unicode JavaScript Variables and&nbsp;Functions</a></p>

<p><a href="http://dwf.tw/treehouse"><img src="http://davidwalsh.name/wp-content/themes/jack/images/treehouse-2.png" alt="Treehouse"></a></p>

<p><a href="http://dwf.tw/senchacon"><img src="http://davidwalsh.name/wp-content/themes/jack/images/senchaCon.png" alt="Sencha Conference"></a></p></p>
]]></description>
				<content:encoded><![CDATA[<p>We all get a kick out of <a href="http://en.wikipedia.org/wiki/List_of_Unicode_characters">unicode symbols</a>, as we all saw in my <a href="http://davidwalsh.name/unicode-css-classes">Unicode CSS Classes</a> post, and just as we can use them for CSS classes, we can use them for JavaScript variables and functions as well!  Let&#8217;s say you want to shave a few bytes of post-minified JavaScript byes by setting false equal to a variable.  Why not do so by using a unicode variable?</p>

<pre class="js">
var ಠ_ಠ = false;

if(someVar === ಠ_ಠ) { // If someVar is false...
	// ...
}
</pre>

<p>Hilarious.  The evil eyes signal a negative result.  What about saving a few byes on true?  The Beatles once said that <a href="http://www.youtube.com/watch?v=CLEtGRUrtJo" rel="nofollow">&#8220;All You Need is Love&#8221;</a>, so let&#8217;s use a heart:</p>

<pre class="js">
var ❤ = true;

if(someVar === ❤) {
	// ...
}
</pre>

<p>You can do the same with function names.  Functions can be named after unicode symbols:</p>

<pre class="js">
function ಠ(arg) {
	// ...
}
</pre>

<p>Of course all of this is a bit &#8230; insane, and you should never use unicode symbols, but nonetheless it&#8217;s possible.  Imagine starting a new job and seeing those symbols as meaningful variables &#8212; you&#8217;d quit immediately!  Hilarious!</p><p><p>Read the full article at:  <a href="http://davidwalsh.name/unicode-javascript">Unicode JavaScript Variables and&nbsp;Functions</a></p>

<p><a href="http://dwf.tw/treehouse"><img src="http://davidwalsh.name/wp-content/themes/jack/images/treehouse-2.png" alt="Treehouse"></a></p>

<p><a href="http://dwf.tw/senchacon"><img src="http://davidwalsh.name/wp-content/themes/jack/images/senchaCon.png" alt="Sencha Conference"></a></p></p>
]]></content:encoded>
			<wfw:commentRss>http://davidwalsh.name/unicode-javascript/feed</wfw:commentRss>
		<slash:comments>3</slash:comments>
		</item>
		<item>
		<title>Turn Your Web Application into a Firefox OS App in 5&#160;Minutes</title>
		<link>http://davidwalsh.name/firefox-app</link>
		<comments>http://davidwalsh.name/firefox-app#comments</comments>
		<pubDate>Mon, 29 Apr 2013 12:34:57 +0000</pubDate>
		<dc:creator>David Walsh</dc:creator>
				<category><![CDATA[Firefox OS]]></category>
		<category><![CDATA[Mobile]]></category>

		<guid isPermaLink="false">http://davidwalsh.name/?p=6396</guid>
		<description><![CDATA[One of the promises that comes with HTML5 mobile apps, especially those to be featured on Firefox OS, is that your existing web applications, if created properly (feature detection, responsive design, etc.), can be turned into a working mobile application in relatively little time.  And that&#8217;s the way it should be, right?  If my app [...]<p><p>Read the full article at:  <a href="http://davidwalsh.name/firefox-app">Turn Your Web Application into a Firefox OS App in 5&nbsp;Minutes</a></p>

<p><a href="http://dwf.tw/treehouse"><img src="http://davidwalsh.name/wp-content/themes/jack/images/treehouse-2.png" alt="Treehouse"></a></p>

<p><a href="http://dwf.tw/senchacon"><img src="http://davidwalsh.name/wp-content/themes/jack/images/senchaCon.png" alt="Sencha Conference"></a></p></p>
]]></description>
				<content:encoded><![CDATA[<p>One of the promises that comes with HTML5 mobile apps, especially those to be featured on Firefox OS, is that your existing web applications, if created properly (feature detection, responsive design, etc.), can be turned into a working mobile application in relatively little time.  And that&#8217;s the way it should be, right?  If my app works in the device&#8217;s browser, shouldn&#8217;t it work perfectly as a standalone app?  Let&#8217;s say you have a responsive, well-written website/app and you want to let Firefox OS users experience your work as a first class app;  the following few steps will help you make those Firefox OS app dreams a reality.</p>

<h2>Step 1:  App&nbsp;Manifest</h2>

<p>The <code>manifest.webapp</code> file is arguably the only real step you need to turn your website into a Firefox OS app.  This manifest should be placed at domain root level:</p>

<pre class="js">

{
	"version": "0.1",
	"name": "Your App",
	"description": "Your new awesome Open Web App",
	"launch_path": "/index.html",
	"appcache_path": "/offline.appcache",
	"icons": {
		"16": "/img/icons/appicon-16.png",
		"48": "/img/icons/appicon-48.png",
		"128": "/img/icons/appicon-128.png"
	},
	"developer": {
		"name": "Your Name",
		"url": "http://yourawesomeapp.com"
	},
	"installs_allowed_from": ["*"],
	"locales": {
		"es": {
			"description": "Su nueva aplicación impresionante Open Web",
			"developer": {
				"url": "http://yourawesomeapp.com"
			}
		},
		"it": {
			"description": "Il vostro nuovo fantastico Open Web App",
			"developer": {
				"url": "http://yourawesomeapp.com"
			}
		}
	},
	"default_locale": "en",
	"permissions": {
		"systemXHR": {}
	}
}

</pre>

<p>And make sure to pronounce it properly within your <code>.htaccess</code> file:</p>

<pre class="htaccess">
AddType application/x-web-app-manifest+json .manifest
</pre>

<p>This manifest serves as the real app glue &#8212; add this manifest and your app is mostly complete.</p>

<h2>Step 2:  Offline&nbsp;Cache</h2>

<p>The ignorant press has sometimes laughed at Firefox OS because they believe Firefox OS apps need a connection to properly function &#8212; not so.  The (admittedly ugly) <a href="https://developer.mozilla.org/en-US/docs/HTML/Using_the_application_cache" rel="nofollow">offline cache API</a> works beautifully on Firefox OS.  Add the following to your HTML:</p>

<pre class="html">
&lt;html manifest="offline.appcache"&gt;
</pre>

<p>With the pointer in place, you can create your offline.appcache with the list of files to download:</p>

<pre class="text">
CACHE MANIFEST
# v0.1
/index.html
/css/app.css
/css/install-button.css
/img/offline.png
/img/online.png
/img/glyphicons-halflings-white.png
/img/glyphicons-halflings.png
/img/gradient.png
/js/app.js
/js/init.js
/js/install-button.js
/js/lib/install.js
/js/lib/require.js
/js/lib/zepto.js
</pre>

<p>Allowing app files to download to the device allow portions of the app to work even when the device is not connected to the internet.</p>

<h2>Step 3:  Submit to&nbsp;Marketplace</h2>

<p>As Paris Hilton can attest to, <del>over</del>exposure is 90% of success.  Submitting an app to the <a href="https://marketplace.firefox.com/" rel="nofollow">Firefox Marketplace</a> showcases the app to millions of users around the world!</p>

<p>Stop the clock &#8212; that&#8217;s all you need to do to make your web application a Firefox OS mobile app.  There&#8217;s a Twitter account, &#8220;Always Bet on JS&#8221;, that makes me think &#8220;Always Bet on the Web.&#8221;  During every talk I give, I ask attendees &#8220;If it works in the device&#8217;s browser, why shouldn&#8217;t it be a first class app?&#8221;  No one is able to tell me otherwise&#8230;and they&#8217;re right.  We&#8217;ve built careers and billion dollar websites for thing long, and they work so incredibly well, why should that change now?  Mobile is a place to extend the traditional model  and Firefox OS makes that easier than ever before!</p><p><p>Read the full article at:  <a href="http://davidwalsh.name/firefox-app">Turn Your Web Application into a Firefox OS App in 5&nbsp;Minutes</a></p>

<p><a href="http://dwf.tw/treehouse"><img src="http://davidwalsh.name/wp-content/themes/jack/images/treehouse-2.png" alt="Treehouse"></a></p>

<p><a href="http://dwf.tw/senchacon"><img src="http://davidwalsh.name/wp-content/themes/jack/images/senchaCon.png" alt="Sencha Conference"></a></p></p>
]]></content:encoded>
			<wfw:commentRss>http://davidwalsh.name/firefox-app/feed</wfw:commentRss>
		<slash:comments>8</slash:comments>
		</item>
		<item>
		<title>CSS Background&#160;Animations</title>
		<link>http://davidwalsh.name/background-animation-css</link>
		<comments>http://davidwalsh.name/background-animation-css#comments</comments>
		<pubDate>Thu, 25 Apr 2013 21:17:47 +0000</pubDate>
		<dc:creator>David Walsh</dc:creator>
				<category><![CDATA[CSS]]></category>
		<category><![CDATA[CSS Animations]]></category>

		<guid isPermaLink="false">http://davidwalsh.name/?p=6449</guid>
		<description><![CDATA[Background animations are an awesome touch when used correctly.  In the past, I used MooTools to animate a background position.  Luckily these days CSS animations are widely supported enough to rely on them to take over JavaScript-based animation tasks.  The following simple CSS snippet animates the background image (via background position) of a given element. [...]<p><p>Read the full article at:  <a href="http://davidwalsh.name/background-animation-css">CSS Background&nbsp;Animations</a></p>

<p><a href="http://dwf.tw/treehouse"><img src="http://davidwalsh.name/wp-content/themes/jack/images/treehouse-2.png" alt="Treehouse"></a></p>

<p><a href="http://dwf.tw/senchacon"><img src="http://davidwalsh.name/wp-content/themes/jack/images/senchaCon.png" alt="Sencha Conference"></a></p></p>
]]></description>
				<content:encoded><![CDATA[<p>Background animations are an awesome touch when used correctly.  In the past, I used <a href="http://davidwalsh.name/mootools-animate-background">MooTools to animate a background position</a>.  Luckily these days CSS animations are widely supported enough to rely on them to take over JavaScript-based animation tasks.  The following simple CSS snippet animates the background image (via background position) of a given element.</p>

<div class="actions"><a href="http://davidwalsh.name/demo/background-animation-css.php" class="demo">View Demo</a><div class="clear"></div></div>

<h2>The&nbsp;CSS</h2>

<p>We&#8217;ll use CSS animations instead of transitions for this effect:</p>

<pre class="css">
@keyframes animatedBackground {
	from { background-position: 0 0; }
	to { background-position: 100% 0; }
}
</pre>

<p>With the animation code in place, now it&#8217;s time to apply it to an element with a background image:</p>

<pre class="css">
#animate-area	{ 
	width: 560px; 
	height: 400px; 
	background-image: url(bg-clouds.png);
	background-position: 0px 0px;
	background-repeat: repeat-x;

	animation: animatedBackground 40s linear infinite;
}
</pre>

<p>The cloud background image within the sample element will elegantly scroll from left to right over a duration of 40 seconds, seamlessly repeating an infinite number of times.</p>

<div class="actions"><a href="http://davidwalsh.name/demo/background-animation-css.php" class="demo">View Demo</a><div class="clear"></div></div>

<p>How epic is it that we don&#8217;t need to use JavaScript to manage these animations anymore?  Of course the mess of vendor prefixes to accomplish the animation sucks, but at least the animations are much more efficient and more easily configurable!</p><p><p>Read the full article at:  <a href="http://davidwalsh.name/background-animation-css">CSS Background&nbsp;Animations</a></p>

<p><a href="http://dwf.tw/treehouse"><img src="http://davidwalsh.name/wp-content/themes/jack/images/treehouse-2.png" alt="Treehouse"></a></p>

<p><a href="http://dwf.tw/senchacon"><img src="http://davidwalsh.name/wp-content/themes/jack/images/senchaCon.png" alt="Sencha Conference"></a></p></p>
]]></content:encoded>
			<wfw:commentRss>http://davidwalsh.name/background-animation-css/feed</wfw:commentRss>
		<slash:comments>11</slash:comments>
		</item>
		<item>
		<title>Unicode CSS&#160;Classes</title>
		<link>http://davidwalsh.name/unicode-css-classes</link>
		<comments>http://davidwalsh.name/unicode-css-classes#comments</comments>
		<pubDate>Wed, 24 Apr 2013 16:18:20 +0000</pubDate>
		<dc:creator>David Walsh</dc:creator>
				<category><![CDATA[CSS]]></category>
		<category><![CDATA[Markup]]></category>

		<guid isPermaLink="false">http://davidwalsh.name/?p=6423</guid>
		<description><![CDATA[CSS class name structure and consistency is really important; some developers camelcase classnames, others use dashes, and others use underscores.  One thing I&#8217;ve learned when toying around by HTML and CSS class names is that you can actually use unicode symbols and icons as classnames.  Check this out! View Demo The HTML and&#160;CSS There&#8217;s only [...]<p><p>Read the full article at:  <a href="http://davidwalsh.name/unicode-css-classes">Unicode CSS&nbsp;Classes</a></p>

<p><a href="http://dwf.tw/treehouse"><img src="http://davidwalsh.name/wp-content/themes/jack/images/treehouse-2.png" alt="Treehouse"></a></p>

<p><a href="http://dwf.tw/senchacon"><img src="http://davidwalsh.name/wp-content/themes/jack/images/senchaCon.png" alt="Sencha Conference"></a></p></p>
]]></description>
				<content:encoded><![CDATA[<p>CSS class name structure and consistency is really important; some developers camelcase classnames, others use dashes, and others use underscores.  One thing I&#8217;ve learned when toying around by HTML and CSS class names is that you can actually use <a href="http://en.wikipedia.org/wiki/List_of_Unicode_characters">unicode symbols and icons</a> as classnames.  Check this out!</p>

<div class="actions"><a href="http://davidwalsh.name/demo/unicode-css-classes.php" class="demo">View Demo</a><div class="clear"></div></div>

<h2>The HTML and&nbsp;CSS</h2>

<p>There&#8217;s only one way to add a classname with HTML so you&#8217;ll do it that way, of course:</p>

<pre class="html">
&lt;-- place this within the document head --&gt;
&lt;meta charset="UTF-8" /&gt;

&lt;-- error message --&gt;
&lt;div class="ಠ_ಠ"&gt;You do not have access to this page.&lt;/div&gt;

&lt;-- success message --&gt;
&lt;div class="❤"&gt;Your changes have been saved successfully!&lt;/div&gt;
</pre>

<p>&#8230;and there&#8217;s only one way to declare styles for a given class:</p>

<pre class="css">
.ಠ_ಠ {
	border: 1px solid #f00;
	background: pink;
}

.❤ {
	background: lightgreen;
	border: 1px solid green;
}
</pre>

<div class="actions"><a href="http://davidwalsh.name/demo/unicode-css-classes.php" class="demo">View Demo</a><div class="clear"></div></div>

<p>Wild that you can use unicode classnames for elements, right?  Of course I don&#8217;t recommend doing so, but you can if you&#8217;d like to!</p><p><p>Read the full article at:  <a href="http://davidwalsh.name/unicode-css-classes">Unicode CSS&nbsp;Classes</a></p>

<p><a href="http://dwf.tw/treehouse"><img src="http://davidwalsh.name/wp-content/themes/jack/images/treehouse-2.png" alt="Treehouse"></a></p>

<p><a href="http://dwf.tw/senchacon"><img src="http://davidwalsh.name/wp-content/themes/jack/images/senchaCon.png" alt="Sencha Conference"></a></p></p>
]]></content:encoded>
			<wfw:commentRss>http://davidwalsh.name/unicode-css-classes/feed</wfw:commentRss>
		<slash:comments>5</slash:comments>
		</item>
		<item>
		<title>6 Reasons to Go&#160;Treehouse</title>
		<link>http://davidwalsh.name/treehouse-coupon-code</link>
		<comments>http://davidwalsh.name/treehouse-coupon-code#comments</comments>
		<pubDate>Wed, 24 Apr 2013 15:58:41 +0000</pubDate>
		<dc:creator>David Walsh</dc:creator>
				<category><![CDATA[Blog]]></category>

		<guid isPermaLink="false">http://davidwalsh.name/?p=6484</guid>
		<description><![CDATA[There are quite a few tech video tutorial websites on the internet but Treehouse is the web&#8217;s best.  Of course it&#8217;s easy to make that claim but here are some of the numerous reasons why that can only be true. Start Learning at Treehouse Trying Treehouse is&#160;FREE Treehouse&#8217;s sponsorship of this site provides developers a [...]<p><p>Read the full article at:  <a href="http://davidwalsh.name/treehouse-coupon-code">6 Reasons to Go&nbsp;Treehouse</a></p>

<p><a href="http://dwf.tw/treehouse"><img src="http://davidwalsh.name/wp-content/themes/jack/images/treehouse-2.png" alt="Treehouse"></a></p>

<p><a href="http://dwf.tw/senchacon"><img src="http://davidwalsh.name/wp-content/themes/jack/images/senchaCon.png" alt="Sencha Conference"></a></p></p>
]]></description>
				<content:encoded><![CDATA[<a href="http://dwf.tw/treehouse" rel="nofollow"><img src="http://davidwalsh.name/wp-content/themes/jack/images/treehouseFrog.png" alt="Treehouse Free" class="image" /></a>

<p>There are quite a few tech video tutorial websites on the internet but Treehouse is the web&#8217;s best.  Of course it&#8217;s easy to make that claim but here are some of the numerous reasons why that can only be true.</p>

<div class="actions"><a href="http://dwf.tw/treehouse" class="demo">Start Learning at Treehouse</a><div class="clear"></div></div>

<h2>Trying Treehouse is&nbsp;FREE</h2>

<p>Treehouse&#8217;s sponsorship of this site provides developers a <a href="http://dwf.tw/treehouse">free month of Treehouse</a>.  A free month with access to all that Treehouse has to offer:  video tutorials, quizzes, code challenges, and more.  There&#8217;s no real reason not to try Treehouse &#8212; <a href="http://dwf.tw/treehouse">give it a shot</a>!</p>

<h2>Expansive Topic&nbsp;Library</h2>

<p>Treehouse doesn&#8217;t specialize in one or two topics;  Treehouse offers courses in HTML, CSS, jQuery, JavaScript, Ruby, Ruby on Rails, WordPress, PHP, Business, iOS, and Android.  These topics cover servers side, client side, and mobile.  In essence, Treehouse provides a complete development stack experience so the barrier of entry is as low as possible.  You don&#8217;t even need to be a Developer to sign up &#8212; start from scratch and <em>become</em> a Developer!</p>

<h2>Quality&nbsp;Picture</h2>

<p>The quality of Treehouse videos is outstanding.  No pixelated capture and no wonky feeds &#8212; Treehouse videos are shot in glorious HD.  Even code samples presented within videos are crystal clear.  Essentially there is no detail missed regardless of current video focus.  Consistency in quality is what Treehouse provides.</p>

<h2>Relatable, Expert&nbsp;Instructors</h2>

<p>Treehouse&#8217;s instructors and presenters aren&#8217;t simply actors or relics from programming past &#8212; they&#8217;re knowledgable, engaging hosts that present with the enthusiasm that every developer has.  That simple difference in host keeps you learning.  Even when I&#8217;m focused on developing, I&#8217;ll play a Treehouse video in the background to catch a few tips on whichever language I choose.</p>

<h2>Interactive Quizzes and Code&nbsp;Challenges</h2>

<p>It&#8217;s easy to read a tutorial or watch an instructional video and say &#8220;Yeah, I got it.&#8221;  But until you get put into the position to need to know it, you can&#8217;t say you know it.  Treehouse provides loads of interactive quizzes and code challenges to help you remember aspects of a given language, or simply jog your memory about important concepts.  That reinforcement will come in handy even when you already have a job and good knowledge of a topic.</p>

<h2>Job Search&nbsp;Assistance</h2>

<p>Many sites provide the basic tech training but don&#8217;t tell you where to go afterward.  Treehouse bucks that trend, providing job placement assistance after you&#8217;ve completed a given program.  You aren&#8217;t simply left looking in the rearview to see Treehouse waving &#8212; Treehouse helps you get to your desired destination.  What a comfort it is to not be seen off, but rather helped to your dream job.</p>

<div class="actions"><a href="http://dwf.tw/treehouse" class="demo">Start Learning at Treehouse</a><div class="clear"></div></div>

<p>There&#8217;s no tech video tutorial service quite like Treehouse.  Treehouse truly takes you from your first day learning a new technology to helping you land that dream job you&#8217;ve been working so hard for.  Give Treehouse a try &#8212; it&#8217;s free to start, cheaper and more convenient than college, and its expansive library will teach you just about everything you need to know.  You really can&#8217;t lose with Treehouse &#8212; go for it!</p>

<div class="disclaimer gb-intro">Treehouse is a sponsor of this blog.  I&#8217;ve written this post on my own volition and without the request of Treehouse.</div><p><p>Read the full article at:  <a href="http://davidwalsh.name/treehouse-coupon-code">6 Reasons to Go&nbsp;Treehouse</a></p>

<p><a href="http://dwf.tw/treehouse"><img src="http://davidwalsh.name/wp-content/themes/jack/images/treehouse-2.png" alt="Treehouse"></a></p>

<p><a href="http://dwf.tw/senchacon"><img src="http://davidwalsh.name/wp-content/themes/jack/images/senchaCon.png" alt="Sencha Conference"></a></p></p>
]]></content:encoded>
			<wfw:commentRss>http://davidwalsh.name/treehouse-coupon-code/feed</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>JS Objects:&#160;De&#8221;construct&#8221;ion</title>
		<link>http://davidwalsh.name/javascript-objects-deconstruction</link>
		<comments>http://davidwalsh.name/javascript-objects-deconstruction#comments</comments>
		<pubDate>Wed, 24 Apr 2013 13:45:06 +0000</pubDate>
		<dc:creator>Kyle Simpson</dc:creator>
				<category><![CDATA[Guest Blogger]]></category>
		<category><![CDATA[JavaScript]]></category>

		<guid isPermaLink="false">http://davidwalsh.name/?p=6499</guid>
		<description><![CDATA[JS Objects:&#160;TL;DR JavaScript has been plagued since the beginning with misunderstanding and awkwardness around its &#8220;prototypal inheritance&#8221; system, mostly due to the fact that &#8220;inheritance&#8221; isn&#8217;t how JS works at all, and trying to do that only leads to gotchas and confusions that we have to pave over with user-land helper libs. Instead, embracing that [...]<p><p>Read the full article at:  <a href="http://davidwalsh.name/javascript-objects-deconstruction">JS Objects:&nbsp;De&#8221;construct&#8221;ion</a></p>

<p><a href="http://dwf.tw/treehouse"><img src="http://davidwalsh.name/wp-content/themes/jack/images/treehouse-2.png" alt="Treehouse"></a></p>

<p><a href="http://dwf.tw/senchacon"><img src="http://davidwalsh.name/wp-content/themes/jack/images/senchaCon.png" alt="Sencha Conference"></a></p></p>
]]></description>
				<content:encoded><![CDATA[<div class="gb-intro">
<h3>JS Objects:&nbsp;TL;DR</h3>
<p>JavaScript has been plagued since the beginning with misunderstanding and awkwardness around its &#8220;prototypal inheritance&#8221; system, mostly due to the fact that &#8220;inheritance&#8221; isn&#8217;t how JS works at all, and trying to do that only leads to gotchas and confusions that we have to pave over with user-land helper libs. Instead, embracing that JS has &#8220;behavior delegation&#8221; (merely delegation links between objects) fits naturally with how JS syntax works, which creates more sensible code without the need of helpers.</p>
<p>When you set aside distractions like mixins, polymorphism, composition, classes, constructors, and instances, and only focus on the objects that link to each other, you gain a powerful tool in behavior delegation that is easier to write, reason about, explain, and code-maintain. Simpler is better. JS is &#8220;objects-only&#8221; (OO). Leave the classes to those other languages!</p>

<h3>Due&nbsp;Thanks</h3>
<p>I&#8217;d like to thank the following amazing devs for their generous time in feedback/tech review of this article series: David Bruant, Hugh Wood, Mark Trostler, and Mark McDonnell. I am also honored that David Walsh wanted to publish these articles on his fantastic blog.</p>

<h3>Complete&nbsp;Series</h3>
<ul>
	<li><a href="http://davidwalsh.name/javascript-objects">Part 1:  JS Objects: Inherited a Mess</a></li>
	<li><a href="http://davidwalsh.name/javascript-objects-distractions">Part 2:  JS Objects: Distractions</a></li>
	<li><a href="http://davidwalsh.name/javascript-objects-deconstruction">Part 3:  JS Objects: De&#8221;construct&#8221;ion</a></li>
</ul>
</div>

<p>In part 1 of this article series (which you should totally go read if you haven&#8217;t yet!), I revisited an idea not original to me: <a href="http://davidwalsh.name/javascript-objects">JS doesn&#8217;t have &#8220;inheritance&#8221; in the traditional sense</a>, and what it does have is more appropriately labeled &#8220;behavior delegation&#8221; &#8212; the ability of one object to delegate a method or property access which it <em>cannot</em> handle over to another object which <em>can</em> handle it.</p>

<p>Then, in part 2, I addressed <a href="http://davidwalsh.name/javascript-objects-distractions">several distractions</a> which <strong><em>I think</em></strong> obfuscate JS&#8217;s true object-oriented identity, including &#8220;custom types&#8221;, &#8220;mixins&#8221;, &#8220;polymorphism&#8221; (which we&#8217;ll come back to again later), and even the new &#8220;class syntax&#8221; coming in ES6. I suggested that to understand (and leverage) better the <code>[[Prototype]]</code>, we needed to strip away the cruft. Here, I will attempt to do that.</p>

<h2><del>Turtles</del> Objects all the&nbsp;way <del>down</del> up</h2>
<p>The key realization, the punchline to this entire article series, is that <code>[[Prototype]]</code> is really only about linking one object to another object, for the purposes of delegating, if the first object <a href="http://www.ecma-international.org/ecma-262/5.1/#sec-8.12.2">cannot handle a property or method access</a> but the second can. In other words, it&#8217;s only objects, linked to other objects. That&#8217;s really all JS has.</p>

<p>In a sense, JS is the purest essence of a &#8220;object-oriented (OO)&#8221; language, in that it really <em>is</em> all about objects. In contrast to most other languages, JS is somewhat unique that you can actually create objects directly without the notion of classes or other abstractions. That&#8217;s a powerful and brilliant feature!</p>

<blockquote><p>People often bash JavaScript, but it’s one of the few prog languages that let you directly create objects. Others: Lua, Dylan, Cecil.</p>

<p>— Axel Rauschmayer (@rauschma) <a href="https://twitter.com/rauschma/status/321712221400989696">April 9, 2013</a></p></blockquote>

<p>JavaScript legitimately <em>is</em> &#8221;object-oriented&#8221;, and perhaps we shouldn&#8217;t have used that term for the other languages which imply a lot more than <strong><em>just</em></strong> &#8221;objects&#8221;. Maybe &#8220;class-oriented&#8221; would have been more accurate, which would have freed us up to use &#8220;object-oriented&#8221; for JS. Of course, as I argued in part 1, <a href="http://davidwalsh.name/javascript-objects#spade-spade">what everybody means when they use some term, matters</a>, so it&#8217;s far too late to redefine or bend the commonly accepted &#8220;object-oriented&#8221; to my own purposes, much as I&#8217;d like to.</p>

<p>I&#8217;m mildly tempted, however, to just hijack the abbreviation of &#8220;OO&#8221; to mean &#8220;objects-only&#8221; instead of &#8220;object-oriented&#8221;, but I bet that probably wouldn&#8217;t get anywhere, either. So, for our purposes here, let&#8217;s just say<strong>JavaScript is &#8220;object-based (OB)&#8221;</strong> to clarify against &#8220;object-oriented (OO)&#8221;.</p>

<p>Whatever we call it, we normally tap into this object mechanism by following the &#8220;OO way&#8221;: we create a function which we use as a &#8220;constructor&#8221;, and we call that function with <code>new</code> so that we can &#8220;instantiate&#8221; our &#8220;class&#8221;, which we specify with the constructor function together with its subsequent <code>.prototype</code> additions&#8230; but all that is like a magician&#8217;s sleight of hand that dazzles you <em>over here</em> to distract you from what&#8217;s really going on <em>over there</em>.</p>

<p>What really matters, at the end of the trick, is that <strong>two objects end up linked to each other via the<code>[[Prototype]]</code> chain</strong>.</p>

<h2>Codez&nbsp;Plz</h2>
<p>
Before we can derive and understand that simpler view of &#8220;objects-only&#8221; or &#8220;object-based&#8221;, we need to understand what actually gets created and linked when we build up some &#8220;inherited&#8221; objects in JavaScript. Not only are we going to see what happens by default, but what <em>doesn&#8217;t</em> happen.</p>

<p>Take this code for our main example:</p>

<pre class="js">function Foo(who) {
    this.me = who;
}

Foo.prototype.identify = function() {
    return "I am " + this.me;
};

function Bar(who) {
    Foo.call(this,who);
}

Bar.prototype = Object.create(Foo.prototype);
// NOTE: .constructor is borked here, need to fix

Bar.prototype.speak = function() {
    alert("Hello, " + this.identify() + ".");
};

var b1 = new Bar("b1");
var b2 = new Bar("b2");

b1.speak(); // alerts: "Hello, I am b1."
b2.speak(); // alerts: "Hello, I am b2."</pre>


<p><strong>Note:</strong> Some people write <code>Bar.prototype = Object.create(Foo.prototype);</code> as <code>Bar.prototype = new Foo();</code>. Both approaches end up with the same linked objects, where <code>Bar.prototype</code> is an object linked via its<code>[[Prototype]]</code> to <code>Foo.prototype</code>. The only real difference is whether or not the <code>Foo</code> function is called during the creation of <code>Bar.prototype</code>. Depending on your circumstances and intent, you may or may not want that to happen, so let&#8217;s consider them roughly interchangable but with different purposes.</p>

<p>What we have is an object labeled <code>Foo.prototype</code> with an <code>identify()</code> method, and another object called<code>Bar.prototype</code> with a <code>speak()</code> method. <code>Bar.prototype</code> is a <em>new empty object</em> that is <code>[[Prototype]]</code>-linked to <code>Foo.prototype</code>. Then we have two objects <code>b1</code> and <code>b2</code>, who each are each respectively linked via their own <code>[[Prototype]]</code> to <code>Bar.prototype</code>. <code>b1</code> and <code>b2</code> also have an &#8220;owned property&#8221; directly on each of them called <code>me</code>, which respectively holds the values &#8220;b1&#8243; and &#8220;b2&#8243;.</p>

<p>Let&#8217;s take a visual look at the relationships implied by the above code snippet:</p>

<p><a href="http://davidwalsh.name/demo/JavaScriptObjects--Simplified.png" target="_blank"><img alt="" src="http://davidwalsh.name/demo/JavaScriptObjects--Simplified.png" /></a></p>

<p><strong>Note:</strong> All the <code>[[Prototype]]</code> links in the diagram also mention <a href="https://developer.mozilla.org/en-US/docs/JavaScript/Reference/Global_Objects/Object/proto">a &#8220;.__proto__&#8221; property</a>. <code>__proto__</code> is a formerly non-standard property (which exists in most but not all JS environments) to expose the internal <code>[[Prototype]]</code>chain. As of ES6, however, it will be standardized.</p>

<p>I left a whole bunch of detail out of that diagram, intentionally, so it was even remotely digestable. But of course, since JS is all objects, all the linkages and ancestry of each item can be fully traced. We&#8217;ll come back to all the omitted parts of this diagram in a little bit.</p>

<p>Note in this diagram that the function constructors all have a <code>.prototype</code> property pointing at an object. As we&#8217;ve been suggesting, the object is what we really care about, and in this way of viewing the JS object mechanism, the way we get that object is to look at a constructor function&#8217;s <code>.prototype</code>. The function doesn&#8217;t really serve any particularly important role.</p>

<p>I know a whole bunch of you just screamed out, &#8220;sure it does! it runs constructor code to initialize the new object!&#8221; OK, you&#8217;re technically right. <code>Foo()</code> has some code in it which is ultimately run against <code>b1</code> and <code>b2</code>.</p>

<p>But the devil&#8217;s always in the details. First, we don&#8217;t need a constructor function to run such code. That&#8217;s just one way of getting that outcome. And I&#8217;m going to suggest it&#8217;s a more distracting approach.</p>

<p>Secondly, <a href="http://max.berger.name/teaching/s06/ch13.jsp">unlike C++</a>, the base-class/superclass <code>Foo()</code> &#8221;constructor&#8221; doesn&#8217;t automatically get called when you run the child-class <code>Bar()</code> &#8221;constructor&#8221; to make <code>b1</code> and <code>b2</code>. So, like Java, we have to manually call the<code>Foo()</code> function from <code>Bar()</code>, but unlike Java, we have to do so with a variation of the <a href="http://davidwalsh.name/javascript-objects-distractions#mixins">explicit &#8220;mixin&#8221; pattern</a> (I&#8217;d probably call it &#8220;implicit mixin&#8221; here) to make it work like we expect. That&#8217;s an ugly detail that is very easy to forget or get wrong.</p>

<p>So, where you&#8217;d probably argue with me that &#8220;constructor&#8221; functions are useful being automatically called at the construction of an object, I&#8217;d point out that this is true for only the immediate level, not for the entire &#8220;chain of inheritance&#8221;, which means that automatic behavior is pretty limited/shallow in utility.</p>

<h2>Polymorphism&nbsp;redux</h2>
<p>Moreover, we see here the first hint of the <a href="http://davidwalsh.name/javascript-objects-distractions#polys">problems with relative polymorphism</a> in JS: <strong>you can&#8217;t do it!</strong> I can&#8217;t tell<code>Bar()</code> to automatically and relatively call his ancestor constructor(s), via a relative reference. I have to manually call (aka, &#8220;borrow&#8221;) the <code>Foo()</code> function (it&#8217;s not a constructor here, just a normal function call!) from inside of<code>Bar()</code>, and to make sure the <code>this</code> is bound correctly, I have to do the slightly more awkward <code>.call(this)</code>style of code. Ugh.</p>

<p>What may not be obvious until you go back and look closer at the diagram above is that the <code>Foo()</code> function is<strong>not</strong> related in any useful/practical way to the <code>Bar()</code> function. The <code>Foo()</code> function does not even appear in the &#8220;inheritance&#8221; (aka &#8220;delegation&#8221;) chain of <code>Bar.prototype</code> object. The fact that there are some lines you can follow on the graph for indirect relationships doesn&#8217;t mean that those relationships are what you&#8217;d want to rely on in your code.</p>

<p>The problem with polymorphism we&#8217;re seeing here is not only for &#8220;constructor&#8221; functions. Any function at one level of the <code>[[Prototype]]</code> chain that wants to call up to an ancestor with the same name must do so via this manual implicit mixin approach just like we did inside of <code>Bar()</code> above. <strong>We have no effective way of making relative references up the chain.</strong></p>

<p>Importantly, this means that not only do we establish the link between <code>Bar</code> and <code>Foo</code> once at &#8220;class&#8221; definition, but every single polymorphic reference also has to be hardcoded with the direct relationship. This significantly decreases the flexibility and maintainability of your code. As soon as you make a function hard-coded with implicit mixin to an &#8220;ancestor&#8221;, now your function can&#8217;t be &#8220;borrowed&#8221; as easily by other objects without those possible unintended side effects.</p>

<p>OK, so let&#8217;s say you agree with me at this point that polymoprhism in JS is more trouble than it&#8217;s worth. <strong>Using constructor-based coding to wire JS objects to each other forces you into the <em>problems</em> of polymorphism</strong>.</p>

<h2>.constructor</h2>
<p>Another detail that&#8217;s easy to miss is that an object&#8217;s <code>.constructor</code> property really doesn&#8217;t behave like we&#8217;d probably expect. It&#8217;s correct at the <code>Foo()</code> level of the graph, but below that, at <code>Bar()</code> and <code>b1</code> / <code>b2</code>, notice that the implied linkage there shows <code>.constructor</code> references, strangely, still pointing at <code>Foo</code>.</p>

<p>Actually, what this means is that the only time a <code>.constructor</code> property is added to an object is when that object is the <strong>default</strong> <code>.prototype</code> attached to a declared function, as is the case of <code>Foo()</code>. When objects are created via <code>new Fn()</code> or <code>Object.create(..)</code> calls, those objects <strong>don&#8217;t</strong> get a <code>.constructor</code> added to them.</p>

<p>Let me say that again: an object created by a constructor doesn&#8217;t actually get a <code>.constructor</code> property to point to which constructor it was created by. This is an <strong>extremely common</strong> misconception.</p>

<p>So if you reference <code>b1.constructor</code> for instance, then you&#8217;re actually going to delegate up the chain a few links, to <code>Foo.prototype</code>. Of course, <code>Foo.prototype</code> has a <code>.constructor</code> property and it&#8217;s pointing at <code>Foo</code> like you&#8217;d expect.</p>

<p>What&#8217;s that mean? In the above snippet, right after you perform <code>Bar.prototype = Object.create(Foo)</code> (or even if you&#8217;d done <code>Bar.prototype = new Foo()</code>), if you plan to rely on the <code>.constructor</code> property (which many do), you need to perform an extra step, right where I put the JS &#8220;Note:&#8221; comment:</p>

<pre class="js">//...
Bar.prototype = Object.create(Foo.prototype);
Bar.prototype.constructor = Bar; // &lt;-- add this line!
//...</pre>

<p>Then <code>b1.constructor</code> references will delegate to that <code>Bar.prototype</code> level, and will &#8220;correctly&#8221; point at <code>Bar()</code>as you&#8217;d probably have expected. Ugh&#8230;**more syntax gotchas** that user-land libs always have to &#8220;fix&#8221; for us.</p>

<p>Furthermore, the fact that <code>Foo.prototype</code> has a <code>.constructor</code> property pointing at <code>Foo</code> is strange, when you think about &#8220;constructor&#8221; the way most people do. It&#8217;s nice that it gives objects created by <code>new Foo()</code> a way to delegate to a <code>.constructor</code> property access and find <code>Foo()</code>, but it&#8217;s bizarre where <code>.constructor</code> actually lives.</p>

<p>It implies that <code>Foo()</code> constructed <code>Foo.prototype</code>, but that&#8217;s nonsense. <code>Foo()</code> had nothing to do with creating the default <code>Foo.prototype</code>. <code>Foo.prototype</code> defaults to an empty object that was <strong>actually constructed by the built-in <code>Object()</code> constructor</strong>.</p>

<p>So we have to change how we think of what the <code>.constructor</code> property means. It does <strong>not</strong> mean &#8220;the constructor this object was created by&#8221;. It <em>actually</em> means &#8220;the constructor which creates any objects that end up getting <code>[[Prototype]]</code> linked to this object.&#8221; Subtle but super important difference to get straight.</p>

<p>Point? These confusions only happen/matter if you&#8217;re using constructor-style code, so it&#8217;s <em>the choice of this style of code</em> that opts you into the problems. You don&#8217;t <strong><em>have</em></strong> to live with that pain. There&#8217;s a better, simpler way!</p>

<h2>The Whole&nbsp;Pie</h2>
<p>Now let&#8217;s look at everything that&#8217;s actually implied by the above snippet of code. Ready for the whole messy thing?</p>

<p><a href="http://davidwalsh.name/demo/JavaScriptObjects--Full.png" target="_blank"><img alt="" src="http://davidwalsh.name/demo/JavaScriptObjects--Full.png" /></a></p>

<p>Take a few minutes to just take all that in. Why show you such a complex diagram?</p>

<p>This diagram actually shows you where some of JavaScript&#8217;s functionality comes from, where before you might have just never considered how it all worked. For instance, have you wondered how all functions are able to use behavior such as <code>call()</code>, <code>apply()</code>, <code>bind()</code>, etc? You may have assumed each function has that behavior built-in, but as you can see from this diagram, functions <strong>delegate</strong> up their <code>[[Prototype]]</code> chain to handle those behaviors.</p>

<p>While the behavior delegation part is sensible and useful, consider all the <em>implied complexity</em> of constructor-style coding as visualized here. It&#8217;s pretty tough to trace all the different entities and diagrams and make much sense of it all. A lot of that complexity comes from the function constructors. (here&#8217;s the same <a href="https://dl.dropboxusercontent.com/u/2474669/jsdiagrams/JavaScript%20Objects%20--%20Full%20--%20Direct%20Only.png">complete graph but with the implied relationship lines omitted</a>, if that helps to digest)</p>

<p>If you take that diagram, and remove all the functions and any associated arrows (which we&#8217;ll see in just a moment), you&#8217;re left with &#8220;objects-only&#8221;, and you&#8217;ll have a <strong>much</strong> more simplified view of the JS objects world.</p>

<h1>Simpler: Object -&gt;&nbsp;Object</h1>

<blockquote><p>Perfection is achieved, not when there is nothing more to add, but when there is nothing left to take away. &#8211;Antoine de Saint-Exupery</p></blockquote>
<p>For refresher, the same prototype-style code from above:</p>

<pre class="js">function Foo(who) {
    this.me = who;
}

Foo.prototype.identify = function() {
    return "I am " + this.me;
};

function Bar(who) {
    Foo.call(this,who);
}

Bar.prototype = Object.create(Foo.prototype);
// NOTE: .constructor is borked here, need to fix

Bar.prototype.speak = function() {
    alert("Hello, " + this.identify() + ".");
};

var b1 = new Bar("b1");
var b2 = new Bar("b2");

b1.speak(); // alerts: "Hello, I am b1."
b2.speak(); // alerts: "Hello, I am b2."</pre>

<p>Now, let&#8217;s instead consider this alternative snippet of code, which accomplishes exactly the same, but it does so without any of the confusion/distraction of &#8220;constructor functions&#8221;, <code>new</code>, <code>.prototype</code>, etc. It just creates several objects and links them together.</p>

<pre class="js">var Foo = {
    init: function(who) {
        this.me = who;
    },
    identify: function() {
        return "I am " + this.me;
    }
};

var Bar = Object.create(Foo);

Bar.speak = function() {
    alert("Hello, " + this.identify() + ".");
};

var b1 = Object.create(Bar);
b1.init("b1");
var b2 = Object.create(Bar);
b2.init("b2");

b1.speak(); // alerts: "Hello, I am b1."
b2.speak(); // alerts: "Hello, I am b2."</pre>

<p>Let&#8217;s try to take some comparison looks between this snippet and the previous one. They both accomplish the same thing, but there&#8217;s some important differences in how we get there.</p>

<p>First of all, <code>Bar</code> and <code>Foo</code> are now <strong>just objects</strong>, they&#8217;re not functions or constructors anymore. I left them as capital letters just for the symmetry and because some people feel better with them. They make it clear that the objects being linked are what we cared about all along, so instead of the indirectness of linking <code>Bar.prototype</code> to<code>Foo.prototype</code>, we just make <code>Foo</code> and <code>Bar</code> objects themselves and link <em>them</em>. <strong>AND</strong>, we only need one line of code to link them, instead of the extra ugly polymorphic linkage. Bam!</p>

<p>Instead of calling function constructors like <code>new Bar(..)</code>, we <a href="https://developer.mozilla.org/en-US/docs/JavaScript/Reference/Global_Objects/Object/create">use <code>Object.create(..)</code></a>, which is an ES5 helper that allows us to create a new object and optionally provide another object to <code>[[Prototype]]</code> link it to. We get the same outcome (object creation and linkage) as a constructor call, but without needing the constructor. BTW, there&#8217;s a <a href="https://developer.mozilla.org/en-US/docs/JavaScript/Reference/Global_Objects/Object/create#Polyfill">simple non-ES5 polyfill</a> for <code>Object.create(..)</code>, so you can safely use this style of code in all browsers without concern.</p>

<p>Secondly, notice that because we&#8217;re not worried about constructors anymore, we have eliminated any concerns about awkward polymorphisms forcing us to do manual implied mixins to call <code>Foo()</code> from <code>Bar()</code>. Instead, we put the code we wanted to run to initialize our objects into a <code>init()</code> method, on <code>Foo</code>, and we can now call<code>b1.init(..)</code> directly via the delegation chain and it &#8220;magically&#8221; just works like we want.</p>

<p>So, we have a tradeoff here. We don&#8217;t get automatic constructor calls, which means we create the object like<code>var b1 = Object.create(Bar)</code> and then we have to additionally call <code>b1.init("b1")</code>. That&#8217;s &#8220;more code&#8221;.</p>

<p>But the benefits we get, <strong>which I think are much better and well worth it</strong>, are no awkwardness with the linkage between <code>Foo</code> and <code>Bar</code> &#8211; instead we leverage <code>[[Prototype]]</code> delegation to get at the code reuse in<code>init()</code>. Also, no more verbose/repetitive <code>.prototype</code> references, and neither do we need to use<code>.call(this)</code> nearly as often (especially if we <strong>avoid</strong> polymorphism!).</p>

<h2>Looks are&nbsp;everything</h2>
<p>And to visualize the simplicity this approach brings us, here&#8217;s the diagram when we remove the functions entirely and focus only on the objects:</p>

<p><a href="http://davidwalsh.name/demo/JavaScriptObjects--OnlyObjects.png" target="_blank"><img alt="" src="http://davidwalsh.name/demo/JavaScriptObjects--OnlyObjects.png" /></a></p>

<p>I don&#8217;t know about you, but I just think that mental model is <strong>so much cleaner</strong>, and the bonus is that its semantics perfectly match the code.</p>

<p>I have shown you simple enough code using only core JS syntax, that I don&#8217;t need any helper libraries to wire up my objects. Of course, I <em>could</em> use one, but why? Simpler is better. KISS.</p>

<blockquote><p>Any fool can make something complicated. It takes a genius to make it simple. &#8211;Woody Guthrie</p></blockquote>
<p>For the record, I&#8217;m not <em>even remotely</em> the genius here. <a href="http://twitter.com/brendaneich">Brendan Eich</a>, creator of our language, was the genius for making something so powerful yet so simple.</p>

<h2>Object&nbsp;self-reflection</h2>
<p>Last thing to address: how does this simplification affect the process of reflecting on an object? In other words, can we inspect an object and find out about its relationships to other objects?</p>

<p>For prototype-style code, reflection looks like this:</p>

<pre class="js">b1 instanceof Bar; // true
b2 instanceof Bar; // true
b1 instanceof Foo; // true
b2 instanceof Foo; // true
Bar.prototype instanceof Foo; // true
Object.getPrototypeOf(b1) === Bar.prototype; // true
Object.getPrototypeOf(b2) === Bar.prototype; // true
Object.getPrototypeOf(Bar.prototype) === Foo.prototype; // true</pre>

<p>Notice that you&#8217;re using <code>instanceof</code> and having to think in terms of the constructor functions that made your objects, and their <code>.prototype</code>s, rather than just reflecting on the objects themselves. Each of those reflections comes with slightly more mental tax as a result.</p>

<p>And when there&#8217;s only objects?</p>

<pre class="js">Bar.isPrototypeOf(b1); // true
Bar.isPrototypeOf(b2); // true
Foo.isPrototypeOf(b1); // true
Foo.isPrototypeOf(b2); // true
Foo.isPrototypeOf(Bar); // true
Object.getPrototypeOf(b1) === Bar; // true
Object.getPrototypeOf(b2) === Bar; // true
Object.getPrototypeOf(Bar) === Foo; // true</pre>

<p>By contrast, reflection on objects is only about the objects. There&#8217;s no awkward references to a constructor&#8217;s<code>.prototype</code> property for the checks. You can just inspect if one object is related via <code>[[Prototype]]</code> to another object. Same capabilities as above, but with less mental tax.</p>

<p>Moreover, as I mentioned in part 2, this sort of explicit object reflection is preferable and more robust/reliable than implicit detection through <a href="http://en.wikipedia.org/wiki/Duck_typing">duck typing</a>.</p>

<h2>Object.wrapItUpAlready()</h2>
<p>Take a deep breath! That was a lot to take in. If you&#8217;ve followed all 3 parts of the article series, I hope by now you see the bottom line: JS has objects and when we link them, we get powerful behavior delegation.</p>

<p>There&#8217;s just no need to pile on class-orientation on top of such a great system, because it ultimately just leads to the confusion and distraction that has kept JS&#8217; object mechanism shrouded and covered up by all these helper libraries and misunderstandings about JS syntax.</p>

<p>If you stop thinking about inheritance, and instead think with the arrows headed the other way: delegation, your JS code will be simpler. Remember: it&#8217;s just objects linked to objects!</p><p><p>Read the full article at:  <a href="http://davidwalsh.name/javascript-objects-deconstruction">JS Objects:&nbsp;De&#8221;construct&#8221;ion</a></p>

<p><a href="http://dwf.tw/treehouse"><img src="http://davidwalsh.name/wp-content/themes/jack/images/treehouse-2.png" alt="Treehouse"></a></p>

<p><a href="http://dwf.tw/senchacon"><img src="http://davidwalsh.name/wp-content/themes/jack/images/senchaCon.png" alt="Sencha Conference"></a></p></p>
]]></content:encoded>
			<wfw:commentRss>http://davidwalsh.name/javascript-objects-deconstruction/feed</wfw:commentRss>
		<slash:comments>32</slash:comments>
		</item>
		<item>
		<title>JS Objects:&#160;Distractions</title>
		<link>http://davidwalsh.name/javascript-objects-distractions</link>
		<comments>http://davidwalsh.name/javascript-objects-distractions#comments</comments>
		<pubDate>Tue, 23 Apr 2013 13:33:21 +0000</pubDate>
		<dc:creator>Kyle Simpson</dc:creator>
				<category><![CDATA[Guest Blogger]]></category>
		<category><![CDATA[JavaScript]]></category>

		<guid isPermaLink="false">http://davidwalsh.name/?p=6471</guid>
		<description><![CDATA[JS Objects:&#160;TL;DR JavaScript has been plagued since the beginning with misunderstanding and awkwardness around its &#8220;prototypal inheritance&#8221; system, mostly due to the fact that &#8220;inheritance&#8221; isn&#8217;t how JS works at all, and trying to do that only leads to gotchas and confusions that we have to pave over with user-land helper libs. Instead, embracing that [...]<p><p>Read the full article at:  <a href="http://davidwalsh.name/javascript-objects-distractions">JS Objects:&nbsp;Distractions</a></p>

<p><a href="http://dwf.tw/treehouse"><img src="http://davidwalsh.name/wp-content/themes/jack/images/treehouse-2.png" alt="Treehouse"></a></p>

<p><a href="http://dwf.tw/senchacon"><img src="http://davidwalsh.name/wp-content/themes/jack/images/senchaCon.png" alt="Sencha Conference"></a></p></p>
]]></description>
				<content:encoded><![CDATA[<div class="gb-intro">
<h3>JS Objects:&nbsp;TL;DR</h3>
<p>JavaScript has been plagued since the beginning with misunderstanding and awkwardness around its &#8220;prototypal inheritance&#8221; system, mostly due to the fact that &#8220;inheritance&#8221; isn&#8217;t how JS works at all, and trying to do that only leads to gotchas and confusions that we have to pave over with user-land helper libs. Instead, embracing that JS has &#8220;behavior delegation&#8221; (merely delegation links between objects) fits naturally with how JS syntax works, which creates more sensible code without the need of helpers.</p>
<p>When you set aside distractions like mixins, polymorphism, composition, classes, constructors, and instances, and only focus on the objects that link to each other, you gain a powerful tool in behavior delegation that is easier to write, reason about, explain, and code-maintain. Simpler is better. JS is &#8220;objects-only&#8221; (OO). Leave the classes to those other languages!</p>

<h3>Due&nbsp;Thanks</h3>
<p>I&#8217;d like to thank the following amazing devs for their generous time in feedback/tech review of this article series: David Bruant, Hugh Wood, Mark Trostler, and Mark McDonnell. I am also honored that David Walsh wanted to publish these articles on his fantastic blog.</p>

<h3>Complete&nbsp;Series</h3>
<ul>
	<li><a href="http://davidwalsh.name/javascript-objects">Part 1:  JS Objects: Inherited a Mess</a></li>
	<li><a href="http://davidwalsh.name/javascript-objects-distractions">Part 2:  JS Objects: Distractions</a></li>
	<li><a href="http://davidwalsh.name/javascript-objects-deconstruction">Part 3:  JS Objects: De&#8221;construct&#8221;ion</a></li>
</ul>
</div>

<p>In <a href="http://davidwalsh.name/javascript-objects">part 1</a> of this article series, I went into great detail (aka, wordiness) about the differences between what the traditional definition of &#8220;inheritance&#8221; means and how JS&#8217;s <code>[[Prototype]]</code> mechanism works. We saw that JS operates oppositely to &#8220;inheritance&#8221;, being better labeled as &#8220;behavior delegation&#8221;. If you haven&#8217;t read it and you have any twinges of doubt or confusion about that statement, I&#8217;d encourage you to go read part 1 first.</p>

<p>Inheritance implies, to an extent, copying of behavioral definition down the chain, whereas behavior delegation implies delegating behavior up the chain. These aren&#8217;t just word semantics, but an important distinction that, when examined, can de-mystify a lot of confusing stuff around JS objects.</p>

<p>I&#8217;m by far not the first dev to realize this truth about JS. What differs here is in my reaction to that realization. The response usually is layering on other concepts to smoothe out the rough edges or unexpected consequences of how &#8220;prototypal inheritance&#8221; can surprise us, to try to make JS <em>feel</em> more like classical OO.</p>

<p><strong>I think those attempts just distract us from the plain truth of how JS works.</strong></p>

<p>I would rather identify the things which are merely distractions, and set them aside, and embrace only the true essence of how JS&#8217;s <code>[[Prototype]]</code> works. Rather than trying to make JS more &#8220;inheritance friendly&#8221;, I&#8217;d rather rip out everything that confuses me (and others) into thinking JS has &#8220;inheritance&#8221; at all.</p>

<h2>Types</h2>
<p>It&#8217;s often cited that in JavaScript, if you declare a function and add things to that function&#8217;s prototype, then that alone makes a definition of a custom &#8220;type&#8221;, which can be <em>instantiated</em>. If we were in a traditional OO language, that sort of thinking might be more appropriate, but here in JS land, it&#8217;s just one of many distractions.</p>

<p>You&#8217;re not really creating a new type in any real sense of that word. It&#8217;s not a new type that will be revealed by the<code>typeof</code> operator, and it&#8217;s not going to affect the internal <code>[[Class]]</code> characteristic of a value (what would be reported by default via <code>Object#toString()</code>). It is true that you can do some self-reflection to check if an object is an &#8220;instance of&#8221; some function&#8217;s construction (via the <a href="https://developer.mozilla.org/en-US/docs/JavaScript/Reference/Operators/instanceof"><code>instanceof</code> operator</a>). But importantly,<code>foo1 instanceof Foo</code> is just following the internal <code>[[Prototype]]</code> chain of your object <code>foo1</code> to see if at any level of that chain it happens to find the <code>.prototype</code> object attached to the <code>Foo</code> function.</p>

<p>In other words, the reflection you&#8217;re doing is not about checking if the value is a specified type at all, nor is it about the function constructor. It&#8217;s <strong>only</strong> about asking if one object is in another object&#8217;s <code>[[Prototype]]</code> chain. The name and <a href="http://www.ecma-international.org/ecma-262/5.1/#sec-11.8.6">semantics of the <code>instanceof</code> operator</a> (referring to &#8220;instances&#8221; and &#8220;constructor functions&#8221;) are layering on extra and unnecessary meaning, which only confuses you into thinking there&#8217;s anything more than simple <code>[[Prototype]]</code> chain checking going on.</p>

<p>Some developers frown on the usage of <code>instanceof</code>, and so an alternate form of determining the &#8220;type&#8221; of some object is called <a href="http://en.wikipedia.org/wiki/Duck_typing">duck typing</a>, which is basically inferring a value&#8217;s &#8220;type&#8221; by inspecting the object for one or more charateristic features, like a specific method or property.</p>

<p>Either way, these aren&#8217;t really &#8220;types&#8221;, they&#8217;re just approximations of types, which is one part of what makes JS&#8217;s object mechanism more complicated than other languages.</p>

<a name="mixins" id="mixins"></a>
<h2>Mixins</h2>
<p>Another distraction is trying to mimic the automatic &#8220;copying&#8221; of <a href="http://www.joezimjs.com/javascript/javascript-mixins-functional-inheritance/">inheritance</a> by <a href="http://javascriptweblog.wordpress.com/2011/05/31/a-fresh-look-at-javascript-mixins/">using the &#8220;mixin&#8221;</a> pattern, which essentially manually iterates through all the methods/properties on an object and makes a &#8220;copy&#8221; (techically just a reference for functions and objects) onto the target object.</p>

<p>I&#8217;m not saying that mixins are bad &#8212; they&#8217;re a very useful pattern. However, <strong>mixins have nothing to do with the<code>[[Prototype]]</code> chain</strong> or inheritance or delegation or any of that &#8212; they rely entirely on implicit assignment of<code>this</code> by having an &#8220;owning object&#8221; at the call-time of a function/method. They are, in fact, completely circumventing the <code>[[Prototype]]</code> chain.</p>

<p>Take any two independent objects, call them <code>A</code> and <code>B</code> (they don&#8217;t have to be linked via <code>[[Prototype]]</code> at all), and you can still mixin <code>A</code>&#8216;s stuff into <code>B</code>. If that style of code works for your situation, use it! But just note that it has nothing to do with <code>[[Prototype]]</code> or inheritance. Trying to think of them as related is <strong>just a distraction</strong>.</p>

<p>Another related distraction is when the inevitable desire to create &#8220;multiple inheritance&#8221; comes up, because JavaScript only allows an object to be <code>[[Prototype]]</code> linked to <strong>one</strong> other object at a time. When we read about the <a href="https://developer.mozilla.org/en-US/docs/JavaScript/Guide/Details_of_the_Object_Model#No_multiple_inheritance">lack of multiple inheritance in JavaScript</a>, several problems come up, and various &#8220;solutions&#8221; are often proposed, but we never actually solve them, we just do more fancy hand-waiving to distract us from the difficulties that JS poses at the syntax/semantic level.</p>

<p>For example, you basically end up doing some form of &#8220;mixin&#8221; to get multiple different sets of properties/methods added into your object, but these techniques don&#8217;t, without <a href="https://shanetomlinson.com/2012/chains-use-composition-to-fake-multiple-inheritance-in-javascript/">elaborate and inefficient work-arounds</a>, gracefully handle collision if two of your &#8220;ancestor&#8221; objects have the same property or method name. Only one version of the property/method is going to end up on your object, and that&#8217;s usually going to be the last one you mixed-in. There&#8217;s not really a clean way to have your object reference the different versions simultaneously.</p>

<p>Some people choose another distraction to resolve these problems, by using the &#8220;composition&#8221; pattern. Basically, instead of wiring your object <code>C</code> to both <code>A</code> and <code>B</code>, you just maintain a separate instance of each of <code>A</code> and<code>B</code> inside your <code>C</code> object&#8217;s properties/members. Again, this is not a bad pattern, it has plenty of goodness to it.</p>

<p><a href="http://www.crockford.com/javascript/inheritance.html">Parasitic Inheritance</a> is another example of a pattern that works around this &#8220;problem&#8221; that <code>[[Prototype]]</code> doesn&#8217;t work like classes by simply avoiding <code>[[Prototype]]</code> altogether. It&#8217;s a fine pattern, but I think it&#8217;s a confusing distraction because it makes you <strong>feel</strong> like you&#8217;re doing OO when you&#8217;re not.</p>


<p>Whatever technique you use here, you basically end up ignoring the <code>[[Prototype]]</code> chain, and doing things manually, which means you&#8217;ve moved away from JavaScript&#8217;s &#8220;prototypal inheritance&#8221; mechanism altogether.

<a name="polys" id="polys"></a>
<h2>Polymorphism</h2>
<p>One particular distraction that ends up creating some of the most awkward code patterns we deal with in JS is polymorphism, which is the practice of having the same method or property name at different levels of your &#8220;inheritance chain&#8221;, and then using <code>super</code>-style relative references to access ancestor versions of the same.</p>

<p>The problem is mechanical: JavaScript provides a <code>this</code> property, but importantly it is always rooted at the bottom of the <code>[[Prototype]]</code> chain, not whatever level of the chain the current function was found at. While it&#8217;s true that <code>this.foobar()</code> might end up resolving (finding) <code>foobar()</code> at an ancestor level of the chain, inside that call, his <code>this</code> will still be the original rooted <code>this</code> object.</p>

<p>Put more simply, <code>this</code> is not relative, but absolute to the beginning of the call stack. If JS had a <code>super</code> or a<code>currentThis</code> (as <a href="https://gist.github.com/getify/5253319">I proposed recently</a>), then those references would be relative to whatever the currently resolved link in the <code>[[Prototype]]</code> chain was, which would allow you to make a relative reference to a link &#8220;above&#8221;. But, JS does not currently have any such mechanism. And <code>this</code> being absolute rooted makes it an ineffective (or <a href="https://gist.github.com/getify/5254459">inefficient at best</a>, thus impractical) solution to these relative references that polymorphism requires.</p>

<p>Most of the OO helper libraries try to provide a way for you to make <code>super</code> calls, but all of them end up having to do inefficient things under the covers to make that kind of relative call work.</p>

<h2>class { ..&nbsp;}</h2>
<p>Lastly, I think the <a href="http://infrequently.org/2012/04/class-warfare/">long and hotly debated</a> topic of <a href="http://wiki.ecmascript.org/doku.php?id=strawman:maximally_minimal_classes"><code>class { .. }</code> syntax</a> that is coming to the language in ES6 represents more of the <em>attempt to cover up what JS actually does with what people wished JS did</em>. These sorts of distractions are not likely to make understanding JS&#8217;s actual mechanisms better. Some <a href="http://www.nczonline.net/blog/2012/10/16/does-javascript-need-classes/">speculate that it will make JS more approachable from traditional OO devotees</a>, which may be true at first, but I suspect the ultimate result is that they&#8217;ll quickly become frustrated about how it doesn&#8217;t work as they&#8217;d expect.</p>

<p>What&#8217;s important to understand is that the new class syntax we&#8217;re getting is not introducing radically new behavior or a more classical version of inheritance. It&#8217;s wrapping up how JS <code>[[Prototype]]</code> delegation currently works, in a <strong>syntax and semantics which come pre-loaded with lots of <del>baggage</del> understanding and expectation, which run quite contradictory to what you&#8217;ll really get with JS classes</strong>. If you currently do not understand, or don&#8217;t like, JS object &#8220;inheritance&#8221;, the <code>class {..}</code> syntax is pretty unlikely to satisfy you.</p>

<p>Yes, the syntax takes away some of the boilerplate of explicitly adding items to a &#8220;constructor&#8221; function&#8217;s<code>.prototype</code> object, and goodness knows we all will love not having to type the <code>function</code> keyword as many times. Hooray! If you already fully understand the awkward parts of JS &#8220;classes&#8221;, and you can&#8217;t wait for<code>class {..}</code> to sugar up the syntax, I&#8217;m sure you&#8217;re happy, but I also think you&#8217;re probably in the minority. It&#8217;s made far too many compromises to even make it into the language to fully please a broad range of totally opposite opinions.</p>

<p>The underlying <code>[[Prototype]]</code> system isn&#8217;t changing, and almost none of the difficulties we just outlined are getting measurably any better. The only exception is the addition of the <code>super</code> keyword. That will be a welcome change I suppose.</p>

<p>Although, as a side note, the engine won&#8217;t actually bind <code>super</code> dynamically (at call time) to the appropriate link in the <code>[[Prototype]]</code> chain, but will instead bind it statically (at definition time) based on the owning object of a function reference. This is going to possibly create some weird WTF&#8217;s because the engine is going to have to create new function references on the fly as functions that use <code>super</code> are assigned around to different owning objects. It&#8217;s possible (unconfirmed suspicion) that it may not work in all cases as you&#8217;d expect if <code>super</code> were instead bound dynamically.</p>

<h2>Simplificationizing™</h2>
<p>We&#8217;ve just examined a variety of ways that many JS devs try to layer on extra abstractions and concepts on top of JS&#8217;s core object mechanism. I argue that this is a mistake that takes us further from the beauty of core JavaScript. Rather than adding complexity to smoothe out the rough edges, I think we need to strip things out to get to the good stuff.</p>

<p>In <a href="http://davidwalsh.name/javascript-objects-deconstruction">part 3</a>, I will address exactly that, taking JS from the more complex world of classes and inheritance back to the simpler world of objects and delegation links.</p><p><p>Read the full article at:  <a href="http://davidwalsh.name/javascript-objects-distractions">JS Objects:&nbsp;Distractions</a></p>

<p><a href="http://dwf.tw/treehouse"><img src="http://davidwalsh.name/wp-content/themes/jack/images/treehouse-2.png" alt="Treehouse"></a></p>

<p><a href="http://dwf.tw/senchacon"><img src="http://davidwalsh.name/wp-content/themes/jack/images/senchaCon.png" alt="Sencha Conference"></a></p></p>
]]></content:encoded>
			<wfw:commentRss>http://davidwalsh.name/javascript-objects-distractions/feed</wfw:commentRss>
		<slash:comments>10</slash:comments>
		</item>
		<item>
		<title>One Year At&#160;Mozilla</title>
		<link>http://davidwalsh.name/year-mozilla</link>
		<comments>http://davidwalsh.name/year-mozilla#comments</comments>
		<pubDate>Tue, 23 Apr 2013 11:16:27 +0000</pubDate>
		<dc:creator>David Walsh</dc:creator>
				<category><![CDATA[Blog]]></category>
		<category><![CDATA[Browsers]]></category>

		<guid isPermaLink="false">http://davidwalsh.name/?p=6417</guid>
		<description><![CDATA[When I was a naive but bright-eyed kid attending a small technical college in 2002, I was first introduced to Mozilla.  The Mozilla browser looked incredibly similar to Netscape but provided a few additional development tools (like syntax-highlighted source windows)!  Developing within Mozilla&#8217;s browser really sparked my love of web development.  Then Mozilla&#8217;s browser became [...]<p><p>Read the full article at:  <a href="http://davidwalsh.name/year-mozilla">One Year At&nbsp;Mozilla</a></p>

<p><a href="http://dwf.tw/treehouse"><img src="http://davidwalsh.name/wp-content/themes/jack/images/treehouse-2.png" alt="Treehouse"></a></p>

<p><a href="http://dwf.tw/senchacon"><img src="http://davidwalsh.name/wp-content/themes/jack/images/senchaCon.png" alt="Sencha Conference"></a></p></p>
]]></description>
				<content:encoded><![CDATA[<p>When I was a naive but bright-eyed kid attending a small technical college in 2002, I was first introduced to Mozilla.  The Mozilla browser looked incredibly similar to Netscape but provided a few additional development tools (like syntax-highlighted source windows)!  Developing within Mozilla&#8217;s browser really sparked my love of web development.  Then Mozilla&#8217;s browser became Firebird, and then eventually Firefox.</p>

<p>In 2009 I caught the attention of Mozilla (or a Moz recruiter) and I was flown to Palo Alto for an interview.  Mozilla was relatively small in those days;  the offices were rented across from Google, the building was old, there were many fewer employees, and many fewer projects.  I was devastated to not get hired back then but I kept a Mozilla employee&#8217;s business card in my car to remind me, when I drove to work each day, that my goal was to represent the dinosaur and that I was on the fringe of being &#8220;good enough.&#8221;  There&#8217;s no better reminder of a goal like that then seeing the dino on the way to a job you dislike.</p>

<p>Three years later, in April of 2012, I was again summoned to Mozilla and I was blessed enough to make the cut.  My second week was spent in Santa Clara <del>getting tipsy</del> meeting with my coworkers, proposing new ideas, improving my team skills, playing beach soccer, and winning that work week&#8217;s hack contest.  When you wake up in a hotel overlooking the ocean in your second week&#8230;you know you&#8217;re doing OK.</p>

<p><img src="http://davidwalsh.name/demo/mozilladevs.jpg" alt="David Walsh Mozilla" /></p>

<p>My primary responsibility has been and continues to be the Mozilla Developer Network.  I&#8217;ve been working with legends like Luke Crouch, Les (&#8220;Is More&#8221;) Orchard, James Bennett, Eric Shepard, Ali Spivak, and numerous other quality developers and documentation gods.  I&#8217;ve worked for an incredible manager, James Socol, who has been the best manager I&#8217;ve ever worked for;  quick to get answers, honest in criticism, and never afraid (even motivated) to jump into a code review.  Even the WebDev boss, Mike Morgan, listens to my hoops smack talk.  The atmosphere and developers I&#8217;ve found myself around are unbelievable.  Inspired to do legendary things every day.</p>

<p>Since joining Mozilla, I&#8217;ve been able to:</p>
<ul>
	<li><span style="line-height: 13px;">Help launch kuma, the new MDN platform</span></li>
	<li>Implement new features on MDN</li>
	<li>Travel to Brazil to speak about MDN and Firefox OS</li>
	<li>Travel to Toronto, Mountain View, and Austin to talk about Firefox OS and MDN</li>
	<li>Dip my hand into gaia (Firefox OS), X-Tags, Firefox Marketplace, Mozilla Hacks</li>
	<li>Be a test drive for the Firefox OS phone</li>
	<li>Make acquaintances with hundreds of incredible developers</li>
</ul>
<p>That&#8217;s one hell of a year, man.  This first year at Mozilla has been a blast, and look forward to many more years helping to keep an open web!</p><p><p>Read the full article at:  <a href="http://davidwalsh.name/year-mozilla">One Year At&nbsp;Mozilla</a></p>

<p><a href="http://dwf.tw/treehouse"><img src="http://davidwalsh.name/wp-content/themes/jack/images/treehouse-2.png" alt="Treehouse"></a></p>

<p><a href="http://dwf.tw/senchacon"><img src="http://davidwalsh.name/wp-content/themes/jack/images/senchaCon.png" alt="Sencha Conference"></a></p></p>
]]></content:encoded>
			<wfw:commentRss>http://davidwalsh.name/year-mozilla/feed</wfw:commentRss>
		<slash:comments>4</slash:comments>
		</item>
		<item>
		<title>JS Objects: Inherited a&#160;Mess</title>
		<link>http://davidwalsh.name/javascript-objects</link>
		<comments>http://davidwalsh.name/javascript-objects#comments</comments>
		<pubDate>Mon, 22 Apr 2013 19:39:36 +0000</pubDate>
		<dc:creator>Kyle Simpson</dc:creator>
				<category><![CDATA[Guest Blogger]]></category>
		<category><![CDATA[JavaScript]]></category>

		<guid isPermaLink="false">http://davidwalsh.name/?p=6458</guid>
		<description><![CDATA[JS Objects:&#160;TL;DR JavaScript has been plagued since the beginning with misunderstanding and awkwardness around its &#8220;prototypal inheritance&#8221; system, mostly due to the fact that &#8220;inheritance&#8221; isn&#8217;t how JS works at all, and trying to do that only leads to gotchas and confusions that we have to pave over with user-land helper libs. Instead, embracing that [...]<p><p>Read the full article at:  <a href="http://davidwalsh.name/javascript-objects">JS Objects: Inherited a&nbsp;Mess</a></p>

<p><a href="http://dwf.tw/treehouse"><img src="http://davidwalsh.name/wp-content/themes/jack/images/treehouse-2.png" alt="Treehouse"></a></p>

<p><a href="http://dwf.tw/senchacon"><img src="http://davidwalsh.name/wp-content/themes/jack/images/senchaCon.png" alt="Sencha Conference"></a></p></p>
]]></description>
				<content:encoded><![CDATA[<div class="gb-intro">
<h3>JS Objects:&nbsp;TL;DR</h3>
<p>JavaScript has been plagued since the beginning with misunderstanding and awkwardness around its &#8220;prototypal inheritance&#8221; system, mostly due to the fact that &#8220;inheritance&#8221; isn&#8217;t how JS works at all, and trying to do that only leads to gotchas and confusions that we have to pave over with user-land helper libs. Instead, embracing that JS has &#8220;behavior delegation&#8221; (merely delegation links between objects) fits naturally with how JS syntax works, which creates more sensible code without the need of helpers.</p>
<p>When you set aside distractions like mixins, polymorphism, composition, classes, constructors, and instances, and only focus on the objects that link to each other, you gain a powerful tool in behavior delegation that is easier to write, reason about, explain, and code-maintain. Simpler is better. JS is &#8220;objects-only&#8221; (OO). Leave the classes to those other languages!</p>

<h3>Due&nbsp;Thanks</h3>
<p>I&#8217;d like to thank the following amazing devs for their generous time in feedback/tech review of this article series: David Bruant, Hugh Wood, Mark Trostler, and Mark McDonnell. I am also honored that David Walsh wanted to publish these articles on his fantastic blog.</p>

<h3>Complete&nbsp;Series</h3>
<ul>
	<li><a href="http://davidwalsh.name/javascript-objects">Part 1:  JS Objects: Inherited a Mess</a></li>
	<li><a href="http://davidwalsh.name/javascript-objects-distractions">Part 2:  JS Objects: Distractions</a></li>
	<li><a href="http://davidwalsh.name/javascript-objects-deconstruction">Part 3:  JS Objects: De&#8221;construct&#8221;ion</a></li>
</ul>
</div>

<blockquote><p>year 2013: Haskell people are still writing monad tutorials, JavaScript people are still trying to explain inheritance.</p>

<p>— Vyacheslav Egorov (@mraleph) <a rel="nofollow" href="https://twitter.com/mraleph/status/322843655381590016">April 12, 2013</a></p></blockquote>

<p>As sad a criticism on JS as that quote is, it&#8217;s quite true. (I have no perspective on Haskell or Monads, so I&#8217;m only talking about the JS and inheritance part). Of all the confusing and, depending on your biases, &#8220;bad&#8221;, parts of this JS language, the behaviors of <code>this</code> and the <code>[[Prototype]]</code> chain have remained some of the most elusive to explain and use accurately.</p>

<p>As a bit of background, I&#8217;ve been developing JS full time since 2007. The first major epiphany I had back then was the understanding of how closures work, and how they enable the classic module pattern. The first open-source project I wrote (early 2008) was <a rel="nofollow" href="https://github.com/flensed/flXHR">flXHR</a>, a cross-domain Ajax <a rel="nofollow" href="https://twitter.com/SlexAxton/status/257543702124306432">prollyfill</a> using the standard Ajax (XHR) interface (via a hidden flash element) that relied heavily upon the module pattern.</p>

<p>It&#8217;s quite possibly my &#8220;ah-ha!&#8221; moment around the module pattern that satisfied me enough that I never really felt a strong need to also apply the &#8220;inheritance&#8221; pattern to my JS design.</p>

<p>Nevertheless, like most JS developers, I&#8217;ve read lots of blogs and books over the years that have tried (and mostly failed) to explain the appeal and mystery that is &#8220;JavaScript inheritance&#8221; (aka, &#8220;prototypal inheritance&#8221;).</p>

<p>But if it&#8217;s so hard to understand, and even harder to actually do correctly, the <em>point</em> yet eludes me. And apparently I&#8217;m not alone in that frustration.</p>

<h2>OO in&nbsp;JavaScript</h2>
<p>In traditional <a rel="nofollow" href="http://en.wikipedia.org/wiki/Object-oriented_programming">Object-oriented languages</a>, the <a rel="nofollow" href="http://www.cs.bu.edu/teaching/cpp/inheritance/intro/">syntax of classes</a> matches the semantics. You can express the object-oriented concepts of classes, inheritance, and polymorphism directly and explicitly using the language&#8217;s syntax. There&#8217;s no need to use some helper library to fake your way into OO-like behavior through work-arounds of other language facilities.</p>

<p>JavaScript on the other hand has <a rel="nofollow" href="https://developer.mozilla.org/en-US/docs/JavaScript/Introduction_to_Object-Oriented_JavaScript">a set of syntax that looks somewhat OO</a>, but which behaves in frustratingly different ways (which we will cover throughout this article series). As a result, the common way that you implement OO patterns in JS is through any of a variety of user-land helper libraries which let you express the desired semantic relationships between your &#8220;objects&#8221;. The reason most JS developers use them is because the underlying JS syntax makes those semantic expressions awkward. It&#8217;s nice to just let a library handle paving over the confusing syntax hiccups.</p>

<p>Libraries like jQuery are useful because they hide the ugly details of dealing with <strong>cross-browser differences in JS engines</strong>. But these OO-helper libraries are different: they&#8217;re going to great lengths to <strong>hide the true nature of JavaScript&#8217;s OO mechanisms</strong>, instead masking them in a set of patterns that are more familiar to other languages.</p>

<p>At this point of understanding, we should really ask ourselves: is the difficulty of <a rel="nofollow" href="http://javascript.crockford.com/inheritance.html">expressing classes and inheritance in pure JavaScript</a> a failure of the language (one which can <a rel="nofollow" href="http://prototypejs.org/learn/class-inheritance">temporarily</a> be <a rel="nofollow" href="http://mootools.net/docs/core/Class/Class">solved</a> with <a rel="nofollow" href="http://ejohn.org/blog/simple-javascript-inheritance/">user libraries</a>and ultimately solved by <a rel="nofollow" href="http://wiki.ecmascript.org/doku.php?id=strawman:maximally_minimal_classes">additions to the language like <code>class { .. }</code></a> syntax), <a rel="nofollow" href="https://c9.io/site/blog/2011/11/the-time-has-come-to-add-classes-to-javascript/">as many</a> devs <a rel="nofollow" href="http://www.nczonline.net/blog/2012/10/16/does-javascript-need-classes/">feel</a>, or is it something deeper? Is it indicative of a more fundamental disparity, that we&#8217;re trying to <a rel="nofollow" href="http://www.kirupa.com/html5/objects_classes_javascript.htm">do something in JS</a> that it&#8217;s<a rel="nofollow" href="http://webreflection.blogspot.com/2010/01/better-javascript-classes.html">just not meant to do</a>?</p>

<p><a rel="nofollow" href="http://www.2ality.com/2011/11/javascript-classes.html">Not everyone</a> drank the JS classes kool-aid, so the rest of this article series will favor a different perspective.</p>
<h2>Blueprint</h2>
<p>One of the most common metaphors used in <a rel="nofollow" href="http://www.tutorialspoint.com/cplusplus/cpp_inheritance.htm">traditional class/inheritance OO</a> is that the <a rel="nofollow" href="http://msdn.microsoft.com/en-us/library/ee5edha0%28v=vs.80%29.aspx">class represents a &#8220;blueprint&#8221;</a> for a house to be built, but once you instantiate that class, you are basically copying all the characteristics from the blueprint into the actual built house. This metaphor partially matches, to an extent, what actually happens at a language level when the code is compiled, in that it sort-of flattens the definition of a class (sans &#8220;virtual&#8221; methods) inheritance hierarchy into the instance.</p>

<p>Of course, a main pillar of <a rel="nofollow" href="http://www.alexonlinux.com/how-inheritance-encapsulation-and-polymorphism-work-in-cpp">inheritance-oriented coding is overriding and polymorphism</a>, which allows an object to<em>automatically</em> access the most descendant definition for a method, but also to use <code>super</code>-style relative references to access ancestor (aka &#8220;virtual&#8221;) versions of the <em>same-named</em> method. In those cases, the compiler maintains lookup tables for the virtual methods, but it flattens out the non-virtual parts of the class/inheritance definition. The compiler can determine a lot about what needs to be preserved and not and highly optimize the definition structure it creates in the compiled code.</p>

<p>For our purposes, we can think of traditional class-inheritance as basically a flattening &#8220;copy&#8221; of behavior down the chain to the instance. Here&#8217;s a diagram to illustrate the inheritance relationship between a parent/base class<code>Foo</code>, and child class <code>Bar</code>, and then instances of each, respectively named <code>foo1</code>, <code>foo2</code>, <code>bar1</code>, and<code>bar2</code>. Visually, the arrows (aka, &#8220;copying&#8221;) point from left-to-right and top-to-bottom:</p>

<p><a rel="nofollow" href="http://davidwalsh.name/demo/InheritanceArrows.png" target="_blank"><img alt="Inheritance Arrows" src="http://davidwalsh.name/demo/InheritanceArrows.png" /></a></p>
<h2>What&#8217;s in a&nbsp;name?</h2>
<p>Despite the borrowed implications of the common name &#8220;prototypal inheritance&#8221;, JavaScript&#8217;s mechanism works quite differently, which we&#8217;ll see in just a moment.</p>

<p>Both <a rel="nofollow" href="http://dictionary.reference.com/browse/inheritance?s=t">definitionally</a> (&#8220;&#8230;characteristics transmitted from parent to offspring&#8221;) and behaviorally (as described above), &#8220;inheritance&#8221; is most closely associated with the idea of &#8220;copying&#8221; from parent to child.</p>

<p>When you then take &#8220;inheritance&#8221; and apply it to a mechanism which has some very different behavior, you are asking for the confusion which has plagued &#8220;JavaScript inheritance&#8221; <a rel="nofollow" href="https://developer.mozilla.org/en-US/docs/JavaScript/Introduction_to_Object-Oriented_JavaScript">documentation</a>, <a rel="nofollow" href="http://blog.rjzaworski.com/2013/03/composition-in-javascript/">education</a>, and <a rel="nofollow" href="http://tobyho.com/2011/11/11/js-object-inheritance/">usage</a> for nearly 2 decades.</p>

<p>To try to wade through this mess, let&#8217;s set aside the label &#8220;inheritance&#8221; and its implications for JS, and hopefully we can arrive at something that is both conceptually more accurate and functionally more useful.</p>
<h2>A.B.D&#8217;s: Always Be&nbsp;Delegating</h2>
<p>JavaScript&#8217;s OO-like property mechanism for <a rel="nofollow" href="http://www.ecma-international.org/ecma-262/5.1/#sec-8.6">objects</a> is notated by <code>[[Prototype]]</code>, which is the internal characteristic of <strong>any object</strong> called its prototype-chain &#8212; a special link to another object. It&#8217;s kind of like a scope mechanism, in that the <code>[[Prototype]]</code> linkage <a rel="nofollow" href="http://www.ecma-international.org/ecma-262/5.1/#sec-8.12.2">describes which alternate object should be referred to</a> if you request a property or method on your object which doesn&#8217;t exist.</p>

<p>In other words, you&#8217;re indicating an object to <strong>delegate</strong> behavior to if that behavior isn&#8217;t defined on the object in question.</p>

<p>The above class-oriented <code>Foo</code> and <code>Bar</code> example, expressed in JS, relates object <code>Bar.prototype</code> to<code>Foo.prototype</code>, and then the <code>foo1</code>, <code>foo2</code>, <code>bar1</code> and <code>bar2</code> objects to their respective <code>[[Prototype]]</code>s. The arrows (not copies but live-links) point in a right-to-left, bottom-to-top fashion in JS:</p>

<p><a rel="nofollow" href="https://dl.dropboxusercontent.com/u/2474669/jsdiagrams/DelegationArrows.png" target="_blank"><img alt="Delegation Arrows" src="http://davidwalsh.name/demo/DelegationArrows.png" /></a></p>

<p>&#8220;Behavior delegation&#8221; is a more accurate term to describe JavaScript&#8217;s <code>[[Prototype]]</code>. This is not just a matter of word semantics, it&#8217;s a fundamentally different type of functionality.</p>

<p>If you try to illustrate behavior delegation in terms of the &#8220;blueprint&#8221; metaphor, you quickly see how it totally breaks down. There&#8217;s no way that my home, lacking a guest bedroom, could simply refer to another house, or to the original blueprints, to provide a bedroom for my mother-in-law when she comes to visit. Though the outcomes you can achieve have <strong><em>some</em></strong> respective similarities, the concepts of &#8220;inheritance&#8221; and &#8220;behavior delegation&#8221; <em>are quite different</em>.</p>

<p>Some devs insist that &#8220;delegation&#8221; is just the dynamic version of &#8220;inheritance&#8221;, like two sides of the same coin, but I see them <strong>as orthagonal systems</strong>.</p>

<h3>How to&nbsp;delegate?</h3>
<p>We&#8217;ll revisit this later in the article series, but <a rel="nofollow" href="https://developer.mozilla.org/en-US/docs/JavaScript/Reference/Global_Objects/Object/create"><code>Object.create(..)</code> was added to ES5</a> to assist with creating an object and then optionally linking its <code>[[Prototype]]</code> to another object. The link that is created is a delegation link, as opposed to an inheritance-by-copy.</p>

<p><strong>Note:</strong> Once an object has its <code>[[Prototype]]</code> chain set at its creation, it <em>should</em> for the most part be considered set in stone and not changeable. Technically, browsers which support <a rel="nofollow" href="https://developer.mozilla.org/en-US/docs/JavaScript/Reference/Global_Objects/Object/proto">the <code>__proto__</code> property</a>, a public representation of the internal link, allow you to change at any time where an object is linked to. However, this practice is littered with landmines and generally frowned upon &#8212; it&#8217;s almost certainly something you&#8217;d want to<strong>avoid</strong> in your code.</p>

<a name="spade-spade" id="spade-spade"></a>
<h2>Spade a&nbsp;Spade</h2>
<p>You&#8217;ve seen how the mechanisms in JavaScript are comparitively different from the mechanisms in other languages. But is it ok to just hand-waive over these differences so we can keep using the term &#8220;inheritance&#8221; for JS?</p>

<p>The fact is, it&#8217;s just <strong>not</strong> an accurate usage of the term. By insisting that JavaScript has &#8220;inheritance&#8221;, we&#8217;re really saying that the meaning of the word &#8220;inheritance&#8221; doesn&#8217;t matter, or is rather soft.</p>

<p>JS doesn&#8217;t statically analyze what parts of an inheritance chain it can safely flatten out <strong>and copy</strong>, it maintains links to the entire delegation chain throughout runtime, <strong>as distinct objects</strong>, which means our code can take advantage of <a rel="nofollow" href="http://michaux.ca/articles/early-mixins-late-mixins">a variety of powerful &#8220;late binding&#8221; dynamic patterns</a>.</p>

<p>If we keep trying to mimic inheritance in JavaScript (syntax hurdles be damned), we get <em>distracted</em> and <strong>miss out on <em>all that power</em> that was built into our language from the start</strong>.</p>

<p>I say: let&#8217;s call it what it is, and stop trying to pile on JavaScript these other concepts that the &#8220;inheritance&#8221; label implies.
<h2>So&nbsp;What?</h2>
<p>So far, I&#8217;ve tried to identify some misconceptions about JS&#8217;s <code>[[Prototype]]</code> mechanism and how &#8220;inheritance&#8221; is not a helpful label.</p>

<p>You may still be skeptical why it <em>actually</em> matters what we call this OO-like mechanism in JS? In the <a href="http://davidwalsh.name/javascript-objects-distractions">next part</a> of the article series, I&#8217;m going to address many of the trappings of traditional &#8220;class-based&#8221; programming which I think are distractions that lead us to missing out on the essence of how JS objects interoperate. In fact, we could even say that <a rel="nofollow" href="http://www.infoq.com/presentations/Classes-Are-Premature-Optimization">classes/inheritance are a premature optimization</a> for JavaScript.</p>

<p>Clearing those distractions out of the way leads us to <a href="http://davidwalsh.name/javascript-objects-deconstruction">part 3</a>, where we&#8217;ll see a simpler and more robust pattern for our JS code, and more importantly, <strong>our code will actually match our semantics</strong> <em>without</em> us having to jump through hoops to hide the ugly mismatches.</p>

<div class="gb-intro"><p>Look forward to <a href="http://davidwalsh.name/javascript-objects-distractions">parts 2</a> and 3 later this week!</p></div><p><p>Read the full article at:  <a href="http://davidwalsh.name/javascript-objects">JS Objects: Inherited a&nbsp;Mess</a></p>

<p><a href="http://dwf.tw/treehouse"><img src="http://davidwalsh.name/wp-content/themes/jack/images/treehouse-2.png" alt="Treehouse"></a></p>

<p><a href="http://dwf.tw/senchacon"><img src="http://davidwalsh.name/wp-content/themes/jack/images/senchaCon.png" alt="Sencha Conference"></a></p></p>
]]></content:encoded>
			<wfw:commentRss>http://davidwalsh.name/javascript-objects/feed</wfw:commentRss>
		<slash:comments>20</slash:comments>
		</item>
		<item>
		<title>Incredible Analytics with Crazy&#160;Egg</title>
		<link>http://davidwalsh.name/crazyegg</link>
		<comments>http://davidwalsh.name/crazyegg#comments</comments>
		<pubDate>Mon, 22 Apr 2013 13:18:47 +0000</pubDate>
		<dc:creator>David Walsh</dc:creator>
				<category><![CDATA[Optimization]]></category>

		<guid isPermaLink="false">http://davidwalsh.name/?p=6410</guid>
		<description><![CDATA[Analytics tracking is an important part of ensuring the success of any website or app, and the better information you get, the more you can do with that information.  I&#8217;ve always loved and appreciated what Google Analytics provides in the way of basic information, but GA&#8217;s lacked in visual presentation and &#8220;snapshot storage&#8221; of analytics. [...]<p><p>Read the full article at:  <a href="http://davidwalsh.name/crazyegg">Incredible Analytics with Crazy&nbsp;Egg</a></p>

<p><a href="http://dwf.tw/treehouse"><img src="http://davidwalsh.name/wp-content/themes/jack/images/treehouse-2.png" alt="Treehouse"></a></p>

<p><a href="http://dwf.tw/senchacon"><img src="http://davidwalsh.name/wp-content/themes/jack/images/senchaCon.png" alt="Sencha Conference"></a></p></p>
]]></description>
				<content:encoded><![CDATA[<p>Analytics tracking is an important part of ensuring the success of any website or app, and the better information you get, the more you can do with that information.  I&#8217;ve always loved and appreciated what Google Analytics provides in the way of basic information, but GA&#8217;s lacked in visual presentation and &#8220;snapshot storage&#8221; of analytics.  I recently found <a href="http://dwf.tw/crazyegg" rel="nofollow">Crazy Egg</a>, another analytics vendor which focuses more on visual representations of visitor data than basic visit and pageview numbers.  Let me share with you how Crazy Egg works and how it can help you learn more about your visitors!</p>

<h2>Signup</h2>

<p>Crazy Egg is a paid service with plans starting at $10/month.  Since <a href="http://dwf.tw/crazyegg" rel="nofollow">Crazy Egg</a> is a sponsor, I can extend my visitors <a href="http://dwf.tw/crazyegg" rel="nofollow">one free month</a> to give it a try!</p>

<div class="actions"><a href="http://dwf.tw/crazyegg" class="demo">Try Crazy Egg Free</a><div class="clear"></div></div>

<h2>Creating&nbsp;Snapshots</h2>

<p>Once signed into your Crazy Egg account, the first step is creating a <em>snapshot</em>.  Snapshots are a moment in time for a given page &#8212; think of snapshots as a virtual screenshot.</p>

<p><img src="http://davidwalsh.name/demo/crazy-egg-screenshot-1.png" alt="Crazy Egg Snapshot" /></p>

<p>Upon creation of a screenshot, a small code snippet is provided to enable stat tracking for that given snapshot:</p>

<pre class="js">
setTimeout(function(){var a=document.createElement("script");
var b=document.getElementsByTagName("script")[0];
a.src=document.location.protocol+"//dnn506yrbagrg.cloudfront.net/pages/scripts/0014/4786.js?"+Math.floor(new Date().getTime()/3600000);
a.async=true;a.type="text/javascript";b.parentNode.insertBefore(a,b)}, 1);
</pre>

<p>Add the code snippet to the footer of your site and Crazy Egg starts doing their thing!</p>

<h2>Statistical&nbsp;Visualization</h2>

<p>Once the snapshot code is in place and you let your uberpopular site rake in the visitors, you&#8217;re ready to analyze the result.  The following image shows a heatmap of my homepage for the day I published my epic <a href="http://davidwalsh.name/css-supports">CSS @supports</a> post:</p>


<p><a href="http://davidwalsh.name/demo/crazy-egg-screenshot-2.jpg"><img src="http://davidwalsh.name/demo/crazy-egg-screenshot-2-small.jpg" /></a></p>

<p><a href="http://davidwalsh.name/demo/crazy-egg-screenshot-3.jpg"><img src="http://davidwalsh.name/demo/crazy-egg-screenshot-3.jpg" /></a></p>

<p><a href="http://davidwalsh.name/demo/crazy-egg-screenshot-4.jpg"><img src="http://davidwalsh.name/demo/crazy-egg-screenshot-4.jpg" /></a></p>

<p>This imagery communicates to me that my demo pages are very popular (both in the menu and post list buttons), visitors search my site frequently, the &#8220;More Posts&#8221; button is incredibly popular, and PHP, HTML5, and jQuery are the most popular subjects clicked in the topic sidebar.  In knowing my goals, I can also see that the <a href="http://davidwalsh.name/forums">Forums</a> and <a href="http://davidwalsh.name/deals">Deals</a> pages are not as popular as I&#8217;d like, so I may try and promote them more in the future.  It&#8217;s incredible what a heatmap can show you!</p>

<p>Crazy Egg doesn&#8217;t just provide you heatmaps though.  Crazy Egg also provides ScrollMap, confetti, overlay, and list visualizations of visitor interaction.  If there&#8217;s another statistical visualization of web visitor data, I don&#8217;t know what it is.  Crazy Egg has given me a completely different perspective on my visitor interactions and what web developers should expect from analytics apps.  No wonder Amazon, eBay, Dell, and Zappos use Crazy Egg. The visual data that Crazy Egg provides me is easier to read and more useful than what Google Analytics provides me!</p>

<div class="actions"><a href="http://dwf.tw/crazyegg" class="demo">Try Crazy Egg Free</a><div class="clear"></div></div>

<h2>Go&nbsp;Crazy!</h2>

<p>Crazy Egg is offering a <a href="http://dwf.tw/crazyegg" rel="nofollow">free month to my visitors</a>, so if you&#8217;re looking for a more intelligent and visual statistics system, go crazy with Crazy Egg.  Crazy Egg has changed my expectation of site analytics and very much for the better.  If you like what I&#8217;ve presented here, give Crazy Egg a try &#8212; it may change the way you view your web results.</p><p><p>Read the full article at:  <a href="http://davidwalsh.name/crazyegg">Incredible Analytics with Crazy&nbsp;Egg</a></p>

<p><a href="http://dwf.tw/treehouse"><img src="http://davidwalsh.name/wp-content/themes/jack/images/treehouse-2.png" alt="Treehouse"></a></p>

<p><a href="http://dwf.tw/senchacon"><img src="http://davidwalsh.name/wp-content/themes/jack/images/senchaCon.png" alt="Sencha Conference"></a></p></p>
]]></content:encoded>
			<wfw:commentRss>http://davidwalsh.name/crazyegg/feed</wfw:commentRss>
		<slash:comments>4</slash:comments>
		</item>
	</channel>
</rss>

<!-- Performance optimized by W3 Total Cache. Learn more: http://www.w3-edge.com/wordpress-plugins/

Page Caching using disk: enhanced (User agent is rejected)
Database Caching 2/5 queries in 0.002 seconds using disk: basic
Object Caching 2157/2159 objects using disk: basic

Served from: davidwalsh.name @ 2013-05-19 11:26:48 -->