<?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>uxebu » blog &#187; javascript</title>
	<atom:link href="http://uxebu.com/tag/javascript/feed/" rel="self" type="application/rss+xml" />
	<link>http://uxebu.com/blog</link>
	<description></description>
	<lastBuildDate>Thu, 19 Jan 2012 14:53:24 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.2.1</generator>
		<item>
		<title>EmbedJS 0.2 and AMD</title>
		<link>http://uxebu.com/blog/2011/10/17/embedjs-0-2-and-amd/</link>
		<comments>http://uxebu.com/blog/2011/10/17/embedjs-0-2-and-amd/#comments</comments>
		<pubDate>Mon, 17 Oct 2011 15:43:16 +0000</pubDate>
		<dc:creator>Jens Arps</dc:creator>
				<category><![CDATA[development tools]]></category>
		<category><![CDATA[html5]]></category>
		<category><![CDATA[html5apps]]></category>
		<category><![CDATA[javascript]]></category>
		<category><![CDATA[mobile]]></category>
		<category><![CDATA[development]]></category>
		<category><![CDATA[dojo]]></category>
		<category><![CDATA[embedjs]]></category>

		<guid isPermaLink="false">http://uxebu.com/blog/?p=1843</guid>
		<description><![CDATA[During JSConf.eu, we released EmbedJS 0.2. That release was mainly about a fundamental architectural change: the move to the AMD pattern for our features. Let me explain why we decided to do this, why we think this is a big step forward and what this means for the user. Modules AMD stands for &#8220;Asynchronous Module [...]]]></description>
			<content:encoded><![CDATA[<p>During JSConf.eu, we released EmbedJS 0.2. That release was mainly about a fundamental architectural change: the move to the AMD pattern for our features. Let me explain why we decided to do this, why we think this is a big step forward and what this means for the user.<br />
<span id="more-1843"></span></p>
<h2>Modules</h2>
<p>AMD stands for &#8220;Asynchronous Module Definition&#8221;. So, what are modules? I&#8217;ll quote the definition from the <a href="http://wiki.commonjs.org/wiki/Modules/1.1" target="_blank">CommonJS Module specification</a>, as it describes it very well:</p>
<blockquote><p>This specification addresses how modules should be written in order to be interoperable among a class of module systems that can be both client and server side, secure or insecure, implemented today or supported by future systems with syntax extensions. These modules are offered privacy of their top scope, facility for importing singleton objects from other modules, and exporting their own API.</p></blockquote>
<p>So, modules are isolated parts of code that live in their own scope and are kind of self-contained. Well, that is pretty much how we see the individual Features in EmbedJS – we made our feature implementations as small as possible and reduced dependencies as much as possible. Many of EmbedJS&#8217; features are also browser-independent, so why not make it possible to use some of the tools in a non-browser environment?</p>
<p>(If you want to learn more on modules and why we need them, I recommend <a href="http://twitter.com/briancavalier" target="_blank">@briancavalier</a>&#8216;s <a href="http://briancavalier.com/presentations/pgh-js-amd-10-2011/" target="_blank">presentation on modules</a>.)</p>
<h2>Features in EmbedJS</h2>
<p>So far, I didn&#8217;t really explain what I mean with &#8220;Features&#8221;, so let me do another quote, this time from the <a href="http://uxebu.github.com/embedjs/" target="_blank">Project Page</a>:</p>
<blockquote><p>EmbedJS uses the concept of features: Functionalities are split up into features, as fine-grained as possible, and each feature might have multiple implementations.</p></blockquote>
<p>Our Features are kinda small and contain only few methods, sometimes even just one. The asyc-promise feature, for example, will get you Promises, and the transport-jsonp feature will get you a method for pull in data via jsonp – and that&#8217;s it.  If multiple methods are contained within one feature, this is because they are too closely related to each other to split them up, like the html-class feature, which contains the <code class="codecolorer text mac-classic"><span class="text">addClass</span></code>, <code class="codecolorer text mac-classic"><span class="text">removeClass</span></code>, <code class="codecolorer text mac-classic"><span class="text">toggleClass</span></code> and <code class="codecolorer text mac-classic"><span class="text">hasClass</span></code> methods.</p>
<h2>AMD vs CommonJS Modules 1.1</h2>
<p>Now that we knew modules were the way to go, there was another, quite important question to solve: There are two common patterns for modules, so which one do we want to apply? One pattern is described in the above mentioned CommonJS Wiki, the other pattern is described in the <a href="https://github.com/amdjs/amdjs-api/wiki/AMD" target="_blank">AMD wiki</a>.</p>
<p>Well, there have been <a href="http://tagneto.blogspot.com/2011/04/on-inventing-js-module-formats-and.html" target="_blank">some</a> <a href="http://unscriptable.com/index.php/2011/09/30/amd-versus-cjs-whats-the-best-format/" target="_blank">blog</a> <a href="http://blog.millermedeiros.com/2011/09/amd-is-better-for-the-web-than-commonjs-modules/" target="_blank">posts</a> lately that compare the pros and cons of each of them, so I don&#8217;t want to repeat all of this – but I&#8217;m hiving a similar tendency towards AMD. Besides this, there was another big reason to go with AMD: Remember, EmbedJS is based on the <a href="http://dojotoolkit.org/" target="_blank">Dojo Toolkit</a>, and it&#8217;s a <a href="http://dojofoundation.org/" target="_blank">Dojo Foundation Project</a> – and we hope that the efforts that went into making EmbedJS might flow back into the Dojo Toolkit one day. And as the Dojo Toolkit uses the AMD approach, the decision was quickly made to do so as well.</p>
<h2>Implications for EmbedJS</h2>
<p>There are several huge benefits for EmbedJS in using the AMD approach. The most noteworthy surely is the ease-of-use for developers. In your code, when you find you want an EmbedJS method, you <a href="http://uxebu.github.com/embedjs/docs/" target="_blank">look up the feature name</a> that provides this method, add it to the require statement and there you go. Man, easy.</p>
<p>A second important thing is that EmbedJS now plays really nice with AMD based projects. In fact, you could see EmbedJS now as a collection of small useful modules, that can be easily integrated – and also easily extended.</p>
<p>The third major benefit is that we can now get rid of all the custom tools we built around EmbedJS – you can load features with any AMD loader (though we use and recommend RequireJS) and build it with any AMD optimizer/builder (we recommend r.js).</p>
<p>Let&#8217;s have an extra word on</p>
<p><strong>Building</strong></p>
<p>It was always possible with EmbedJS to create a custom build that would contain only the features you wanted/needed in your project. But, you had to use our build tool that we created. And learning &amp; using Yet-Another-Build-Tool™ is always… well, let&#8217;s just say it would be cool to avoid it (and, not to forget, so is maintaining it). We did a lot to improve that, we created a GUI that allowed you to just &#8220;click together&#8221; the features you wanted, and we worked on a dynamic loader that would ease development. But still, the situation was far from ideal, and people just ended up going for our ready-made &#8220;kitchensink&#8221; builds that contained <em>all</em> of the features.</p>
<p>Now, we still provide those ready-made builds that contain only the implementations for a given platform (and are stripped of require/define calls), but I really hope that using the AMD approach and using an AMD optimizer to deploy will become the more adopted style.</p>
<h2>Q: Cross-target? A: The Feature Plugin</h2>
<p>One goal of EmbedJS is to allow for easy cross-device development. We got rid of run-time branching but instead went for an approach that held the different implementations for a given feature in separate files, so that one only has to deploy the actually needed code to a given device – and not all the alternate implementations, that sit useless in the device&#8217;s memory and never get used. We needed to achieve that with &#8220;in-house&#8221; means provided by the AMD approach. Lucky us, there&#8217;s the concept of <a href="https://github.com/amdjs/amdjs-api/wiki/Loader-Plugins" target="_blank">loader-plugins</a>, and so I created a <a href="https://github.com/jensarps/AMD-feature" target="_blank">feature-plugin for AMD loaders</a>. The idea is simple yet effective: You create an implementation map that maps a feature name to an implementation, and if there are multiple implementations, you supply tests that tells the plugin which feature to use.</p>
<p>When you&#8217;re ready to deploy, you have two choices: I f you really don&#8217;t know on what target your code is going to end up, you can use the dynamic implementation map containing the tests for the optimizer, and you&#8217;ll get a build that contains all of the available implementations. Then, during runtime, the feature plugin will run the tests and load the specific implementation. But, if you <em>do</em> know what target your code will run on, you provide a specific implementation map that doesn&#8217;t contain tests but directly maps to the according implementations, and you will only have those in your built file.</p>
<h2>Synopsis</h2>
<p>AMD is exactly what we needed to make EmbedJS more what we want it to be – an easy to use JavaScript toolbox that is especially handy for cross-target scenarios such as cross-platform mobile development. So, please don&#8217;t hesitate and check it out on Github, read the project page and check out the examples!</p>
<p>&nbsp;</p>
<p><strong>Resources / Further Reading</strong></p>
<ul>
<li><a href="https://github.com/uxebu/embedjs" target="_blank">EmbedJS on GitHub</a></li>
<li><a href="http://uxebu.github.com/embedjs/" target="_blank">EmbedJS Project Page</a></li>
<li><a href="http://uxebu.github.com/embedjs/docs/" target="_blank">EmbedJS API Docs</a></li>
<li><a href="https://github.com/jensarps/AMD-feature" target="_blank">AMD-Feature plugin</a></li>
<li><a href="https://github.com/amdjs/amdjs-api/wiki" target="_blank">AMD Wiki</a></li>
<li><a href="http://wiki.commonjs.org/wiki/CommonJS" target="_blank">CommonJS Wiki</a></li>
</ul>
]]></content:encoded>
			<wfw:commentRss>http://uxebu.com/blog/2011/10/17/embedjs-0-2-and-amd/feed/</wfw:commentRss>
		<slash:comments>4</slash:comments>
		</item>
		<item>
		<title>Creating a persistent Dojo Object Store</title>
		<link>http://uxebu.com/blog/2011/04/27/creating-a-persistent-dojo-object-store/</link>
		<comments>http://uxebu.com/blog/2011/04/27/creating-a-persistent-dojo-object-store/#comments</comments>
		<pubDate>Wed, 27 Apr 2011 16:13:45 +0000</pubDate>
		<dc:creator>Jens Arps</dc:creator>
				<category><![CDATA[dojo]]></category>
		<category><![CDATA[javascript]]></category>
		<category><![CDATA[offline]]></category>
		<category><![CDATA[storage]]></category>

		<guid isPermaLink="false">http://uxebu.com/blog/?p=1540</guid>
		<description><![CDATA[As of version 1.6, dojo comes with the new Dojo Object Store API. This is an awesome thing, as it greatly simplifies the work with data stores in Dojo. Everybody who had to do with the traditional dojo.data API felt it was overly complex and hard to use – this has finally changed now. There [...]]]></description>
			<content:encoded><![CDATA[<p>As of version 1.6, dojo comes with the new <a href="http://docs.dojocampus.org/dojo/store" target="_blank">Dojo Object Store API</a>. This is an awesome thing, as it greatly simplifies the work with data stores in Dojo. Everybody who had to do with the traditional dojo.data API felt it was overly complex and hard to use – this has finally changed now. There are also wrappers from and to the old and new APIs, so that you can do stuff like using your traditional data-aware widgets with a new Object Store. And the goodness doesn&#8217;t end here; but more on this later. If you haven&#8217;t done so yet, you might want to read the <a href="https://www.sitepen.com/blog/2011/02/15/dojo-object-stores/" target="_blank">excellent post on the new Dojo Object Stores</a> by Kris Zyp where he explains all the awesomeness he created.</p>
<p>Dojo also comes with two fresh implementations for the API, a non-persistent memory store and a JsonRest store that interacts with a server through RESTful HTTP requests. You can also observe changes made to objects in the store. What is missing is a store that uses a client-side persistent backend, which would be useful for a couple of reasons (e.g., as one commenter in that post asks, to use it as chache store for dojo.store.Cache). As Kris already mentions, it&#8217;s a piece of cake to create one, so let&#8217;s go ahead and do it!<br />
<span id="more-1540"></span></p>
<h2>Choosing the Backend</h2>
<p>The post mentions two possible candidates: dojox.storage and <a href="https://github.com/jensarps/StorageJS" target="_blank">StorageJS</a>, both already having a similar API, so making them compliant to the Dojo Object Store API is fairly easy. For this tutorial I choose StorageJS – because it is very lightweight and still provides all we need (and, well, for a couple of other reasons that don&#8217;t belong in this post). So, what is the exact API we need to comply to? Check it out <a href="http://docs.dojocampus.org/dojo/store#id2" target="_blank">in the docs</a>. See the first sentence in that paragraph? <em>&#8220;Every method in the API is optional, it&#8217;s presence indicating support for that feature.&#8221;</em> Wow, that&#8217;s nice!</p>
<h2>Basic compliance</h2>
<p>Let&#8217;s start with the methods get(), put(), add() and remove(). That will make our store pretty usable for most store scenarios.</p>
<p>First, create a &#8216;store&#8217; object we can work with (StorageJS creates a global variable called <code class="codecolorer text mac-classic"><span class="text">storage</span></code> where all it&#8217;s methods reside):</p>
<div class="codecolorer-container javascript twitlight" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:435px;"><table cellspacing="0" cellpadding="0"><tbody><tr><td style="padding:5px;text-align:center;color:#888888;background-color:#EEEEEE;border-right: 1px solid #9F9F9F;font: normal 12px/1.4em Monaco, Lucida Console, monospace;"><div>1<br /></div></td><td><div class="javascript codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap"><span style="color: #003366; font-weight: bold;">var</span> store <span style="color: #339933;">=</span> dojo.<span style="color: #660066;">delegate</span><span style="color: #009900;">&#40;</span>storage<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span></div></td></tr></tbody></table></div>
<p>&#8216;store&#8217; now contains everything that &#8216;storage&#8217; does, but we can leave the original &#8216;storage&#8217; object alone and modify our &#8216;store&#8217; object if needed. Time for the first method, let&#8217;s start with <code class="codecolorer text mac-classic"><span class="text">get()</span></code>. StorageJS works with Strings as keys and values only, so we need to parse what we get from it.</p>
<div class="codecolorer-container javascript twitlight" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:435px;"><table cellspacing="0" cellpadding="0"><tbody><tr><td style="padding:5px;text-align:center;color:#888888;background-color:#EEEEEE;border-right: 1px solid #9F9F9F;font: normal 12px/1.4em Monaco, Lucida Console, monospace;"><div>1<br />2<br />3<br />4<br /></div></td><td><div class="javascript codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap">store.<span style="color: #660066;">get</span> <span style="color: #339933;">=</span> <span style="color: #003366; font-weight: bold;">function</span><span style="color: #009900;">&#40;</span>id<span style="color: #009900;">&#41;</span><span style="color: #009900;">&#123;</span><br />
&nbsp; &nbsp; <span style="color: #006600; font-style: italic;">// Need to parse the string we get from backend:</span><br />
&nbsp; &nbsp; <span style="color: #000066; font-weight: bold;">return</span> dojo.<span style="color: #660066;">fromJson</span><span style="color: #009900;">&#40;</span>storage.<span style="color: #660066;">get</span><span style="color: #009900;">&#40;</span>id<span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
<span style="color: #009900;">&#125;</span><span style="color: #339933;">;</span></div></td></tr></tbody></table></div>
<p>Easy one, now move on to <code class="codecolorer text mac-classic"><span class="text">put()</span></code>. The Object Store API allows user to do stuff like this: <code class="codecolorer text mac-classic"><span class="text">store.put({foo: 'bar'}, {id: 3})</span></code> as well as <code class="codecolorer text mac-classic"><span class="text">store.put({id: 3, foo: 'bar'})</span></code>. Also, StorageJS only has the method set() instead of put() and add().</p>
<div class="codecolorer-container javascript twitlight" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:435px;"><table cellspacing="0" cellpadding="0"><tbody><tr><td style="padding:5px;text-align:center;color:#888888;background-color:#EEEEEE;border-right: 1px solid #9F9F9F;font: normal 12px/1.4em Monaco, Lucida Console, monospace;"><div>1<br />2<br />3<br />4<br />5<br />6<br />7<br />8<br />9<br />10<br /></div></td><td><div class="javascript codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap">store.<span style="color: #660066;">put</span> <span style="color: #339933;">=</span> <span style="color: #003366; font-weight: bold;">function</span><span style="color: #009900;">&#40;</span>object<span style="color: #339933;">,</span> options<span style="color: #009900;">&#41;</span><span style="color: #009900;">&#123;</span><br />
&nbsp; &nbsp; <span style="color: #006600; font-style: italic;">// Get the id, wherever it comes from</span><br />
&nbsp; &nbsp; <span style="color: #003366; font-weight: bold;">var</span> id <span style="color: #339933;">=</span> options <span style="color: #339933;">&amp;</span>amp<span style="color: #339933;">;&amp;</span>amp<span style="color: #339933;">;</span> options.<span style="color: #660066;">id</span> <span style="color: #339933;">||</span> object.<span style="color: #660066;">id</span><span style="color: #339933;">;</span><br />
<br />
&nbsp; &nbsp; <span style="color: #006600; font-style: italic;">// Stringify data -- remember, StorageJS is a key/value backend wrapper.</span><br />
&nbsp; &nbsp; <span style="color: #003366; font-weight: bold;">var</span> data <span style="color: #339933;">=</span> dojo.<span style="color: #660066;">toJson</span><span style="color: #009900;">&#40;</span>object<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
<br />
&nbsp; &nbsp; <span style="color: #006600; font-style: italic;">// Now save the data away</span><br />
&nbsp; &nbsp; storage.<span style="color: #660066;">set</span><span style="color: #009900;">&#40;</span>id<span style="color: #339933;">,</span> data<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
<span style="color: #009900;">&#125;</span><span style="color: #339933;">;</span></div></td></tr></tbody></table></div>
<p>Easy, as well – you could write this as a one-liner. Now, <code class="codecolorer text mac-classic"><span class="text">add()</span></code>. It&#8217;s close to put, except that it won&#8217;t overwrite data that already exists. So we just need to check if something with the given id already exists and throw an error if so.</p>
<div class="codecolorer-container javascript twitlight" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:435px;"><table cellspacing="0" cellpadding="0"><tbody><tr><td style="padding:5px;text-align:center;color:#888888;background-color:#EEEEEE;border-right: 1px solid #9F9F9F;font: normal 12px/1.4em Monaco, Lucida Console, monospace;"><div>1<br />2<br />3<br />4<br />5<br />6<br />7<br />8<br />9<br />10<br />11<br />12<br />13<br />14<br />15<br /></div></td><td><div class="javascript codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap">store.<span style="color: #660066;">add</span> <span style="color: #339933;">=</span> <span style="color: #003366; font-weight: bold;">function</span><span style="color: #009900;">&#40;</span>object<span style="color: #339933;">,</span> options<span style="color: #009900;">&#41;</span><span style="color: #009900;">&#123;</span><br />
&nbsp; &nbsp; <span style="color: #006600; font-style: italic;">// Get the id</span><br />
&nbsp; &nbsp; <span style="color: #003366; font-weight: bold;">var</span> id <span style="color: #339933;">=</span> options <span style="color: #339933;">&amp;</span>amp<span style="color: #339933;">;&amp;</span>amp<span style="color: #339933;">;</span> options.<span style="color: #660066;">id</span> <span style="color: #339933;">||</span> object.<span style="color: #660066;">id</span><span style="color: #339933;">;</span><br />
<br />
&nbsp; &nbsp; <span style="color: #006600; font-style: italic;">// Check if something exists under the given</span><br />
&nbsp; &nbsp; <span style="color: #006600; font-style: italic;">// id -- StorageJS always returns null when</span><br />
&nbsp; &nbsp; <span style="color: #006600; font-style: italic;">// there's no data for a certain key.</span><br />
&nbsp; &nbsp; <span style="color: #000066; font-weight: bold;">if</span><span style="color: #009900;">&#40;</span>storage.<span style="color: #660066;">get</span><span style="color: #009900;">&#40;</span>id<span style="color: #009900;">&#41;</span> <span style="color: #339933;">!==</span> <span style="color: #003366; font-weight: bold;">null</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#123;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #000066; font-weight: bold;">throw</span> <span style="color: #003366; font-weight: bold;">new</span> Error<span style="color: #009900;">&#40;</span><span style="color: #3366CC;">&quot;Object already exists&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp; <span style="color: #009900;">&#125;</span><br />
<br />
&nbsp; &nbsp; <span style="color: #006600; font-style: italic;">// We're save to go now! So hand over the</span><br />
&nbsp; &nbsp; <span style="color: #006600; font-style: italic;">// request to the put() method.</span><br />
&nbsp; &nbsp; store.<span style="color: #660066;">put</span><span style="color: #009900;">&#40;</span>object<span style="color: #339933;">,</span> options<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
<span style="color: #009900;">&#125;</span><span style="color: #339933;">;</span></div></td></tr></tbody></table></div>
<p>What about <code class="codecolorer text mac-classic"><span class="text">remove()</span></code>? Nothing to do there, it already has the desired signature and functionality.</p>
<p>That&#8217;s it, we&#8217;re done with basic compliance. Our &#8216;store&#8217; object now implements the basic methods of the Dojo Object Store API and can be used by anything that can work with a Dojo Object Store – and it will keep it&#8217;s state persistent in the browser.</p>
<h2>More compliance? Querying!</h2>
<p>There&#8217;s one interesting thing in the API: the <code class="codecolorer text mac-classic"><span class="text">query()</span></code> method. Uh, yeah, we want that! Lucky us, Dojo already provides a query engine as well as a method that makes sure there are iterative methods and a <code class="codecolorer text mac-classic"><span class="text">total</span></code> property containing the number of hits available in our result (well, it also abstracts away the differences between sync and async results, but as StorageJS is purely synchronous, this is not important to us). First, define a queryEngine:</p>
<div class="codecolorer-container javascript twitlight" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:435px;"><table cellspacing="0" cellpadding="0"><tbody><tr><td style="padding:5px;text-align:center;color:#888888;background-color:#EEEEEE;border-right: 1px solid #9F9F9F;font: normal 12px/1.4em Monaco, Lucida Console, monospace;"><div>1<br />2<br /></div></td><td><div class="javascript codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap"><span style="color: #006600; font-style: italic;">// Let's take the provided queryEngine :)</span><br />
store.<span style="color: #660066;">queryEngine</span> <span style="color: #339933;">=</span> dojo.<span style="color: #660066;">store</span>.<span style="color: #660066;">util</span>.<span style="color: #660066;">SimpleQueryEngine</span><span style="color: #339933;">;</span></div></td></tr></tbody></table></div>
<p>So, if we want our store to be able to run queries, all we need to do is to take the store&#8217;s data and hand it over to the query engine. We use StorageJS&#8217; getAll() method to acquire the data. It will return an array of objects in the form { key: &#8216;theKey&#8217;, value: &#8216;theValue&#8217;}, but the queryEngine wants an array containing only the objects, so we need to do a conversion.</p>
<div class="codecolorer-container javascript twitlight" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:435px;"><table cellspacing="0" cellpadding="0"><tbody><tr><td style="padding:5px;text-align:center;color:#888888;background-color:#EEEEEE;border-right: 1px solid #9F9F9F;font: normal 12px/1.4em Monaco, Lucida Console, monospace;"><div>1<br />2<br />3<br />4<br />5<br />6<br />7<br />8<br />9<br />10<br />11<br />12<br />13<br />14<br />15<br />16<br />17<br />18<br />19<br />20<br /></div></td><td><div class="javascript codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap">store.<span style="color: #660066;">query</span> <span style="color: #339933;">=</span> <span style="color: #003366; font-weight: bold;">function</span><span style="color: #009900;">&#40;</span>query<span style="color: #339933;">,</span> options<span style="color: #009900;">&#41;</span><span style="color: #009900;">&#123;</span><br />
&nbsp; &nbsp; <span style="color: #006600; font-style: italic;">// Get all data from the backend</span><br />
&nbsp; &nbsp; <span style="color: #003366; font-weight: bold;">var</span> rawData <span style="color: #339933;">=</span> storage.<span style="color: #660066;">getAll</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
<br />
&nbsp; &nbsp; <span style="color: #006600; font-style: italic;">// And convert it into a format that the</span><br />
&nbsp; &nbsp; <span style="color: #006600; font-style: italic;">// queryEngine can work with</span><br />
&nbsp; &nbsp; <span style="color: #003366; font-weight: bold;">var</span> data <span style="color: #339933;">=</span> <span style="color: #009900;">&#91;</span><span style="color: #009900;">&#93;</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp; <span style="color: #000066; font-weight: bold;">for</span><span style="color: #009900;">&#40;</span><span style="color: #003366; font-weight: bold;">var</span> i <span style="color: #339933;">=</span> <span style="color: #CC0000;">0</span><span style="color: #339933;">,</span> l <span style="color: #339933;">=</span> rawData.<span style="color: #660066;">length</span><span style="color: #339933;">;</span> i <span style="color: #339933;">&amp;</span>lt<span style="color: #339933;">;</span> l<span style="color: #339933;">;</span> i<span style="color: #339933;">++</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#123;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #003366; font-weight: bold;">var</span> <span style="color: #000066; font-weight: bold;">item</span> <span style="color: #339933;">=</span> rawData<span style="color: #009900;">&#91;</span>i<span style="color: #009900;">&#93;</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #003366; font-weight: bold;">var</span> dataObject <span style="color: #339933;">=</span> dojo.<span style="color: #660066;">fromJson</span><span style="color: #009900;">&#40;</span><span style="color: #000066; font-weight: bold;">item</span>.<span style="color: #660066;">value</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; data.<span style="color: #660066;">push</span><span style="color: #009900;">&#40;</span>dataObject<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp; <span style="color: #009900;">&#125;</span><br />
<br />
&nbsp; &nbsp; <span style="color: #006600; font-style: italic;">// Now call the queryEngine on the data</span><br />
&nbsp; &nbsp; <span style="color: #003366; font-weight: bold;">var</span> results <span style="color: #339933;">=</span> <span style="color: #000066; font-weight: bold;">this</span>.<span style="color: #660066;">queryEngine</span><span style="color: #009900;">&#40;</span>query<span style="color: #339933;">,</span> options<span style="color: #009900;">&#41;</span><span style="color: #009900;">&#40;</span>data<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
<br />
&nbsp; &nbsp; <span style="color: #006600; font-style: italic;">// And return the result, after it has been</span><br />
&nbsp; &nbsp; <span style="color: #006600; font-style: italic;">// enhanced.</span><br />
&nbsp; &nbsp; <span style="color: #000066; font-weight: bold;">return</span> dojo.<span style="color: #660066;">store</span>.<span style="color: #660066;">util</span>.<span style="color: #660066;">QueryResults</span><span style="color: #009900;">&#40;</span>results<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
<span style="color: #009900;">&#125;</span></div></td></tr></tbody></table></div>
<p>Now we can query our store for specific data, like so: <code class="codecolorer text mac-classic"><span class="text">var results = store.query({ prime: true });</span></code> This will give us all objects in the store that have a property called &#8216;prime&#8217; and have that property set to true.</p>
<h2>Hierarchie and Transactions</h2>
<p>The API also mentions methods about data hierarchie and transactions, but I&#8217;m not going to cover these in this tutorial.</p>
<h2>Convenience and Optimization</h2>
<p>Currently, every time we run a query, we fetch all data again from our storage engine. So, if you don&#8217;t have a massive amount of data and want to query the store a lot, it might be wise to maintain a copy of all data in memory. There are other things you could optimize as well – or you could write your own queryEngine.</p>
<p>For your convenience,  <a href="http://statis.uxebu.com/~jens/dojo.store/PersistentLocal.js" target="_blank">here&#8217;s a wrapper</a> that creates a Dojo Object Store like we did above and accepts a useMemory parameter during instantiation that denotes whether a copy of the data should be kept in memory or not. It uses the traditional require/provide instead of the AMD format. If you want to use it, make sure you use the right namespace in the declare() call! (Or, not recommended, copy it in the /dojo/store directory.)</p>
<p>If you want to fool around with it in the console first, you can go <a href="http://static.uxebu.com/~jens/dojo.store/index-persistent-nomemory.html" target="_blank">here (non-memory-using)</a> or <a href="http://static.uxebu.com/~jens/dojo.store/index-persistent-usememory.html" target="_blank">here (memory-using)</a>, check out the source and fire up the console and try things out.</p>
<h2>Getting StorageJS</h2>
<p>The above code uses the features base and getAll, so you could do a specific build of StorageJS with these features or just <a href="https://github.com/jensarps/StorageJS/tree/master/builds?raw=true" target="_blank">grab a full build</a> of it for your desired storage engine (the storage-full-[engine].js files). If you don&#8217;t know about storage engines, all modern browser support localStorage, so <a href="https://github.com/jensarps/StorageJS/raw/master/builds/storage-full-localStorage.js" target="_blank">this is the one</a> what you might want.</p>
]]></content:encoded>
			<wfw:commentRss>http://uxebu.com/blog/2011/04/27/creating-a-persistent-dojo-object-store/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Using IndexedDB on Firefox</title>
		<link>http://uxebu.com/blog/2011/03/30/using-indexeddb-on-firefox/</link>
		<comments>http://uxebu.com/blog/2011/03/30/using-indexeddb-on-firefox/#comments</comments>
		<pubDate>Wed, 30 Mar 2011 14:55:00 +0000</pubDate>
		<dc:creator>Jens Arps</dc:creator>
				<category><![CDATA[frontend engineering]]></category>
		<category><![CDATA[html5]]></category>
		<category><![CDATA[javascript]]></category>
		<category><![CDATA[firefox]]></category>
		<category><![CDATA[IDB]]></category>
		<category><![CDATA[indexedDB]]></category>
		<category><![CDATA[offline]]></category>
		<category><![CDATA[storage]]></category>

		<guid isPermaLink="false">http://uxebu.com/blog/?p=1494</guid>
		<description><![CDATA[As Firefox 4 is now stable, chances are that the async IDB (IndexedDB) API is not going to change anymore. But IDB is not localStorage – it can be a major pain to work with. So, here is a guide on how to use IDB in Firefox as a key-value store. TL;DR There is example code [...]]]></description>
			<content:encoded><![CDATA[<p>As Firefox 4 is now stable, chances are that the async IDB (IndexedDB) API is not going to change anymore. But IDB is not localStorage – it can be a major pain to work with. So, here is a guide on how to use IDB in Firefox as a key-value store.</p>
<p><em><strong>TL;DR</strong></em><br />
There is example code available at the end of the post. If you don‘t want to read this lengthy post and/or prefer to jump right in the middle of the action, grab the example implementation and play around with it.<br />
<span id="more-1494"></span></p>
<h1>Key-Value Store? But, IDB is a full-blown database!</h1>
<p>Yes, it is. But I still argue that in WebApp context a key-value store will cover about 90% of use cases. So I won‘t cover in this post how to create fancy key ranges or how to create multiple indices. But I will cover the five basic methods that you will need to work with any store: get, set, remove, getAll and clear – this will enable you to work with IDB and give you an understanding of how it works. You‘re then of course heartly encouraged to explore it and it‘s features further!</p>
<h1>A word on requests, transactions and events</h1>
<p>As long as the sync API is not there, all operations are async. That means that you will make requests. All the time. These requests will have an onerror and an onsuccess property, where you can put your callbacks/handlers in. If your request was successful, you will get an event in your callback. That event contains a lot of useful and useless information, but what you will want to look at in most cases is event.target.result – this is where your result lives in. In most cases, you will have to start a transaction before; inside of the transaction, you can access the objectStore and make your request. However, there are different types of transactions, but you will get the details below.</p>
<h1>Getting the Object Store ready to use</h1>
<p>Yep, it‘s not that you can just start away and store your data, there‘s some work to do ahead…</p>
<h2>Open the Database</h2>
<p>Easy one:</p>
<div class="codecolorer-container javascript twitlight" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:435px;"><table cellspacing="0" cellpadding="0"><tbody><tr><td style="padding:5px;text-align:center;color:#888888;background-color:#EEEEEE;border-right: 1px solid #9F9F9F;font: normal 12px/1.4em Monaco, Lucida Console, monospace;"><div>1<br />2<br />3<br />4<br /></div></td><td><div class="javascript codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap"><span style="color: #003366; font-weight: bold;">var</span> openRequest <span style="color: #339933;">=</span> mozIndexedDB.<span style="color: #000066;">open</span><span style="color: #009900;">&#40;</span>dbName<span style="color: #339933;">,</span> dbDescription<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
openRequest.<span style="color: #660066;">onsuccess</span> <span style="color: #339933;">=</span> <span style="color: #003366; font-weight: bold;">function</span><span style="color: #009900;">&#40;</span>event<span style="color: #009900;">&#41;</span><span style="color: #009900;">&#123;</span><br />
&nbsp; &nbsp; <span style="color: #006600; font-style: italic;">//event.target.result contains a reference to the db</span><br />
<span style="color: #009900;">&#125;</span></div></td></tr></tbody></table></div>
<p><code class="codecolorer text mac-classic"><span class="text">event.target.result</span></code> then contains a reference to the database. Store it, you will need it.</p>
<h2>Creating/Opening the ObjectStore</h2>
<p>That‘s a little tougher. Some operations, such as creating and deleting object stores, require the database to be in a „mutation transaction state“. Wow. Well, there‘s only one way to put the db into this state, and that‘s via a <code class="codecolorer text mac-classic"><span class="text">setVersion</span></code> request. If you are opening the db for the first time ever, you will need to do this anyway. You can, of course, change the version of the database anytime, which might be a useful thing if you, e.g., change the structure of how your objectStore saves the data. To check what version the database in the user‘s browser has, you can check the <code class="codecolorer text mac-classic"><span class="text">version</span></code> property in the reference to the database anytime.</p>
<div class="codecolorer-container javascript twitlight" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:435px;"><table cellspacing="0" cellpadding="0"><tbody><tr><td style="padding:5px;text-align:center;color:#888888;background-color:#EEEEEE;border-right: 1px solid #9F9F9F;font: normal 12px/1.4em Monaco, Lucida Console, monospace;"><div>1<br />2<br />3<br />4<br /></div></td><td><div class="javascript codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap"><span style="color: #003366; font-weight: bold;">var</span> versionRequest <span style="color: #339933;">=</span> db.<span style="color: #660066;">setVersion</span><span style="color: #009900;">&#40;</span>dbVersion<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
versionRequest.<span style="color: #660066;">onsuccess</span> <span style="color: #339933;">=</span> <span style="color: #003366; font-weight: bold;">function</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#123;</span><br />
&nbsp; &nbsp; <span style="color: #006600; font-style: italic;">// Woohoo, the database is in a mutation transaction!!</span><br />
<span style="color: #009900;">&#125;</span><span style="color: #339933;">;</span></div></td></tr></tbody></table></div>
<p>Inside of the callback function, you can now check if your store already exists, or if you need to create it. The reference to your db contains an object called <code class="codecolorer text mac-classic"><span class="text">objectStoreNames</span></code>, which is a list of existing objectStores. As this is a DOMStringList, it features the handy <code class="codecolorer text mac-classic"><span class="text">contains()</span></code> method:</p>
<div class="codecolorer-container javascript twitlight" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:435px;"><table cellspacing="0" cellpadding="0"><tbody><tr><td style="padding:5px;text-align:center;color:#888888;background-color:#EEEEEE;border-right: 1px solid #9F9F9F;font: normal 12px/1.4em Monaco, Lucida Console, monospace;"><div>1<br /></div></td><td><div class="javascript codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap"><span style="color: #003366; font-weight: bold;">var</span> hasMyStore <span style="color: #339933;">=</span> db.<span style="color: #660066;">objectStoreNames</span>.<span style="color: #660066;">contains</span><span style="color: #009900;">&#40;</span>storeName<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span></div></td></tr></tbody></table></div>
<p>If the store is not there, you need to create it via the <code class="codecolorer text mac-classic"><span class="text">createObjectStore()</span></code> method. It takes two arguments: the name of the store, and an object with two properties: <code class="codecolorer text mac-classic"><span class="text">keyPath</span></code> and <code class="codecolorer text mac-classic"><span class="text">autoIncrement</span></code>. keyPath is… well… the path to the key. Think of it as the one column that is the primary key in your data table (you all did some MySQL at some time, I bet). You can do really fancy things with the keyPath in objectStores, but it‘s perfectly fine to just use something like „id“ as the keyPath. If you want to store an object and want to provide the keyPath, all you need to do is let the object you want to store have a property with the name of the keyPath, and a unique value. Uh, it‘s not that complicated, examples will follow. AutoIncrement is, well, autoIncrement. You have the option to let the objectStore take care for the uniqueness of it‘s data‘s keyPath properties, or do it yourself.</p>
<div class="codecolorer-container javascript twitlight" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:435px;"><table cellspacing="0" cellpadding="0"><tbody><tr><td style="padding:5px;text-align:center;color:#888888;background-color:#EEEEEE;border-right: 1px solid #9F9F9F;font: normal 12px/1.4em Monaco, Lucida Console, monospace;"><div>1<br /></div></td><td><div class="javascript codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap"><span style="color: #003366; font-weight: bold;">var</span> store <span style="color: #339933;">=</span> db.<span style="color: #660066;">createObjectStore</span><span style="color: #009900;">&#40;</span>storeName<span style="color: #339933;">,</span> <span style="color: #009900;">&#123;</span>keyPath<span style="color: #339933;">:</span> <span style="color: #3366CC;">'id'</span><span style="color: #339933;">,</span> autoIncrement<span style="color: #339933;">:</span> <span style="color: #003366; font-weight: bold;">false</span><span style="color: #009900;">&#125;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span></div></td></tr></tbody></table></div>
<p>You immediately get a reference to the newly created objectStore, so save it.</p>
<p>If the store is already there, you need to open it. Um, well, you don‘t really have to open it, but you might want to obtain a reference to it, just to make sure that it really exists. To do this, you need to start a transaction via the <code class="codecolorer text mac-classic"><span class="text">transaction()</span></code> method. It accepts three arguments: an array of store names (the ones which you want to work with), an integer denoting whether you want to read and/or write during that transaction, and a timeout. There are constants for read/write you can use for better readability in your code, you can find them at <code class="codecolorer text mac-classic"><span class="text">IDBTransaction.READ_ONLY</span></code> and <code class="codecolorer text mac-classic"><span class="text">IDBTransaction.READ_WRITE</span></code>. The timeout parameter is optional. That method returns a transaction object that contains a method called <code class="codecolorer text mac-classic"><span class="text">objectStore</span></code>, which you can use to access your store. This is the „default“ way transactions work; the mutation transaction above is an eception to this, where you need to be in the callback of the transaction to modify the database.</p>
<p>In our case, we can start an „empty“ transaction:</p>
<div class="codecolorer-container javascript twitlight" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:435px;"><table cellspacing="0" cellpadding="0"><tbody><tr><td style="padding:5px;text-align:center;color:#888888;background-color:#EEEEEE;border-right: 1px solid #9F9F9F;font: normal 12px/1.4em Monaco, Lucida Console, monospace;"><div>1<br />2<br /></div></td><td><div class="javascript codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap"><span style="color: #003366; font-weight: bold;">var</span> emptyTransaction <span style="color: #339933;">=</span> db.<span style="color: #660066;">transaction</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#91;</span><span style="color: #009900;">&#93;</span><span style="color: #339933;">,</span> IDBTransaction.<span style="color: #660066;">READ_ONLY</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
<span style="color: #003366; font-weight: bold;">var</span> store <span style="color: #339933;">=</span> emptyTransaction.<span style="color: #660066;">objectStore</span><span style="color: #009900;">&#40;</span>storeName<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span></div></td></tr></tbody></table></div>
<p>There you have your reference to the store.</p>
<h1>Data Manipulation</h1>
<p>Now that the store is ready to use, it‘s time to store some data in it. To do so, you need to start a transaction, access the object store, and call the store‘s <code class="codecolorer text mac-classic"><span class="text">put</span></code> method:</p>
<div class="codecolorer-container javascript twitlight" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:435px;"><table cellspacing="0" cellpadding="0"><tbody><tr><td style="padding:5px;text-align:center;color:#888888;background-color:#EEEEEE;border-right: 1px solid #9F9F9F;font: normal 12px/1.4em Monaco, Lucida Console, monospace;"><div>1<br />2<br />3<br />4<br />5<br /></div></td><td><div class="javascript codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap"><span style="color: #003366; font-weight: bold;">var</span> putTransaction <span style="color: #339933;">=</span> db.<span style="color: #660066;">transaction</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#91;</span>storeName<span style="color: #009900;">&#93;</span><span style="color: #339933;">,</span> IDBTransaction.<span style="color: #660066;">READ_WRITE</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
<span style="color: #003366; font-weight: bold;">var</span> putRequest <span style="color: #339933;">=</span> putTransaction.<span style="color: #660066;">objectStore</span><span style="color: #009900;">&#40;</span>storeName<span style="color: #009900;">&#41;</span>.<span style="color: #660066;">put</span><span style="color: #009900;">&#40;</span>dataObj<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
putRequest.<span style="color: #660066;">onsuccess</span> <span style="color: #339933;">=</span> <span style="color: #003366; font-weight: bold;">function</span><span style="color: #009900;">&#40;</span>event<span style="color: #009900;">&#41;</span><span style="color: #009900;">&#123;</span><br />
&nbsp; &nbsp; <span style="color: #006600; font-style: italic;">//event.target.result contains the value of the keyPath of the data written (the „key“)</span><br />
<span style="color: #009900;">&#125;</span><span style="color: #339933;">;</span></div></td></tr></tbody></table></div>
<p>That‘s all. This type of transaction is what you also use for other data manipulation tasks, for example to retrieve data:</p>
<div class="codecolorer-container javascript twitlight" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:435px;"><table cellspacing="0" cellpadding="0"><tbody><tr><td style="padding:5px;text-align:center;color:#888888;background-color:#EEEEEE;border-right: 1px solid #9F9F9F;font: normal 12px/1.4em Monaco, Lucida Console, monospace;"><div>1<br />2<br />3<br />4<br />5<br /></div></td><td><div class="javascript codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap"><span style="color: #003366; font-weight: bold;">var</span> getTransaction <span style="color: #339933;">=</span> db.<span style="color: #660066;">transaction</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#91;</span>storeName<span style="color: #009900;">&#93;</span><span style="color: #339933;">,</span> IDBTransaction.<span style="color: #660066;">READ_ONLY</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
<span style="color: #003366; font-weight: bold;">var</span> getRequest <span style="color: #339933;">=</span> getTransaction.<span style="color: #660066;">objectStore</span><span style="color: #009900;">&#40;</span>storeName<span style="color: #009900;">&#41;</span>.<span style="color: #660066;">get</span><span style="color: #009900;">&#40;</span>key<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
getRequest.<span style="color: #660066;">onsuccess</span> <span style="color: #339933;">=</span> <span style="color: #003366; font-weight: bold;">function</span><span style="color: #009900;">&#40;</span>event<span style="color: #009900;">&#41;</span><span style="color: #009900;">&#123;</span><br />
&nbsp; &nbsp; <span style="color: #006600; font-style: italic;">//event.target.result contains the data object</span><br />
<span style="color: #009900;">&#125;</span><span style="color: #339933;">;</span></div></td></tr></tbody></table></div>
<p>You will need to provide the key of the stored entry to get it; as you need to do when you want to remove it:</p>
<div class="codecolorer-container javascript twitlight" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:435px;"><table cellspacing="0" cellpadding="0"><tbody><tr><td style="padding:5px;text-align:center;color:#888888;background-color:#EEEEEE;border-right: 1px solid #9F9F9F;font: normal 12px/1.4em Monaco, Lucida Console, monospace;"><div>1<br />2<br />3<br />4<br />5<br /></div></td><td><div class="javascript codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap"><span style="color: #003366; font-weight: bold;">var</span> removeTransaction <span style="color: #339933;">=</span> db.<span style="color: #660066;">transaction</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#91;</span>storeName<span style="color: #009900;">&#93;</span><span style="color: #339933;">,</span> IDBTransaction.<span style="color: #660066;">READ_WRITE</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
<span style="color: #003366; font-weight: bold;">var</span> deleteRequest <span style="color: #339933;">=</span> removeTransaction.<span style="color: #660066;">objectStore</span><span style="color: #009900;">&#40;</span>storeName<span style="color: #009900;">&#41;</span>.<span style="color: #000066; font-weight: bold;">delete</span><span style="color: #009900;">&#40;</span>key<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
deleteRequest.<span style="color: #660066;">onsuccess</span> <span style="color: #339933;">=</span> <span style="color: #003366; font-weight: bold;">function</span><span style="color: #009900;">&#40;</span>event<span style="color: #009900;">&#41;</span><span style="color: #009900;">&#123;</span><br />
&nbsp; &nbsp; <span style="color: #006600; font-style: italic;">//event.target.result contains the key of the deleted data object</span><br />
<span style="color: #009900;">&#125;</span><span style="color: #339933;">;</span></div></td></tr></tbody></table></div>
<p>If you want to get all data objects from the store:</p>
<div class="codecolorer-container javascript twitlight" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:435px;"><table cellspacing="0" cellpadding="0"><tbody><tr><td style="padding:5px;text-align:center;color:#888888;background-color:#EEEEEE;border-right: 1px solid #9F9F9F;font: normal 12px/1.4em Monaco, Lucida Console, monospace;"><div>1<br />2<br />3<br />4<br />5<br /></div></td><td><div class="javascript codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap"><span style="color: #003366; font-weight: bold;">var</span> getAllTransaction <span style="color: #339933;">=</span> db.<span style="color: #660066;">transaction</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#91;</span>storeName<span style="color: #009900;">&#93;</span><span style="color: #339933;">,</span> IDBTransaction.<span style="color: #660066;">READ_ONLY</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
<span style="color: #003366; font-weight: bold;">var</span> getAllRequest <span style="color: #339933;">=</span> getAllTransaction.<span style="color: #660066;">objectStore</span><span style="color: #009900;">&#40;</span>storeName<span style="color: #009900;">&#41;</span>.<span style="color: #660066;">getAll</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
getAllRequest.<span style="color: #660066;">onsuccess</span> <span style="color: #339933;">=</span> <span style="color: #003366; font-weight: bold;">function</span><span style="color: #009900;">&#40;</span>event<span style="color: #009900;">&#41;</span><span style="color: #009900;">&#123;</span><br />
&nbsp; &nbsp; <span style="color: #006600; font-style: italic;">//event.target.result contains an array of all data objects</span><br />
<span style="color: #009900;">&#125;</span><span style="color: #339933;">;</span></div></td></tr></tbody></table></div>
<p>And, finally, if you want to clear (i.e., delete all data objects) the store:</p>
<div class="codecolorer-container javascript twitlight" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:435px;"><table cellspacing="0" cellpadding="0"><tbody><tr><td style="padding:5px;text-align:center;color:#888888;background-color:#EEEEEE;border-right: 1px solid #9F9F9F;font: normal 12px/1.4em Monaco, Lucida Console, monospace;"><div>1<br />2<br />3<br />4<br />5<br /></div></td><td><div class="javascript codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap"><span style="color: #003366; font-weight: bold;">var</span> clearTransaction <span style="color: #339933;">=</span> db.<span style="color: #660066;">transaction</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#91;</span>storeName<span style="color: #009900;">&#93;</span><span style="color: #339933;">,</span> IDBTransaction.<span style="color: #660066;">READ_WRITE</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
<span style="color: #003366; font-weight: bold;">var</span> clearRequest <span style="color: #339933;">=</span> clearTransaction.<span style="color: #660066;">objectStore</span><span style="color: #009900;">&#40;</span>storeName<span style="color: #009900;">&#41;</span>.<span style="color: #660066;">clear</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
clearRequest.<span style="color: #660066;">onsuccess</span> <span style="color: #339933;">=</span> <span style="color: #003366; font-weight: bold;">function</span><span style="color: #009900;">&#40;</span>event<span style="color: #009900;">&#41;</span><span style="color: #009900;">&#123;</span><br />
&nbsp; &nbsp; <span style="color: #006600; font-style: italic;">//event.target.result contains undefined</span><br />
<span style="color: #009900;">&#125;</span><span style="color: #339933;">;</span></div></td></tr></tbody></table></div>
<h1>But how‘s this better than localStorage?</h1>
<p>It isn‘t. Yeah, well, it is, as you can store objects and don‘t have to stringify them before. But basically, to get the real advantages of IDB, you will need to dive deeper into it. Features like keyRanges remove the need to do map/reduce action when you search for items in your store. But now you have an idea of how the whole thing works. If you want to go further, browse in the spec or the MDC docs and try things out!</p>
<p><strong>Example Code</strong></p>
<p>There is an example page over here: <a href="http://static.uxebu.com/~jens/indexeddb/ff-idb.html" target="_blank">http://static.uxebu.com/~jens/indexeddb/ff-idb.html</a>.</p>
<p>It uses an ObjectStore wrapper I created using dojo that covers the basic methods mentioned above, the JavaScript is here: <a href="http://static.uxebu.com/~jens/indexeddb/idb-ff.js" target="_blank">http://static.uxebu.com/~jens/indexeddb/idb-ff.js</a>.</p>
<p>When you open the page, open the console and you will see some messages there. Check out the source to see what‘s going on; it‘s basically a round trip through all the basic methods.</p>
<p><strong>Further Reading</strong></p>
<ul>
<li>The spec: <a href="http://www.w3.org/TR/IndexedDB/" target="_blank">http://www.w3.org/TR/IndexedDB/</a></li>
<li>The MDC article: <a href="https://developer.mozilla.org/en/IndexedDB" target="_blank">https://developer.mozilla.org/en/IndexedDB</a></li>
</ul>
]]></content:encoded>
			<wfw:commentRss>http://uxebu.com/blog/2011/03/30/using-indexeddb-on-firefox/feed/</wfw:commentRss>
		<slash:comments>1</slash:comments>
		</item>
		<item>
		<title>Object-Based Inheritance For ECMAScript 5</title>
		<link>http://uxebu.com/blog/2011/02/23/object-based-inheritance-for-ecmascript-5/</link>
		<comments>http://uxebu.com/blog/2011/02/23/object-based-inheritance-for-ecmascript-5/#comments</comments>
		<pubDate>Wed, 23 Feb 2011 12:38:40 +0000</pubDate>
		<dc:creator>David Aurelio</dc:creator>
				<category><![CDATA[javascript]]></category>
		<category><![CDATA[ECMAScript 5]]></category>
		<category><![CDATA[Inheritance]]></category>
		<category><![CDATA[Object.create]]></category>

		<guid isPermaLink="false">http://uxebu.com/blog/?p=1405</guid>
		<description><![CDATA[This post describes a simple, object-based inheritance system for ECMAScript&#160;5 compliant runtimes. First, I will explain how inheritance works in JavaScript/ECMAScript, and how inheritance chains are built usually to give you a deeper understanding what I’m talking about. After that, I’ll introduce the new Object.create() method found in ECMAScript&#160;5 and an inheritance system built on [...]]]></description>
			<content:encoded><![CDATA[<p>This post describes a simple, object-based inheritance system for ECMAScript&nbsp;5 compliant runtimes. First, I will explain how inheritance works in JavaScript/ECMAScript, and how inheritance chains are built usually to give you a deeper understanding what I’m talking about. After that, I’ll introduce the new <em>Object.create()</em> method found in ECMAScript&nbsp;5 and an inheritance system built on top of that functionality.<br />
 <span id="more-1405"></span></p>
<h2>The Prototype Chain / Inheritance in JavaScript</h2>
<p>Inheritance in JavaScript bases on the <em>Prototype Chain.</em> Every object has an internal <em>“[[Prototype]]”</em> property pointing to another object or <code class="codecolorer text mac-classic"><span class="text">null</span></code>. The notation with square brackets is used in the <a href="http://www.ecma-international.org/publications/standards/Ecma-262.htm">ECMAScript specification</a>, I will stick to it in this post. This property typically isn&#8217;t accessible in a program – it is private to the JavaScript runtime, even though Gecko and some other runtimes have started exposing it through the non-standard <code class="codecolorer text mac-classic"><span class="text">__proto__</span></code> property.</p>
<p>When trying to access a property on an object (e.g. <code class="codecolorer text mac-classic"><span class="text">fooObject.barProp</span></code>), the runtime will follow the prototype chain from object to object (using the <em>[[Prototype]]</em> property) until the property is found. If it is not defined throughout the whole prototype chain, <code class="codecolorer text mac-classic"><span class="text">undefined</span></code> is returned.</p>
<p>The difference to class-based programming languages is, that property/method access is not resolved within a class hierarchy, but rather within an hierarchy of objects (“class instances”). Properties shared between instances (“shared properties” in ECMAScript terminology) and methods are defined on an object deeper in the chain, while “instance variables”(“own properties” in ECMAScript) are defined on the top-most object itself – the object you are working with.</p>
<h2>Creating Inheriting Objects</h2>
<p>The traditional method of creating objects with a specific prototype chain is to define a <em>constructor function</em> and set its <code class="codecolorer text mac-classic"><span class="text">prototype</span></code> property to the desired object.</p>
<div class="codecolorer-container javascript twitlight" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:435px;"><table cellspacing="0" cellpadding="0"><tbody><tr><td style="padding:5px;text-align:center;color:#888888;background-color:#EEEEEE;border-right: 1px solid #9F9F9F;font: normal 12px/1.4em Monaco, Lucida Console, monospace;"><div>1<br />2<br />3<br />4<br />5<br />6<br />7<br />8<br />9<br /></div></td><td><div class="javascript codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap"><span style="color: #003366; font-weight: bold;">function</span> MyConstructor<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span><br />
<span style="color: #009900;">&#125;</span><br />
<br />
MyConstructor.<span style="color: #660066;">prototype</span> <span style="color: #339933;">=</span> <span style="color: #009900;">&#123;</span><br />
&nbsp; &nbsp; foo<span style="color: #339933;">:</span> <span style="color: #3366CC;">&quot;some property&quot;</span><span style="color: #339933;">,</span><br />
&nbsp; &nbsp; bar<span style="color: #339933;">:</span> <span style="color: #003366; font-weight: bold;">function</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp;<span style="color: #000066; font-weight: bold;">return</span> <span style="color: #3366CC;">&quot;some method&quot;</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp; <span style="color: #009900;">&#125;</span><br />
<span style="color: #009900;">&#125;</span><span style="color: #339933;">;</span></div></td></tr></tbody></table></div>
<p>Now it is possible to create instances using the new operator:</p>
<div class="codecolorer-container javascript twitlight" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:435px;"><table cellspacing="0" cellpadding="0"><tbody><tr><td style="padding:5px;text-align:center;color:#888888;background-color:#EEEEEE;border-right: 1px solid #9F9F9F;font: normal 12px/1.4em Monaco, Lucida Console, monospace;"><div>1<br /></div></td><td><div class="javascript codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap"><span style="color: #003366; font-weight: bold;">var</span> myInstance <span style="color: #339933;">=</span> <span style="color: #003366; font-weight: bold;">new</span> MyConstructor<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span></div></td></tr></tbody></table></div>
<p>This code creates a new object, sets its internal <em>[[Prototype]]</em> property to <code class="codecolorer text mac-classic"><span class="text">MyConstructor.prototype</span></code>, and executes <code class="codecolorer text mac-classic"><span class="text">MyConstructor</span></code> in the context of the newly created object (“in the context” means that the <code class="codecolorer text mac-classic"><span class="text">this</span></code> keyword points to the new object).</p>
<p>The <code class="codecolorer text mac-classic"><span class="text">instanceof</span></code> operator allows us to check whether an object has been created by a specific constructor function:</p>
<div class="codecolorer-container javascript twitlight" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:435px;"><table cellspacing="0" cellpadding="0"><tbody><tr><td style="padding:5px;text-align:center;color:#888888;background-color:#EEEEEE;border-right: 1px solid #9F9F9F;font: normal 12px/1.4em Monaco, Lucida Console, monospace;"><div>1<br /></div></td><td><div class="javascript codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap">myInstance <span style="color: #000066; font-weight: bold;">instanceof</span> MyConstructor<span style="color: #339933;">;</span> <span style="color: #006600; font-style: italic;">// true</span></div></td></tr></tbody></table></div>
<p>(instanceof checks the whole prototype chain, and works with “subclasses”, too).<br />
This coupling of constructor and instances is rather loose; when creating a second constructor with its prototype pointing to the same object as the first constructor, an object will be instance of both constructors:</p>
<div class="codecolorer-container javascript twitlight" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:435px;"><table cellspacing="0" cellpadding="0"><tbody><tr><td style="padding:5px;text-align:center;color:#888888;background-color:#EEEEEE;border-right: 1px solid #9F9F9F;font: normal 12px/1.4em Monaco, Lucida Console, monospace;"><div>1<br />2<br />3<br />4<br />5<br />6<br />7<br /></div></td><td><div class="javascript codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap"><span style="color: #003366; font-weight: bold;">function</span> MySecondConstructor<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
MySecondConstructor.<span style="color: #660066;">prototype</span> <span style="color: #339933;">=</span> MyConstructor.<span style="color: #660066;">prototype</span><span style="color: #339933;">;</span><br />
<br />
<span style="color: #003366; font-weight: bold;">new</span> MyConstructor<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #000066; font-weight: bold;">instanceof</span> MyConstructor<span style="color: #339933;">;</span> <span style="color: #006600; font-style: italic;">// true – ok</span><br />
<span style="color: #003366; font-weight: bold;">new</span> MySecondConstructor<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #000066; font-weight: bold;">instanceof</span> MySecondConstructor<span style="color: #339933;">;</span> <span style="color: #006600; font-style: italic;">// true – we’ve expected that</span><br />
<span style="color: #003366; font-weight: bold;">new</span> MySecondConstructor<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #000066; font-weight: bold;">instanceof</span> MyConstructor<span style="color: #339933;">;</span> <span style="color: #006600; font-style: italic;">// also true</span><br />
<span style="color: #003366; font-weight: bold;">new</span> MyConstructor<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #000066; font-weight: bold;">instanceof</span> MySecondConstructor<span style="color: #339933;">;</span> <span style="color: #006600; font-style: italic;">// true again</span></div></td></tr></tbody></table></div>
<p>also, when changing the prototype property of a constructor to another object,</p>
<div class="codecolorer-container text twitlight" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:435px;"><table cellspacing="0" cellpadding="0"><tbody><tr><td style="padding:5px;text-align:center;color:#888888;background-color:#EEEEEE;border-right: 1px solid #9F9F9F;font: normal 12px/1.4em Monaco, Lucida Console, monospace;"><div>1<br /></div></td><td><div class="text codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap">instanceof</div></td></tr></tbody></table></div>
<p>yields a different result:</p>
<div class="codecolorer-container javascript twitlight" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:435px;"><table cellspacing="0" cellpadding="0"><tbody><tr><td style="padding:5px;text-align:center;color:#888888;background-color:#EEEEEE;border-right: 1px solid #9F9F9F;font: normal 12px/1.4em Monaco, Lucida Console, monospace;"><div>1<br />2<br />3<br />4<br />5<br /></div></td><td><div class="javascript codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap"><span style="color: #003366; font-weight: bold;">var</span> myInstance <span style="color: #339933;">=</span> <span style="color: #003366; font-weight: bold;">new</span> MyConstructor<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
myInstance <span style="color: #000066; font-weight: bold;">instanceof</span> MyConstructor<span style="color: #339933;">;</span> <span style="color: #006600; font-style: italic;">// true</span><br />
<br />
MyConstructor.<span style="color: #660066;">prototype</span> <span style="color: #339933;">=</span> <span style="color: #009900;">&#123;</span><span style="color: #009900;">&#125;</span><span style="color: #339933;">;</span><br />
myInstance <span style="color: #000066; font-weight: bold;">instanceof</span> MyConstructor<span style="color: #339933;">;</span> <span style="color: #006600; font-style: italic;">// false</span></div></td></tr></tbody></table></div>
<p>The explanation is simple – since <code class="codecolorer text mac-classic"><span class="text">MyConstructor.prototype</span></code> is not in the prototype chain of <code class="codecolorer text mac-classic"><span class="text">myInstance</span></code>, the operator yields <code class="codecolorer text mac-classic"><span class="text">false</span></code></p>
<h2>Object-Based inheritance</h2>
<p>The latest edition of ECMAScript – ECMAScript 5th Edition – gives us a powerful new tool to create objects with a specific prototype: <a href="https://developer.mozilla.org/en/JavaScript/Reference/Global_Objects/Object/create"><code class="codecolorer text mac-classic"><span class="text">Object.create()</span></code></a>. The method takes two parameters: The object to use as <em>[[Prototype]]</em> and an object containing <a href="https://developer.mozilla.org/en/JavaScript/Reference/Global_Objects/Object/defineProperty#Description">property descriptors</a> for the new object to create. Using <code class="codecolorer text mac-classic"><span class="text">Object.create</span></code> allows us to short-circuit object creation and avoid the usage of constructor functions.</p>
<p>Creating two objects sharing a common prototype is easy:</p>
<div class="codecolorer-container javascript twitlight" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:435px;"><table cellspacing="0" cellpadding="0"><tbody><tr><td style="padding:5px;text-align:center;color:#888888;background-color:#EEEEEE;border-right: 1px solid #9F9F9F;font: normal 12px/1.4em Monaco, Lucida Console, monospace;"><div>1<br />2<br />3<br />4<br />5<br />6<br />7<br />8<br />9<br /></div></td><td><div class="javascript codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap"><span style="color: #003366; font-weight: bold;">var</span> baseObject <span style="color: #339933;">=</span> <span style="color: #009900;">&#123;</span><br />
&nbsp; &nbsp; foo<span style="color: #339933;">:</span> <span style="color: #3366CC;">&quot;some property&quot;</span><span style="color: #339933;">,</span><br />
&nbsp; &nbsp; bar<span style="color: #339933;">:</span> <span style="color: #003366; font-weight: bold;">function</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp;<span style="color: #000066; font-weight: bold;">return</span> <span style="color: #3366CC;">&quot;some method&quot;</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp; <span style="color: #009900;">&#125;</span><br />
<span style="color: #009900;">&#125;</span><br />
<br />
<span style="color: #003366; font-weight: bold;">var</span> myObject <span style="color: #339933;">=</span> Object.<span style="color: #660066;">create</span><span style="color: #009900;">&#40;</span>baseObject<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
<span style="color: #003366; font-weight: bold;">var</span> mySecondObject <span style="color: #339933;">=</span> Object.<span style="color: #660066;">create</span><span style="color: #009900;">&#40;</span>baseObject<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span></div></td></tr></tbody></table></div>
<p>Building an inheritance chain is also possible, but might get a little bit tedious when using property descriptors to add new properties:</p>
<div class="codecolorer-container javascript twitlight" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:435px;height:300px;"><table cellspacing="0" cellpadding="0"><tbody><tr><td style="padding:5px;text-align:center;color:#888888;background-color:#EEEEEE;border-right: 1px solid #9F9F9F;font: normal 12px/1.4em Monaco, Lucida Console, monospace;"><div>1<br />2<br />3<br />4<br />5<br />6<br />7<br />8<br />9<br />10<br />11<br />12<br />13<br />14<br />15<br />16<br />17<br />18<br />19<br />20<br />21<br />22<br />23<br />24<br />25<br />26<br />27<br />28<br />29<br />30<br />31<br />32<br />33<br />34<br />35<br />36<br />37<br /></div></td><td><div class="javascript codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap"><span style="color: #003366; font-weight: bold;">var</span> Person <span style="color: #339933;">=</span> <span style="color: #009900;">&#123;</span><br />
&nbsp; &nbsp; firstName<span style="color: #339933;">:</span> <span style="color: #003366; font-weight: bold;">null</span><span style="color: #339933;">,</span> <span style="color: #006600; font-style: italic;">// the person’s first name</span><br />
&nbsp; &nbsp; lastName<span style="color: #339933;">:</span> <span style="color: #003366; font-weight: bold;">null</span> <span style="color: #006600; font-style: italic;">// the person’s last name</span><br />
<span style="color: #009900;">&#125;</span><span style="color: #339933;">;</span><br />
<br />
<span style="color: #006600; font-style: italic;">// “subclass” Person</span><br />
<span style="color: #003366; font-weight: bold;">var</span> Employee <span style="color: #339933;">=</span> Object.<span style="color: #660066;">create</span><span style="color: #009900;">&#40;</span>Person<span style="color: #339933;">,</span> <span style="color: #009900;">&#123;</span><br />
&nbsp; &nbsp; id<span style="color: #339933;">:</span> <span style="color: #009900;">&#123;</span> <span style="color: #006600; font-style: italic;">// the employees’s id</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; value<span style="color: #339933;">:</span> <span style="color: #003366; font-weight: bold;">null</span><span style="color: #339933;">,</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; enumerable<span style="color: #339933;">:</span> <span style="color: #003366; font-weight: bold;">true</span><span style="color: #339933;">,</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; configurable<span style="color: #339933;">:</span> <span style="color: #003366; font-weight: bold;">true</span><span style="color: #339933;">,</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; writable<span style="color: #339933;">:</span> <span style="color: #003366; font-weight: bold;">true</span><br />
&nbsp; &nbsp; <span style="color: #009900;">&#125;</span><br />
<span style="color: #009900;">&#125;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
<br />
<span style="color: #006600; font-style: italic;">// create an instance of Employee</span><br />
<span style="color: #003366; font-weight: bold;">var</span> david <span style="color: #339933;">=</span> Object.<span style="color: #660066;">create</span><span style="color: #009900;">&#40;</span>Employee<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
david.<span style="color: #660066;">firstName</span> <span style="color: #339933;">=</span> <span style="color: #3366CC;">&quot;David&quot;</span><span style="color: #339933;">;</span><br />
david.<span style="color: #660066;">lastName</span> <span style="color: #339933;">=</span> <span style="color: #3366CC;">&quot;Aurelio&quot;</span><span style="color: #339933;">;</span><br />
david.<span style="color: #660066;">id</span> <span style="color: #339933;">=</span> <span style="color: #CC0000;">1</span><span style="color: #339933;">;</span><br />
<br />
<span style="color: #006600; font-style: italic;">// “subclass” Employee</span><br />
<span style="color: #003366; font-weight: bold;">var</span> Manager <span style="color: #339933;">=</span> Object.<span style="color: #660066;">create</span><span style="color: #009900;">&#40;</span>Employee<span style="color: #339933;">,</span> <span style="color: #009900;">&#123;</span><br />
&nbsp; &nbsp; department<span style="color: #339933;">:</span> <span style="color: #009900;">&#123;</span> <span style="color: #006600; font-style: italic;">// the manager’s department</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; value<span style="color: #339933;">:</span> <span style="color: #003366; font-weight: bold;">null</span><span style="color: #339933;">,</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; enumerable<span style="color: #339933;">:</span> <span style="color: #003366; font-weight: bold;">true</span><span style="color: #339933;">,</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; configurable<span style="color: #339933;">:</span> <span style="color: #003366; font-weight: bold;">true</span><span style="color: #339933;">,</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; writable<span style="color: #339933;">:</span> <span style="color: #003366; font-weight: bold;">true</span><br />
&nbsp; &nbsp; <span style="color: #009900;">&#125;</span><br />
<span style="color: #009900;">&#125;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
<br />
<span style="color: #006600; font-style: italic;">// create an instance of Manager</span><br />
<span style="color: #003366; font-weight: bold;">var</span> tobias <span style="color: #339933;">=</span> Object.<span style="color: #660066;">create</span><span style="color: #009900;">&#40;</span>Manager<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
tobias.<span style="color: #660066;">firstName</span> <span style="color: #339933;">=</span> <span style="color: #3366CC;">&quot;Tobias&quot;</span><span style="color: #339933;">;</span><br />
tobias.<span style="color: #660066;">lastName</span> <span style="color: #339933;">=</span> <span style="color: #3366CC;">&quot;von Klipstein&quot;</span><span style="color: #339933;">;</span><br />
tobias.<span style="color: #660066;">id</span> <span style="color: #339933;">=</span> <span style="color: #CC0000;">0</span><span style="color: #339933;">;</span><br />
tobias.<span style="color: #660066;">department</span> <span style="color: #339933;">=</span> <span style="color: #3366CC;">&quot;Infrastructure&quot;</span><span style="color: #339933;">;</span></div></td></tr></tbody></table></div>
<p>You may have noticed two things in this example: </p>
<ol>
<li>Creating “instances” of an object or “subclassing” it are basically the same process: We create a new object with its prototype set to the base object. Personally I like this fact; it just feels natural in an prototype-based inheritance model like the one in ECMAScript/JavaScript.</li>
<li>Not using a constructor function shows how useful such an initialization function would be for instance creation.</li>
</ol>
<h2>Inheritance System Based On <code class="codecolorer text mac-classic"><span class="text">Object.create()</span></code></h2>
<p>For me, using <code class="codecolorer text mac-classic"><span class="text">Object.create()</span></code> felt incredibly natural compared to fiddling around with constructor functions and the prototype object attached to them – that doesn’t mean having a constructor is bad; they are really useful for setting up an instance. But the coupling of constructor, constructor.prototype, and instances is too loose in my eyes, and makes it unnecessarily hard to understand inheritance in JavaScript.</p>
<p>What I don’t like is the verbosity of the descriptor syntax. Granted, it gives us a lot of possibilities, but for most cases it is too cumbersome. In addition, I wanted to have a constructor when creating objects that are supposed to be used like a class instance in a class-oriented programming language.</p>
<p>I came up with a light-weight inheritance system that provides separate methods for the two cases where I use <code class="codecolorer text mac-classic"><span class="text">Object.create()</span></code>, object extension and instantiation.</p>
<p>Have a look at <a href="https://gist.github.com/838778">this gist</a>.<br />
[gist id=838778 file=base.js]</p>
<p>You can use <code class="codecolorer text mac-classic"><span class="text">BaseObject</span></code> as base for your inheritance/object tree, or simply copy all of its methods onto your own base object. Both instantiation and extension are done through Object.create, but work pretty differently. Let me go into detail and explain the four methods present on <code class="codecolorer text mac-classic"><span class="text">BaseObject</span></code>.</p>
<ul>
<li>The <code class="codecolorer text mac-classic"><span class="text">_construct</span></code> method acts like a regular constructor: It is executed in the context of a newly created instance right after creation in the context of that instance (<code class="codecolorer text mac-classic"><span class="text">this</span></code> works as you would expect). You may overwrite this constructor as often as necessary. You can access parent constructors through the <code class="codecolorer text mac-classic"><span class="text">_super</span></code> convenience method (see below for details). The dangling dash suggests you should regard this method as private. Usually, you never need to call it manually.
</li>
<li><code class="codecolorer text mac-classic"><span class="text">create</span></code> is used for instantiation. Whenever you need a new “instance” of a base object (that you might use like a class), you’d simply call “create()” on it: <code class="codecolorer text mac-classic"><span class="text">var myInstance = MyBase.create(arg1, arg2);</span></code>. The instance is created, and its “_construct” method is called with all arguments passed to “create()”. Eventually, the instance is returned. Normally, you might not need to overwrite the base create method, but you may do so for complete control over the instance creation process if needed.</li>
<li>Use the <code class="codecolorer text mac-classic"><span class="text">extend</span></code> method to create “subclasses” – it differs from creating instances in two aspects: the constructor isn’t called, and you can easily create additional properties on the new object: The first parameter is a simple object. All of its properties are copied onto the new object (you may also pass <code class="codecolorer text mac-classic"><span class="text">null</span></code>). The second (optional) parameter is an object containing <em>property descriptors</em> for properties to be created on the new object. Check the example below for details. This system allows it to use simple properties without taking the power of property descriptors away from you.</li>
<li><code class="codecolorer text mac-classic"><span class="text">_super</span></code> is a convenience method to call overwritten parent methods in the context of the current object. It works similar to python’s <em>super</em> and takes three arguments: A reference to the object that overwrites a method (<strong>not</strong> the name of the parent object), the name of the overwritten method (as string), and an array of parameters/an arguments object. Just take a look at the example to get the idea how it works.</li>
</ul>
<p>Generally, you should not overwrite the “extend()” and “_super()” methods. “create()” may be overwritten for more control over the creation process of instances. “_construct()” can be defined as often as needed.</p>
<p>This example is a rewrite of the Person/Employee/Manager example using BaseObject as parent of Person:</p>
<div class="codecolorer-container javascript twitlight" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:435px;height:300px;"><table cellspacing="0" cellpadding="0"><tbody><tr><td style="padding:5px;text-align:center;color:#888888;background-color:#EEEEEE;border-right: 1px solid #9F9F9F;font: normal 12px/1.4em Monaco, Lucida Console, monospace;"><div>1<br />2<br />3<br />4<br />5<br />6<br />7<br />8<br />9<br />10<br />11<br />12<br />13<br />14<br />15<br />16<br />17<br />18<br />19<br />20<br />21<br />22<br />23<br />24<br />25<br />26<br />27<br />28<br />29<br />30<br />31<br />32<br />33<br />34<br />35<br />36<br />37<br />38<br />39<br />40<br />41<br />42<br />43<br />44<br />45<br />46<br />47<br />48<br />49<br />50<br />51<br />52<br />53<br />54<br />55<br />56<br />57<br />58<br />59<br />60<br />61<br /></div></td><td><div class="javascript codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap"><span style="color: #003366; font-weight: bold;">var</span> Person <span style="color: #339933;">=</span> BaseObject.<span style="color: #660066;">extend</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#123;</span> <span style="color: #006600; font-style: italic;">// using only properties that are copied over</span><br />
&nbsp; &nbsp; firstName<span style="color: #339933;">:</span> <span style="color: #003366; font-weight: bold;">null</span><span style="color: #339933;">,</span><br />
&nbsp; &nbsp; lastName<span style="color: #339933;">:</span> <span style="color: #003366; font-weight: bold;">null</span><span style="color: #339933;">,</span><br />
&nbsp; &nbsp; _construct<span style="color: #339933;">:</span> <span style="color: #003366; font-weight: bold;">function</span><span style="color: #009900;">&#40;</span>firstName<span style="color: #339933;">,</span> lastName<span style="color: #009900;">&#41;</span><span style="color: #009900;">&#123;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #000066; font-weight: bold;">this</span>.<span style="color: #660066;">firstName</span> <span style="color: #339933;">=</span> firstName<span style="color: #339933;">;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #000066; font-weight: bold;">this</span>.<span style="color: #660066;">lastName</span> <span style="color: #339933;">=</span> lastName<span style="color: #339933;">;</span><br />
&nbsp; &nbsp; <span style="color: #009900;">&#125;</span><span style="color: #339933;">,</span><br />
&nbsp; &nbsp; toString<span style="color: #339933;">:</span> <span style="color: #003366; font-weight: bold;">function</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#123;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #000066; font-weight: bold;">return</span> <span style="color: #000066; font-weight: bold;">this</span>.<span style="color: #660066;">firstName</span> <span style="color: #339933;">+</span> <span style="color: #3366CC;">&quot; &quot;</span> <span style="color: #339933;">+</span> <span style="color: #000066; font-weight: bold;">this</span>.<span style="color: #660066;">lastName</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp; <span style="color: #009900;">&#125;</span><br />
<span style="color: #009900;">&#125;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
<br />
<span style="color: #003366; font-weight: bold;">var</span> Employee <span style="color: #339933;">=</span> Person.<span style="color: #660066;">extend</span><span style="color: #009900;">&#40;</span><span style="color: #003366; font-weight: bold;">null</span><span style="color: #339933;">,</span> <span style="color: #009900;">&#123;</span> <span style="color: #006600; font-style: italic;">// using only property descriptors</span><br />
&nbsp; &nbsp; id<span style="color: #339933;">:</span> <span style="color: #009900;">&#123;</span> <span style="color: #006600; font-style: italic;">// the employees’s id</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; value<span style="color: #339933;">:</span> <span style="color: #003366; font-weight: bold;">null</span><span style="color: #339933;">,</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; enumerable<span style="color: #339933;">:</span> <span style="color: #003366; font-weight: bold;">true</span><span style="color: #339933;">,</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; configurable<span style="color: #339933;">:</span> <span style="color: #003366; font-weight: bold;">false</span><span style="color: #339933;">,</span> <span style="color: #006600; font-style: italic;">// can’t be deleted</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; writable<span style="color: #339933;">:</span> <span style="color: #003366; font-weight: bold;">true</span><br />
&nbsp; &nbsp; <span style="color: #009900;">&#125;</span><span style="color: #339933;">,</span><br />
&nbsp; &nbsp; _construct<span style="color: #339933;">:</span> <span style="color: #009900;">&#123;</span> <span style="color: #006600; font-style: italic;">// has only a value, is not enumerable, writable, or deletable </span><br />
&nbsp; &nbsp; &nbsp; &nbsp; value<span style="color: #339933;">:</span> <span style="color: #003366; font-weight: bold;">function</span><span style="color: #009900;">&#40;</span>firstName<span style="color: #339933;">,</span> lastName<span style="color: #339933;">,</span> id<span style="color: #009900;">&#41;</span><span style="color: #009900;">&#123;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #000066; font-weight: bold;">this</span>._super<span style="color: #009900;">&#40;</span>Employee<span style="color: #339933;">,</span> arguments<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span> <span style="color: #006600; font-style: italic;">// calls the parent constructor with all received arguments</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #000066; font-weight: bold;">this</span>.<span style="color: #660066;">id</span> <span style="color: #339933;">=</span> id<span style="color: #339933;">;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #009900;">&#125;</span><br />
&nbsp; &nbsp; <span style="color: #009900;">&#125;</span><span style="color: #339933;">,</span><br />
<br />
&nbsp; &nbsp; toString<span style="color: #339933;">:</span> <span style="color: #009900;">&#123;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; value<span style="color: #339933;">:</span> <span style="color: #003366; font-weight: bold;">function</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#123;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #000066; font-weight: bold;">return</span> <span style="color: #3366CC;">&quot;[id: &quot;</span> <span style="color: #339933;">+</span> <span style="color: #000066; font-weight: bold;">this</span>.<span style="color: #660066;">id</span> <span style="color: #339933;">+</span> <span style="color: #3366CC;">&quot; / &quot;</span> <span style="color: #339933;">+</span> <br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #000066; font-weight: bold;">this</span>._super<span style="color: #009900;">&#40;</span>Employee<span style="color: #339933;">,</span> <span style="color: #3366CC;">&quot;toString&quot;</span><span style="color: #009900;">&#41;</span> <span style="color: #339933;">+</span> <span style="color: #3366CC;">&quot;]&quot;</span><span style="color: #339933;">;</span> <span style="color: #006600; font-style: italic;">// calls parent &quot;toString&quot;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #009900;">&#125;</span><br />
&nbsp; &nbsp; <span style="color: #009900;">&#125;</span><br />
<span style="color: #009900;">&#125;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
<br />
<span style="color: #006600; font-style: italic;">// create an instance of Employee</span><br />
<span style="color: #003366; font-weight: bold;">var</span> david <span style="color: #339933;">=</span> Employee.<span style="color: #660066;">create</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">&quot;David&quot;</span><span style="color: #339933;">,</span> <span style="color: #3366CC;">&quot;Aurelio&quot;</span><span style="color: #339933;">,</span> <span style="color: #CC0000;">1</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
david.<span style="color: #660066;">toString</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span> <span style="color: #006600; font-style: italic;">// &quot;[id: 1 / David Aurelio]&quot;</span><br />
<br />
<span style="color: #003366; font-weight: bold;">var</span> Manager <span style="color: #339933;">=</span> Employee.<span style="color: #660066;">extend</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#123;</span><br />
&nbsp; &nbsp; <span style="color: #006600; font-style: italic;">// simple properties</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; _construct<span style="color: #339933;">:</span> <span style="color: #003366; font-weight: bold;">function</span><span style="color: #009900;">&#40;</span>firstName<span style="color: #339933;">,</span> lastName<span style="color: #339933;">,</span> department<span style="color: #339933;">,</span> id<span style="color: #009900;">&#41;</span><span style="color: #009900;">&#123;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #000066; font-weight: bold;">this</span>._super<span style="color: #009900;">&#40;</span>Manager<span style="color: #339933;">,</span> <span style="color: #009900;">&#91;</span>firstName<span style="color: #339933;">,</span> lastName<span style="color: #339933;">,</span> id<span style="color: #009900;">&#93;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span> <span style="color: #006600; font-style: italic;">// calls the parent constructor with three parameters</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #000066; font-weight: bold;">this</span>.<span style="color: #660066;">department</span> <span style="color: #339933;">=</span> department<span style="color: #339933;">;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #009900;">&#125;</span><span style="color: #339933;">,</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; toString<span style="color: #339933;">:</span> <span style="color: #003366; font-weight: bold;">function</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#123;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #000066; font-weight: bold;">return</span> <span style="color: #000066; font-weight: bold;">this</span>._super<span style="color: #009900;">&#40;</span>Manager<span style="color: #339933;">,</span> <span style="color: #3366CC;">&quot;toString&quot;</span><span style="color: #009900;">&#41;</span> <span style="color: #339933;">+</span> <span style="color: #006600; font-style: italic;">// calls parent &quot;toString&quot; method without arguments</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #3366CC;">&quot; (Manager)&quot;</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #009900;">&#125;</span><br />
&nbsp; &nbsp; <span style="color: #009900;">&#125;</span><span style="color: #339933;">,</span><br />
&nbsp; &nbsp; <span style="color: #009900;">&#123;</span> <span style="color: #006600; font-style: italic;">// property descriptors</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; department<span style="color: #339933;">:</span> <span style="color: #009900;">&#123;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; value<span style="color: #339933;">:</span> <span style="color: #003366; font-weight: bold;">null</span><span style="color: #339933;">,</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; enumerable<span style="color: #339933;">:</span> <span style="color: #003366; font-weight: bold;">true</span><span style="color: #339933;">,</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; configurable<span style="color: #339933;">:</span> <span style="color: #003366; font-weight: bold;">false</span><span style="color: #339933;">,</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; writable<span style="color: #339933;">:</span> <span style="color: #003366; font-weight: bold;">true</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #009900;">&#125;</span><br />
<span style="color: #009900;">&#125;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
<br />
<span style="color: #006600; font-style: italic;">// create an instance of Manager</span><br />
<span style="color: #003366; font-weight: bold;">var</span> tobias <span style="color: #339933;">=</span> Manager.<span style="color: #660066;">create</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">&quot;Tobias&quot;</span><span style="color: #339933;">,</span> &nbsp;<span style="color: #3366CC;">&quot;von Klipstein&quot;</span><span style="color: #339933;">,</span> <span style="color: #3366CC;">&quot;Infrastructure&quot;</span><span style="color: #339933;">,</span> <span style="color: #CC0000;">0</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
tobias.<span style="color: #660066;">toString</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span> <span style="color: #006600; font-style: italic;">// &quot;[id: 0 / Tobias von Klipstein] (Manager)&quot;</span></div></td></tr></tbody></table></div>
<h2>What’s Nice</h2>
<p>What I really, really like is the fact everything feels just natural – we’re dealing with a set of objects, not with a constructor/prototype construction (which might be one of the reasons inheritance in JS is so difficult to understand when coming from a class-based language). For example: Where would you define a “static” member in the classic approach? On the prototype? It’s in the inheritance chain and can be overwritten by “subclasses”, but might be hard to reach from other places (think of <code class="codecolorer text mac-classic"><span class="text">Shape.prototype.SQUARE</span></code>. Not cool. On the constructor? That&#8217;s nicer for access (<code class="codecolorer text mac-classic"><span class="text">Shape.SQUARE</span></code>), but places the property outside of the inheritance chain, thus making it impossible to overwrite it. In addition, I feel that using <code class="codecolorer text mac-classic"><span class="text">this.SQUARE</span></code> from methods feels more natural.</p>
<p>Using a hierarchy of objects solves all that little inconsistencies. Yum!</p>
<p>Extending incredibly flexible: since <code class="codecolorer text mac-classic"><span class="text">extend()</span></code> is present on every object created by <code class="codecolorer text mac-classic"><span class="text">extend()/create()</span></code>, you can extend everything with new properties.</p>
<h2>What’s Not So Nice</h2>
<p>Since the objects we use in a “class” role are objects, not functions, we can’t use the <code class="codecolorer text mac-classic"><span class="text">new</span></code> operator. After years of using it, I really have to adopt to the use of <code class="codecolorer text mac-classic"><span class="text">create()</span></code> instead.</p>
<p>Also, <code class="codecolorer text mac-classic"><span class="text">instanceof</span></code> only works with constructor functions. Use the <a href="https://developer.mozilla.org/en/JavaScript/Reference/Global_Objects/Object/isPrototypeOf">.isPrototypeOf()</a> method of ECMAScript 5 instead.</p>
<p>Compatibility is still an issue. The browser/runtime must support the new ECMAScript 5 features Object.create(), Object.getOwnPropertyNames(), Object.getOwnPropertyDescriptor(), and Object.getPrototypeOf(). Browsers supporting the required functionality include: Firefox 4, Safari 5, Chrome 9,<br />
Internet Explorer 9 (untested). Opera has no support for these features yet.</p>
]]></content:encoded>
			<wfw:commentRss>http://uxebu.com/blog/2011/02/23/object-based-inheritance-for-ecmascript-5/feed/</wfw:commentRss>
		<slash:comments>5</slash:comments>
		</item>
		<item>
		<title>Explaining EmbedJS</title>
		<link>http://uxebu.com/blog/2011/01/31/explaining-embedjs/</link>
		<comments>http://uxebu.com/blog/2011/01/31/explaining-embedjs/#comments</comments>
		<pubDate>Mon, 31 Jan 2011 14:02:28 +0000</pubDate>
		<dc:creator>Jens Arps</dc:creator>
				<category><![CDATA[development tools]]></category>
		<category><![CDATA[dojo]]></category>
		<category><![CDATA[frontend engineering]]></category>
		<category><![CDATA[javascript]]></category>
		<category><![CDATA[mobile]]></category>
		<category><![CDATA[Uncategorized]]></category>
		<category><![CDATA[uxebu]]></category>
		<category><![CDATA[development]]></category>
		<category><![CDATA[embedjs]]></category>
		<category><![CDATA[memory usage]]></category>
		<category><![CDATA[optimize]]></category>
		<category><![CDATA[performance]]></category>

		<guid isPermaLink="false">http://uxebu.com/blog/?p=1366</guid>
		<description><![CDATA[Last week, we tagged the current state of EmbedJS 0.1. This is a large step for us, and something we have been waiting for and wanting to do for a long time. And with doing so, the need arises to answer a lot of questions – and we better start sooner than later. So, here [...]]]></description>
			<content:encoded><![CDATA[<p>Last week, we tagged the current state of EmbedJS 0.1. This is a large step for us, and something we have been waiting for and wanting to do for a long time. And with doing so, the need arises to answer a lot of questions – and we better start sooner than later. So, here it is, the first part of „Explaining EmbedJS“.</p>
<h2>What is it?</h2>
<p>It‘s a JavaScript Toolkit, especially aimed at embedded devices. Embedded devices, that is phones, tablet devices, TVs, car dashboards and the like. Devices that are capable of running JavaScript. We are sure that this list is going to grow over time – and will hopefully one day also include devices like your fridge. EmbedJS is based on the Dojo Toolkit.<br />
<span id="more-1366"></span></p>
<h2>Why?</h2>
<p>Because the toolkits and frameworks that exist focus on desktop browsers and therefore have a different approach – an approach we didn‘t find that suitable for embedded/mobile development. And the JS frameworks that target mobile devices are flawed, too: either they target only a specific device or platform, or they provide far more than we do need and want.</p>
<p>What we wanted was a toolkit that offered a unified API across different platforms, but with the best implementation that was available for each platform. And we didn‘t want to ship code that implemented a certain feature for, say, an old Blackberry device to an iPhone. Plus, we wanted that API to be small – enough to assist us in our daily work, but not that bloated that we would ship code to a phone that we wouldn‘t need in most of our projects.</p>
<h2>The Dojo Toolkit</h2>
<p>We at uxebu have a strong affinity to the <a href="http://dojotoolkit.org/" target="_blank">Dojo Toolkit</a> – most of us are committers – for a good reason: we believe that it has done many things right, and that it has an excellent code quality. Thus, finding a starting point for our venture to what was later to be EmbedJS was pretty easy: It was the very core of the Dojo Toolkit, dojo._base.</p>
<p>We examined the code and split it into its smallest parts (features, how we call them in EmbedJS). After having the features all nicely separated, we could look for the best implementation for each of the platforms we wanted to support, and put it into a separate file. Then we optimized our code, tweaking here and there, but also using the code from dojo._base as it was, because it already was the best implementation.</p>
<p>And we needed to find means to put all these tiny parts together again, respecting any given dependecies, to create a file that then contained all the feature‘s implementations for a given platform – so the EmbedJS Tools were born, including our highly flexible build system.</p>
<h2>API</h2>
<p>EmbedJS‘ API is a small subset on Dojo‘s API. It provides around 70 methods, from the fields language enhancement (like Array manipulation or JSON), OO (like classes and inheritance), transport (like JSONP or XHR), event system (like connecting to DOM events or methods), DOM manipulation (like style or node creation) and misc things like Promises or pub/sub.</p>
<p>We kept the method signatures intact so that you can import your existing knowledge from working with the Dojo Toolkit to EmbedJS. There are only a few minor changes where we<br />
a) found a new name highly useful, e.g <code class="codecolorer text mac-classic"><span class="text">dojo.io.script.get()</span></code> is <code class="codecolorer text mac-classic"><span class="text">embed.jsonp()</span></code><br />
b) we needed to strip given functionality for the sake of code size.</p>
<p>All methods in EmbedJS are available under the embed namespace as well as under the dojo namespace. I.e. to call the connect method you can use <code class="codecolorer text mac-classic"><span class="text">dojo.connect()</span></code> and <code class="codecolorer text mac-classic"><span class="text">embed.connect()</span></code>.</p>
<p>The full API documentations is <a href="http://embedjs.org/apidocs/dools/app/apidoc/embedjs/" target="_blank">here</a>, though it‘s undergoing some updates right now.</p>
<h2>Summary</h2>
<p>I didn‘t want to dig too deep in the first article on EmbedJS, but I guess you got the point: EmbedJS is a highly optimized Dojo core, and offers you a specialized, optimal build for a given platform. To explain how the build system works, how we manage dependencies, how we separate our features and our implementations and how one can create a project-oriented custom build will need another blog post – but if you are too curious, you are invited to take a look at <a href="https://github.com/uxebu/embedjs" target="_blank">the source code at github</a>; a lot is self-explenatory there. A further source of information is the <a href="http://uxebu.github.com/embedjs/" target="_blank">EmbedJS project page</a>, as well as the <a href="https://github.com/uxebu/embedjs-tools" target="_blank">EmbedJS Tools source</a>.</p>
]]></content:encoded>
			<wfw:commentRss>http://uxebu.com/blog/2011/01/31/explaining-embedjs/feed/</wfw:commentRss>
		<slash:comments>5</slash:comments>
		</item>
		<item>
		<title>indexedDB Updates &#8211; FF4.09b</title>
		<link>http://uxebu.com/blog/2011/01/17/indexeddb-updates-ff4-09b/</link>
		<comments>http://uxebu.com/blog/2011/01/17/indexeddb-updates-ff4-09b/#comments</comments>
		<pubDate>Mon, 17 Jan 2011 15:52:26 +0000</pubDate>
		<dc:creator>Jens Arps</dc:creator>
				<category><![CDATA[frontend engineering]]></category>
		<category><![CDATA[javascript]]></category>
		<category><![CDATA[Uncategorized]]></category>
		<category><![CDATA[firefox]]></category>
		<category><![CDATA[html5]]></category>
		<category><![CDATA[IDB]]></category>
		<category><![CDATA[indexedDB]]></category>
		<category><![CDATA[offline]]></category>
		<category><![CDATA[storage]]></category>

		<guid isPermaLink="false">http://uxebu.com/blog/?p=1344</guid>
		<description><![CDATA[IndexedDB is all the buzz right now, but it is pretty hard to find information about it&#8217;s implementations. In addition to that, the impls change on a weekly basis. So I figured it would be nice to summarize every now and then what has been happening around IDB in the last time. And as Mozilla [...]]]></description>
			<content:encoded><![CDATA[<p>IndexedDB is all the buzz right now, but it is pretty hard to find information about it&#8217;s implementations. In addition to that, the impls change on a weekly basis. So I figured it would be nice to summarize every now and then what has been happening around IDB in the last time.</p>
<p>And as Mozilla released a new Firefox 4 beta these days, and introduces changes to it&#8217;s IDB impl, let&#8217;s start the series with a brief summary of the most important of these changes.<br />
<span id="more-1344"></span></p>
<h2>Changes in 4.09b</h2>
<ul>
<li>The indexedDB object has been renamed: <code class="codecolorer text mac-classic"><span class="text">moz_indexedDB</span></code> is now <code class="codecolorer text mac-classic"><span class="text">mozIndexedDB</span></code>.</li>
<li>When opening a database, the event handed over to the onsuccess callback no more has a property called &#8220;result&#8221;, where a reference to the databasse used to be; it is now available at <code class="codecolorer text mac-classic"><span class="text">event.target.result</span></code> (event.target contains the IDBRequest).</li>
<li>This applies to many (all?) cases, where you&#8217;d expect the result of a request to live in event.result, where it now is in event.target.result.</li>
<li>When creating an object store, the second argument, <code class="codecolorer text mac-classic"><span class="text">keyPath</span></code>, now has to be passed as a property inside of an object: <code class="codecolorer text mac-classic"><span class="text">{keyPath: &quot;keyPathName&quot;}</span></code></li>
<li>When creating an objectStore, the third argument, <code class="codecolorer text mac-classic"><span class="text">autoIncrement</span></code>, is dropped and now has to be part of the second argument: <code class="codecolorer text mac-classic"><span class="text">{autoIncrement: true}</span></code></li>
<li>Methods that read from an objectStore now only seem to work inside of a transaction; before, it was allowed to call methods like get directly.</li>
<li>keyRange support seems to have increased. However, I&#8217;m not really interested (right now) in keyRanges and queries, so I did not play around with these.</li>
</ul>
<p>Note that the changes for the <code class="codecolorer text mac-classic"><span class="text">createObjectStore</span></code> arguments are against the spec &#8211; the 4.08b was compliant, the 4.09b is no more. But, on the other hand, it is now closer to the Chrome impl, which also expects an object as second argument (but has no support for autoIncrement).</p>
<h2>What it does *not* have (yet)</h2>
<p>The <a href="http://www.w3.org/TR/IndexedDB/#sync-database" target="_blank">sync API</a>. Alas. To me, the snychronous implementation is exactly what makes indexedDB that attractive – I&#8217;m not a fan of databases and I don&#8217;t like to start transactions, iterate over resultSets and the like… I want a store with easy access. Seriously, in most real life scenarios, an async approach won&#8217;t get you anywhere but displaying a waiting message to your user, because you can&#8217;t go on until you have the data from the store.</p>
<h2>More info?</h2>
<p>If you want to stay in touch with what happens around the indexedDB impl, the best place to go ist the Mozilla bugtracker. Um, yeah, reading browser source code can be no fun at times, but it gets you as close as you can get. Or, check what happens in the test files.</p>
<h2>Need working code?</h2>
<p>The *only* place to get reliable, working code is the test files in the repository. Seriously, there&#8217;s so much code out there claiming to work, but it simply doesn&#8217;t. And even if it did one day, it might break the other day, after changes have been made to the impl. And there will be *lots* of changes in the future.</p>
<h2>Where are these test files?</h2>
<p>In the repo. To see them online, go here: <a href="http://hg.mozilla.org/mozilla-central/file/" target="_blank">http://hg.mozilla.org/mozilla-central/file/</a> and then navigate to dom/indexedDB/test.</p>
<h2>Something else?</h2>
<p>Nope, not really. There don&#8217;t seem to be any *major* changes for 4.09b – I hope you still found this short summary useful.</p>
<p>I hope to continue this series about IDB and post about different IDB topics every now and then  – so if you want to know more about indexedDB in general, or something specific, just drop a line in the comments!</p>
<h3>Further reading</h3>
<ul>
<li>The spec: <a href="http://www.w3.org/TR/IndexedDB/" target="_blank">http://www.w3.org/TR/IndexedDB/</a></li>
<li>The MDC article: <a href="https://developer.mozilla.org/en/IndexedDB" target="_blank">https://developer.mozilla.org/en/IndexedDB</a></li>
</ul>
]]></content:encoded>
			<wfw:commentRss>http://uxebu.com/blog/2011/01/17/indexeddb-updates-ff4-09b/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>BarCamp Munich 2010 &#8211; seen through JavaScript and mobile eyes</title>
		<link>http://uxebu.com/blog/2010/10/16/barcamp-munich-2010-seen-through-javascript-and-mobile-eyes/</link>
		<comments>http://uxebu.com/blog/2010/10/16/barcamp-munich-2010-seen-through-javascript-and-mobile-eyes/#comments</comments>
		<pubDate>Sat, 16 Oct 2010 15:47:05 +0000</pubDate>
		<dc:creator>wolfram</dc:creator>
				<category><![CDATA[business]]></category>
		<category><![CDATA[event]]></category>
		<category><![CDATA[html5apps]]></category>
		<category><![CDATA[javascript]]></category>
		<category><![CDATA[mobile]]></category>
		<category><![CDATA[uxebu]]></category>
		<category><![CDATA[w3c widgets]]></category>
		<category><![CDATA[barcamp]]></category>
		<category><![CDATA[conference]]></category>

		<guid isPermaLink="false">http://uxebu.com/blog/?p=1281</guid>
		<description><![CDATA[First and foremost big thanks to the organizers and sponsors of this awesome BarCamp. It almost seems as if BarCamps have already passed their tipping point, but still every time I discover new and interesting things, I meet people who attend for the first time and are excited about it, which makes it really awesome [...]]]></description>
			<content:encoded><![CDATA[<p>First and foremost big thanks to the organizers and sponsors of this awesome <a href="http://barcampmunich.mixxt.de/">BarCamp</a>. It almost seems as if BarCamps have already passed their tipping point, but still every time I discover new and interesting things, I meet people who attend for the first time and are excited about it, which makes it really awesome and fun. Maybe the tipping point had been reached but I guess the BarCamp is just on it&#8217;s way to become an integral part of the web culture and establishes itself as a standard event type besides the normal (mostly commercial) conferences. And as I always like to point out the people at BarCamps are those that want to go there, not those that have to be there (because their company had sent them or alike). We enjoyed it and have hopefully also brought value to it &#8211; that is what a BarCamp is all about: participate.<br />
Thank you!<br />
And let&#8217;s move on, which I do right this moment, sitting on the train going to the next (commercial) event the <a href="http://webtechcon.de">WebTechCon in Mainz</a> to bring the cross platform mobile web to it :).<br />
<span id="more-1281"></span></p>
<h2>&#8220;Modern Company&#8221; session &#8211; uxebu as the example</h2>
<p>But before I dive into the tech sessions I would like to mention that I really felt the need to hold a session which I had called &#8220;Modern Company&#8221; where I tried to show our philosophy at uxebu, how we work together, how we use the modern media, how it all came about and why we do it this way. First I didn&#8217;t know where to start. Then I started to talk about when and how the idea for uxebu came about, that we <a href="">decided online to found it</a> and really met some months later. Thanks to our open source background and our history we wanted to continue the same spirit inside the company, I tried to show how and based on what facts we grow our team (the word &#8220;hiring&#8221; seems such a one-sided process). I realize now that I forgot a lot of the good stuff, but maybe this is the chance to make this become a separate talk to hold at other events.<br />
I really loved the discussion and the questions that it had started, <a href="http://titanpad.com/izRqsSTAoT">notes had been taken</a>, a lot of critical questions came up and had been answered by <a href="http://twitter.com/tobeytailor">Tobias</a> and me. One interesting mentioning came up about <a href="http://www.faz.net/s/Rub8EC3C0841F934F3ABA0703761B67E9FA/Doc~E80113EC3EB3F48A5A14211D237BF109D~ATpl~Ecommon~Scontent.html">an article which mentioned a company that pays the same salary to all employees</a>. Thank you for all this input, I really enjoyed it and will definitely talk about this more and will prepare some more info next time.</p>
<h2>JavaScript &#8211; a topic of big interest</h2>
<p>We had founded uxebu purely for doing JavaScript consulting and pushing the limits and it is just really exciting to see how much interest this language is gaining in the last years. Not only <a href="http://jsconf.com">jsconf</a> the mother of all JavaScript events underlines this but also a lot of other conferences and events like <a href="http://2010.full-frontal.org/">fullfrontal</a>, <a href="http://fronteers.nl/">fronteers</a>, <a href="http://js1k.com/home">js1k</a>, <a href="http://10k.aneventapart.com/">10k Apart</a>, <a href="http://events.jquery.org/2010/boston/">jQueryConf</a> and so on which <a href="http://twitter.com/netzzwerg">Béla Varga</a> listed in his &#8220;<a href="http://www.slideshare.net/netzzwerg/javascript-rock-and-roll">JavaScript Rock&#8217;n Roll</a>&#8221; session.<br />
The &#8220;<a href="http://nodejs.org/">node.js</a>&#8221; session and &#8220;JavaScript Rock&#8217;n Roll&#8221; (by <a href="http://twitter.com/netzzwerg">Béla (@netzzwerg)</a> and <a href="http://twitter.com/tobeytailor">Tobias Schneider (@tobeytailor)</a>) made it very obvious that JavaScript is a hot topic, the BarCamp was not necessarily overcrowded with programmers, but still those two sessions had been very well filled and the discussions had been really fun. The conclusion of the &#8220;node.js&#8221; session that Tobias drew and evangelized was &#8220;node.js is currently very hyped and just not yet ready for production, so use at your own risk, it&#8217;s not even alpha yet&#8221;. Everybody also agreed that node.js is not the new saviour and a replacement for Apache, it&#8217;s another tool you should add to your tool belt, use it when it comes in handy and be sure to know how and when to use it. Final conclusion was, as usual, if you should use it depends on what you want to do :).<br />
Béla opened up the &#8220;JavaScript Rock&#8217;n Roll&#8221; session by giving a very good overview of what toolkits exist out there, who created them and what they are good for. He very well layed out some of the advantages and disadvantages, the rough corners and what is hard and what easy to learn. Shortly the discussion about the right JavaScript IDE came up but as usual there is no one-fits-all solution, choose yourself! Everybody was very carefully listening when <a href="http://twitter.com/tobeytailor">@tobeytailor</a> dove deep into the details of <a href="http://www.ecma-international.org/publications/standards/Ecma-262.htm">ECMAScript</a>, JavaScript, the differences, the latest specification, the changes, prototypes, <a href="https://developer.mozilla.org/en/JavaScript/Reference/Global_Objects/Object/proto">__proto__</a> and so on. Other interesting topics in those sessions had been <a href="http://promotejs.com/">promotejs</a>, <a href="http://github.com/madrobby/vapor.js">vapor.js</a>, <a href="http://www.slideshare.net/jensarps/the-hitchhikers-guide-to-client-side-persistent-storage">details about storage mechanisms</a>, <a href="http://jquerymobile.com/">jQueryMobile</a>, <a href="http://embedjs.org/">embedJS</a>, <a href="http://apparat.io">apparat.io</a><br />
That was real &#8220;JavaScript Rock&#8217;n Roll&#8221;!</p>
<h2>Native vs. Web</h2>
<p>On Saturday, the first day of the BarCamp, I had offered a session &#8220;Mobile: Native vs. Web&#8221; but didn&#8217;t see too many hands raised, so I just didn&#8217;t put it in the schedule, later some people asked me when this session would take place and that I should hold it on Sunday, which I fortunately then did together with <a href="http://twitter.com/flowdi">Florian Detig</a>. And I have to say I am very glad we did so, because 1) we (at uxebu) have a lot of awesome stuff to talk about and 2) it was really fun interacting with the audience and passing on all this information. It is always amazing again how much there is to say about how to create an app which can also be submitted into any of the various app stores by just using web technologies (the <a href="http://www.yourappshop.com">adult industry proved that again</a>). HTML, CSS and JavaScript are just way to underestimated for being of good use in the mobile world. Florian first showed his <a href="http://prezi.com/otqop-_wynvl/">awesome slides</a> (I think they are really cool!). I continued the talk and dove a bit deeper into the mobile web topic, after my try of explaining the different movements in the native API space with players like W3C&#8217;s DAP, BONDI, JIL, WAC we deeply went into answering and discussing web technologies on mobile devices, I talked about our experiences, showed some projects we realized and answered a couple questions.<br />
Not really a surprise but still impressing was the positive feedback we got when I showed of <a href="http://apparat.io">apparat.io</a>, our soon to be launched service that will be able to convert a web app into a native app (You want to know about it first &#8211; follow <a href="http://twitter.com/apparatio">@apparatio</a>). The release date for the private beta will be the 25th October 2010, people who follow @apparatio will get accounts first. I also showed our project <a href="http://embedjs.org">embedJS</a> a toolkit (based on dojo) optimized for mobile usage.</p>
<p>All in all the BarCamp just made it obvious again that we are playing in an very interesting field and that we at uxebu are pushing the limits, that is fun, as much as the BarCamp was big fun, we will be back next time, for sure!<br />
Thanks again and keep up the good work!</p>
]]></content:encoded>
			<wfw:commentRss>http://uxebu.com/blog/2010/10/16/barcamp-munich-2010-seen-through-javascript-and-mobile-eyes/feed/</wfw:commentRss>
		<slash:comments>2</slash:comments>
		</item>
		<item>
		<title>uxebu is hiring</title>
		<link>http://uxebu.com/blog/2010/09/18/uxebu-is-hiring/</link>
		<comments>http://uxebu.com/blog/2010/09/18/uxebu-is-hiring/#comments</comments>
		<pubDate>Sat, 18 Sep 2010 20:17:05 +0000</pubDate>
		<dc:creator>wolfram</dc:creator>
				<category><![CDATA[business]]></category>
		<category><![CDATA[frontend engineering]]></category>
		<category><![CDATA[html5apps]]></category>
		<category><![CDATA[javascript]]></category>
		<category><![CDATA[mobile]]></category>
		<category><![CDATA[uxebu]]></category>
		<category><![CDATA[hiring]]></category>

		<guid isPermaLink="false">http://uxebu.com/blog/?p=1271</guid>
		<description><![CDATA[JSConf is just around the corner and all the JavaScript geeks are meeting there. We from uxebu are going there too, of course :). We are going to give four cool talks David Aurelio (of TouchScroll fame) &#8211; Interface Styling &#038; Scripting for WebKit Mobile Jens Arps (our storage expert) &#8211; The hitchhiker&#8217;s guide to [...]]]></description>
			<content:encoded><![CDATA[<p><a href="http://jsconf.eu/2010/">JSConf</a> is just around the corner and all the JavaScript geeks are meeting there. We from uxebu are going there too, of course :). We are going to give <a href="http://jsconf.eu/2010/speakers.html">four cool talks</a></p>
<ul>
<li><a href="http://twitter.com/void_0">David Aurelio</a> (of <a href="http://static.uxebu.com/~david/touchscroll/">TouchScroll</a> fame) &#8211; Interface Styling &#038; Scripting for WebKit Mobile</li>
<li><a href="http://twitter.com/jensarps">Jens Arps</a> (our <a href="http://jensarps.de/">storage expert</a>) &#8211; The hitchhiker&#8217;s guide to client side persistent data storage</li>
<li><a href="http://twitter.com/tobeytailor">Tobias Schneider</a> (the uncomparable <a href="http://wiki.github.com/tobeytailor/gordon/">Gordon</a> author) &#8211; Not your Mother&#8217;s JavaScript!</li>
<li><a href="http://twitter.com/nonken">Nikolai Onken</a> (a Dojo senior) &#8211; Robotic JavaScript</li>
</ul>
<p>and if you think you have something as cool as what you will see at JSConf you should contact us and we should talk about working together. We at uxebu are putting fun in the job first, actually we are trying to have fun all day and just call that work. Family and health are the top priorities in our company and the absolute base for doing a great job &#8230; oh and JavaScript is what we love, currently we are hacking it only on mobile (or as we like to name it &#8220;embedded&#8221;) devices. &#8220;Embedded&#8221; because we think JavaScript is not only hot on mobiles, but on TV&#8217;s in car dashboards and maybe even in your washing machine :)!? You know where we are going &#8230; are you as enthusiastic as we are? Come and talk to us at the JSConf<span id="more-1271"></span>, or if you have no ticket for JSConf yet give us enough reason to invite you there and get to know your geekiness :).<br />
Let&#8217;s rock the mobile/embedded world &#8230;<br />
Write us to <a href="mailto:contact@uxebu.com">contact@uxebu.com</a> or find us on twitter <a href="http://twitter.com/uxebu">@uxebu</a>.</p>
]]></content:encoded>
			<wfw:commentRss>http://uxebu.com/blog/2010/09/18/uxebu-is-hiring/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Forgotten tricks for iPhones Safari</title>
		<link>http://uxebu.com/blog/2010/04/28/forgotten-tricks-for-iphones-safari/</link>
		<comments>http://uxebu.com/blog/2010/04/28/forgotten-tricks-for-iphones-safari/#comments</comments>
		<pubDate>Wed, 28 Apr 2010 14:18:21 +0000</pubDate>
		<dc:creator>nonken</dc:creator>
				<category><![CDATA[frontend engineering]]></category>
		<category><![CDATA[html5apps]]></category>
		<category><![CDATA[javascript]]></category>
		<category><![CDATA[mobile]]></category>
		<category><![CDATA[iphone]]></category>
		<category><![CDATA[web]]></category>

		<guid isPermaLink="false">http://uxebu.com/blog/?p=1171</guid>
		<description><![CDATA[Safari om the iPhone is an incredible powerful browser and comes with a whole bunch of features. Besides the amazing support for CSS3, a superfast rendering engine and great JavaScript support, there are a few hidden gems I want to explain in this (and maybe following) blogposts. If you are interested in mobile web development, [...]]]></description>
			<content:encoded><![CDATA[<p>Safari om the iPhone is an incredible powerful browser and comes with a whole bunch of features.<br />
Besides the amazing support for CSS3, a superfast rendering engine and great JavaScript support, there are a few hidden gems I want to explain in this (and maybe following) blogposts. If you are interested in mobile web development, maybe you will find a few features you haven&#8217;t seen before.</p>
<h2>Making your web app iPhone ready</h2>
<p>Note: <a href="http://static.uxebu.com/~nonken/a/safariGems">you can visit the example used in this blog post from your iPhone here.</a></p>
<p><object width="500" height="344"><param name="allowfullscreen" value="true" /><param name="allowscriptaccess" value="always" /><param name="movie" value="http://vimeo.com/moogaloop.swf?clip_id=11241853&amp;server=vimeo.com&amp;show_title=1&amp;show_byline=1&amp;show_portrait=1&amp;color=00ADEF&amp;fullscreen=1" /><embed src="http://vimeo.com/moogaloop.swf?clip_id=11241853&amp;server=vimeo.com&amp;show_title=1&amp;show_byline=1&amp;show_portrait=1&amp;color=00ADEF&amp;fullscreen=1" type="application/x-shockwave-flash" allowfullscreen="true" allowscriptaccess="always" width="500" height="344"></embed></object><br />
<span id="more-1171"></span><br />
The most well-known and really most important meta tag you need to know about is the viewport meta-tag. It allows you to define the minimum and maximum zoom-level and whether or not the user is allowed to zoom into the view. You disable zooming by setting the minimum-scale to the same value as maximum-scale. At the same time you can define the viewport width and height by setting the &#8220;device-width&#8221; or &#8220;device-height&#8221; properties. If you have a website with a width of 960px and you want it to be zoomed in to fit exactly that width, all you need to do is to set width to 960. Since your website should run on several devices though with different resolutions it is not recommended to set an absolute width and instead use the predefined device-width and device-height values. This ensures that your viewport has exactly the size supported by the device.</p>
<div class="codecolorer-container javascript twitlight" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:435px;"><table cellspacing="0" cellpadding="0"><tbody><tr><td style="padding:5px;text-align:center;color:#888888;background-color:#EEEEEE;border-right: 1px solid #9F9F9F;font: normal 12px/1.4em Monaco, Lucida Console, monospace;"><div>1<br /></div></td><td><div class="javascript codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap"><span style="color: #339933;">&lt;</span>meta <span style="color: #000066;">name</span><span style="color: #339933;">=</span><span style="color: #3366CC;">&quot;viewport&quot;</span> content<span style="color: #339933;">=</span><span style="color: #3366CC;">&quot;width=device-width,minimum-scale=1.0,maximum-scale=1.0&quot;</span><span style="color: #339933;">/&gt;</span></div></td></tr></tbody></table></div>
<p>Now lets get to the juicy part. When creating an HTML5 app for the iPhone you can define an icon which you will see on the start screen of your iPhone. Once you set this meta tag, the user won&#8217;t be able to see a difference between a native application and a web application on your iPhones homescreen.</p>
<div class="codecolorer-container javascript twitlight" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:435px;"><table cellspacing="0" cellpadding="0"><tbody><tr><td style="padding:5px;text-align:center;color:#888888;background-color:#EEEEEE;border-right: 1px solid #9F9F9F;font: normal 12px/1.4em Monaco, Lucida Console, monospace;"><div>1<br /></div></td><td><div class="javascript codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap"><span style="color: #339933;">&lt;</span>link rel<span style="color: #339933;">=</span><span style="color: #3366CC;">&quot;apple-touch-icon&quot;</span> href<span style="color: #339933;">=</span><span style="color: #3366CC;">&quot;/yourIcon.png&quot;</span><span style="color: #339933;">/&gt;</span></div></td></tr></tbody></table></div>
<p>To add even another level of detail you can define a startup screen for your application. This is especially useful when your application supports offline mode using cache manifests.</p>
<div class="codecolorer-container javascript twitlight" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:435px;"><table cellspacing="0" cellpadding="0"><tbody><tr><td style="padding:5px;text-align:center;color:#888888;background-color:#EEEEEE;border-right: 1px solid #9F9F9F;font: normal 12px/1.4em Monaco, Lucida Console, monospace;"><div>1<br /></div></td><td><div class="javascript codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap"><span style="color: #339933;">&lt;</span>link rel<span style="color: #339933;">=</span><span style="color: #3366CC;">&quot;apple-touch-startup-image&quot;</span> href<span style="color: #339933;">=</span><span style="color: #3366CC;">&quot;/startupImage.png&quot;</span><span style="color: #339933;">&gt;</span></div></td></tr></tbody></table></div>
<p>All of those features wouldn&#8217;t be complete if you couldn&#8217;t hide the browsers chrome once you start your application from the homescreen. To disable the chrome just insert following tag:</p>
<div class="codecolorer-container javascript twitlight" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:435px;"><table cellspacing="0" cellpadding="0"><tbody><tr><td style="padding:5px;text-align:center;color:#888888;background-color:#EEEEEE;border-right: 1px solid #9F9F9F;font: normal 12px/1.4em Monaco, Lucida Console, monospace;"><div>1<br /></div></td><td><div class="javascript codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap"><span style="color: #339933;">&lt;</span>meta <span style="color: #000066;">name</span><span style="color: #339933;">=</span><span style="color: #3366CC;">&quot;apple-mobile-web-app-capable&quot;</span> content<span style="color: #339933;">=</span><span style="color: #3366CC;">&quot;yes&quot;</span><span style="color: #339933;">&gt;</span></div></td></tr></tbody></table></div>
<p>Now after you have added this meta tag you want to be able to differenciate between the user launching the app from the homescreen of the browser.</p>
<div class="codecolorer-container javascript twitlight" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:435px;"><table cellspacing="0" cellpadding="0"><tbody><tr><td style="padding:5px;text-align:center;color:#888888;background-color:#EEEEEE;border-right: 1px solid #9F9F9F;font: normal 12px/1.4em Monaco, Lucida Console, monospace;"><div>1<br />2<br />3<br />4<br />5<br />6<br />7<br />8<br />9<br />10<br /></div></td><td><div class="javascript codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap"><span style="color: #006600; font-style: italic;">// navigator.standalone</span><br />
<span style="color: #000066; font-weight: bold;">if</span> <span style="color: #009900;">&#40;</span><span style="color: #3366CC;">&quot;standalone&quot;</span> <span style="color: #000066; font-weight: bold;">in</span> navigator<span style="color: #009900;">&#41;</span><span style="color: #009900;">&#123;</span><br />
&nbsp; &nbsp; <span style="color: #000066; font-weight: bold;">if</span> <span style="color: #009900;">&#40;</span>navigator.<span style="color: #660066;">standalone</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#123;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #000066;">alert</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">&quot;Started from homescreen&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp; <span style="color: #009900;">&#125;</span><span style="color: #000066; font-weight: bold;">else</span><span style="color: #009900;">&#123;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #000066;">alert</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">&quot;Started in browser&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp; <span style="color: #009900;">&#125;</span><br />
<span style="color: #009900;">&#125;</span><span style="color: #000066; font-weight: bold;">else</span><span style="color: #009900;">&#123;</span><br />
&nbsp; &nbsp; <span style="color: #000066;">alert</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">&quot;Not supporting standalone property&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
<span style="color: #009900;">&#125;</span></div></td></tr></tbody></table></div>
<p>Last but not least you can even control the look and feel of the status bar on the top. You can either use the default status bar or a black status bar or a semi translucent status bar:</p>
<div class="codecolorer-container javascript twitlight" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:435px;"><table cellspacing="0" cellpadding="0"><tbody><tr><td style="padding:5px;text-align:center;color:#888888;background-color:#EEEEEE;border-right: 1px solid #9F9F9F;font: normal 12px/1.4em Monaco, Lucida Console, monospace;"><div>1<br /></div></td><td><div class="javascript codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap"><span style="color: #339933;">&lt;</span>meta <span style="color: #000066;">name</span><span style="color: #339933;">=</span><span style="color: #3366CC;">&quot;apple-mobile-web-app-status-bar-style&quot;</span> content<span style="color: #339933;">=</span><span style="color: #3366CC;">&quot;black/default/black-translucent&quot;</span> <span style="color: #339933;">/&gt;</span></div></td></tr></tbody></table></div>
<p>These are the most important things you need to know to make a simple website be HTML5 app ready on your iPhone (note: this only works on the iPhone but won&#8217;t hurt other devices and platforms). A &#8220;normal&#8221; user won&#8217;t be able to differentiate between a native or a HTML5 app anymore. Do you know of similar features of other phones? Do you think we should standardize the discussed features? (Some are already). </p>
<p>If you need support getting your website or web application HTML5 app ready, feel free to <a href="http://uxebu.com/contact">contact us</a>.</p>
<p>Enjoy</p>
<h2>Dowload</h2>
<ul>
<li>
<a href="http://static.uxebu.com/~nonken/a/safariGems/safariGems.tgz">Source</a>
</li>
<li><a href="http://developer.apple.com/safari/library/documentation/AppleApplications/Reference/SafariWebContent/ConfiguringWebApplications/ConfiguringWebApplications.html">Docs</a></li>
</ul>
]]></content:encoded>
			<wfw:commentRss>http://uxebu.com/blog/2010/04/28/forgotten-tricks-for-iphones-safari/feed/</wfw:commentRss>
		<slash:comments>13</slash:comments>
		</item>
		<item>
		<title>One var statement for one variable!</title>
		<link>http://uxebu.com/blog/2010/04/02/one-var-statement-for-one-variable/</link>
		<comments>http://uxebu.com/blog/2010/04/02/one-var-statement-for-one-variable/#comments</comments>
		<pubDate>Fri, 02 Apr 2010 16:15:12 +0000</pubDate>
		<dc:creator>wolfram</dc:creator>
				<category><![CDATA[frontend engineering]]></category>
		<category><![CDATA[javascript]]></category>
		<category><![CDATA[style guide]]></category>
		<category><![CDATA[var statement]]></category>

		<guid isPermaLink="false">http://uxebu.com/blog/?p=1135</guid>
		<description><![CDATA[JavaScript allows to comma-separate multiple variable declarations, like so: 1var i=0, j=1 . Declaring multiple variables using one var-statement accross multiple lines is a NO GO! I consider this evil. And I learned it the hard way. It might look pretty nice, looks like less code and more efficient. But it definitely is not so [...]]]></description>
			<content:encoded><![CDATA[<p>JavaScript allows to comma-separate multiple variable declarations, like so:</p>
<div class="codecolorer-container text twitlight" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:435px;"><table cellspacing="0" cellpadding="0"><tbody><tr><td style="padding:5px;text-align:center;color:#888888;background-color:#EEEEEE;border-right: 1px solid #9F9F9F;font: normal 12px/1.4em Monaco, Lucida Console, monospace;"><div>1<br /></div></td><td><div class="text codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap">var i=0, j=1</div></td></tr></tbody></table></div>
<p>. Declaring multiple variables using one var-statement accross multiple lines is a NO GO! I consider this evil. And I learned it the hard way. It might look pretty nice, looks like less code and more efficient. But it definitely is not so when writing code. If it results in more speed let your build tool, compressor or compiler do it. But don&#8217;t write code which spreads multiple variable declarations in var-statement across multiple lines!<br />
PERIOD.<br />
<span id="more-1135"></span></p>
<p>This article is quite long for such a simple topic, but anyways I just feel like writing it down, since this is how I initially used <a href="http://wolfram.kriesing.de/blog">my previous blog</a> and I must say it was very helpful for me to look up things again but obviously also for others to find information. I also feel that showing what coding guide lines to use and the reasoning behind it is really important especially also for those who have to apply them.</p>
<h2>Good and Bad</h2>
<p>Let me just quickly show examples for DOs and DON&#8217;Ts.</p>
<div class="codecolorer-container javascript twitlight" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:435px;"><table cellspacing="0" cellpadding="0"><tbody><tr><td style="padding:5px;text-align:center;color:#888888;background-color:#EEEEEE;border-right: 1px solid #9F9F9F;font: normal 12px/1.4em Monaco, Lucida Console, monospace;"><div>1<br />2<br />3<br />4<br /></div></td><td><div class="javascript codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap"><span style="color: #006600; font-style: italic;">// DONT</span><br />
<span style="color: #003366; font-weight: bold;">var</span> x <span style="color: #339933;">=</span> <span style="color: #CC0000;">1</span><span style="color: #339933;">,</span><br />
y <span style="color: #339933;">=</span> <span style="color: #CC0000;">2</span><span style="color: #339933;">,</span><br />
z <span style="color: #339933;">=</span> <span style="color: #CC0000;">3</span><span style="color: #339933;">;</span></div></td></tr></tbody></table></div>
<p>The above looks pretty slick. But not always! Especially when working in a team and when you will rewrite or touch the later code again, which basically is the case with all the useful code out there. The second and the third line depend on the first line. That means if a coworker comes along and pastes in his variable from his awesome and dead-simple code</p>
<div class="codecolorer-container javascript twitlight" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:435px;"><table cellspacing="0" cellpadding="0"><tbody><tr><td style="padding:5px;text-align:center;color:#888888;background-color:#EEEEEE;border-right: 1px solid #9F9F9F;font: normal 12px/1.4em Monaco, Lucida Console, monospace;"><div>1<br />2<br />3<br /></div></td><td><div class="javascript codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap"><span style="color: #006600; font-style: italic;">// DONT</span><br />
<span style="color: #003366; font-weight: bold;">var</span> y0 <span style="color: #339933;">=</span> <span style="color: #CC0000;">1</span><span style="color: #339933;">,</span><br />
y1 <span style="color: #339933;">=</span> <span style="color: #CC0000;">2</span><span style="color: #339933;">;</span></div></td></tr></tbody></table></div>
<p>into my code above. We might get the following:</p>
<div class="codecolorer-container javascript twitlight" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:435px;"><table cellspacing="0" cellpadding="0"><tbody><tr><td style="padding:5px;text-align:center;color:#888888;background-color:#EEEEEE;border-right: 1px solid #9F9F9F;font: normal 12px/1.4em Monaco, Lucida Console, monospace;"><div>1<br />2<br />3<br />4<br />5<br /></div></td><td><div class="javascript codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap"><span style="color: #006600; font-style: italic;">// DONT</span><br />
<span style="color: #003366; font-weight: bold;">var</span> x <span style="color: #339933;">=</span> <span style="color: #CC0000;">1</span><span style="color: #339933;">,</span><br />
y <span style="color: #339933;">=</span> <span style="color: #CC0000;">2</span><span style="color: #339933;">,</span><br />
y1 <span style="color: #339933;">=</span> <span style="color: #CC0000;">2</span><span style="color: #339933;">;</span><br />
z <span style="color: #339933;">=</span> <span style="color: #CC0000;">3</span><span style="color: #339933;">;</span></div></td></tr></tbody></table></div>
<p>And ouch, we have a global variable &#8220;z&#8221;. Of course he should change the trailing semicolon to a comma when pasting in his code. But his IM beeped, phone rang, coffee was done or he simply felt more like skiing and stopped hacking &#8211; and he forgot :). You know what I mean!<br />
If the code hadn&#8217;t contained multiline variable declarations it wouldn&#8217;t have happened, like here:</p>
<div class="codecolorer-container javascript twitlight" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:435px;"><table cellspacing="0" cellpadding="0"><tbody><tr><td style="padding:5px;text-align:center;color:#888888;background-color:#EEEEEE;border-right: 1px solid #9F9F9F;font: normal 12px/1.4em Monaco, Lucida Console, monospace;"><div>1<br />2<br />3<br />4<br />5<br /></div></td><td><div class="javascript codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap"><span style="color: #006600; font-style: italic;">// DO</span><br />
<span style="color: #003366; font-weight: bold;">var</span> x <span style="color: #339933;">=</span> <span style="color: #CC0000;">1</span><span style="color: #339933;">;</span><br />
<span style="color: #003366; font-weight: bold;">var</span> y <span style="color: #339933;">=</span> <span style="color: #CC0000;">2</span><span style="color: #339933;">;</span><br />
<span style="color: #003366; font-weight: bold;">var</span> y1 <span style="color: #339933;">=</span> <span style="color: #CC0000;">2</span><span style="color: #339933;">;</span><br />
<span style="color: #003366; font-weight: bold;">var</span> z <span style="color: #339933;">=</span> <span style="color: #CC0000;">3</span><span style="color: #339933;">;</span></div></td></tr></tbody></table></div>
<p>Though there are cases when I still think that listing multiple variable declarations is ok, like the following. But just not on multiple lines!</p>
<div class="codecolorer-container javascript twitlight" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:435px;"><table cellspacing="0" cellpadding="0"><tbody><tr><td style="padding:5px;text-align:center;color:#888888;background-color:#EEEEEE;border-right: 1px solid #9F9F9F;font: normal 12px/1.4em Monaco, Lucida Console, monospace;"><div>1<br />2<br />3<br /></div></td><td><div class="javascript codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap"><span style="color: #006600; font-style: italic;">// DO</span><br />
<span style="color: #003366; font-weight: bold;">var</span> x <span style="color: #339933;">=</span> <span style="color: #CC0000;">1</span><span style="color: #339933;">,</span> y <span style="color: #339933;">=</span> <span style="color: #CC0000;">2</span><span style="color: #339933;">,</span> z <span style="color: #339933;">=</span> <span style="color: #CC0000;">3</span><span style="color: #339933;">;</span><br />
<span style="color: #000066; font-weight: bold;">for</span> <span style="color: #009900;">&#40;</span><span style="color: #003366; font-weight: bold;">var</span> i<span style="color: #339933;">=</span><span style="color: #CC0000;">0</span><span style="color: #339933;">,</span> l<span style="color: #339933;">=</span>arr.<span style="color: #660066;">length</span><span style="color: #339933;">,</span> k<span style="color: #339933;">;</span> i</div></td></tr></tbody></table></div>
<h2>Real Life Examples</h2>
<p>Let me show you three examples where I really felt the pain of the described problem, which finally triggered me to write this article. Those are real life examples, exactly the code I experienced the problem with. And at some point it was just enough.<br />
The first example shows that a pretty complex piece of code is just not as easy to validate by humans. There is no bug in it, it is just hard to read code.</p>
<div class="codecolorer-container javascript twitlight" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:435px;"><table cellspacing="0" cellpadding="0"><tbody><tr><td style="padding:5px;text-align:center;color:#888888;background-color:#EEEEEE;border-right: 1px solid #9F9F9F;font: normal 12px/1.4em Monaco, Lucida Console, monospace;"><div>1<br />2<br />3<br />4<br />5<br />6<br />7<br />8<br /></div></td><td><div class="javascript codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap"><span style="color: #006600; font-style: italic;">// DONT</span><br />
<span style="color: #003366; font-weight: bold;">var</span> g <span style="color: #339933;">=</span> <span style="color: #000066; font-weight: bold;">this</span>.<span style="color: #660066;">getObject</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">&quot;group.name&quot;</span><span style="color: #339933;">,</span> test<span style="color: #009900;">&#41;</span> <span style="color: #339933;">||</span> groupName<span style="color: #339933;">,</span><br />
<span style="color: #006600; font-style: italic;">// Trim and replace all special chars, to have a proper test ID.</span><br />
group <span style="color: #339933;">=</span> g.<span style="color: #660066;">replace</span><span style="color: #009900;">&#40;</span><span style="color: #009966; font-style: italic;">/^s*/</span><span style="color: #339933;">,</span> <span style="color: #3366CC;">&quot;&quot;</span><span style="color: #009900;">&#41;</span>.<span style="color: #660066;">replace</span><span style="color: #009900;">&#40;</span><span style="color: #009966; font-style: italic;">/s*$/</span><span style="color: #339933;">,</span> <span style="color: #3366CC;">&quot;&quot;</span><span style="color: #009900;">&#41;</span><br />
.<span style="color: #660066;">replace</span><span style="color: #009900;">&#40;</span><span style="color: #009966; font-style: italic;">/[^0-9a-zA-Z]/g</span><span style="color: #339933;">,</span> <span style="color: #3366CC;">&quot;_&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">,</span><br />
<span style="color: #006600; font-style: italic;">// Replace multiple underscores with just one.</span><br />
ret <span style="color: #339933;">=</span> <span style="color: #009900;">&#40;</span><span style="color: #3366CC;">&quot;ID_&quot;</span> <span style="color: #339933;">+</span> group<span style="color: #009900;">&#41;</span>.<span style="color: #660066;">replace</span><span style="color: #009900;">&#40;</span><span style="color: #009966; font-style: italic;">/_+/g</span><span style="color: #339933;">,</span> <span style="color: #3366CC;">&quot;_&quot;</span><span style="color: #009900;">&#41;</span><br />
.<span style="color: #660066;">replace</span><span style="color: #009900;">&#40;</span><span style="color: #009966; font-style: italic;">/[_]+$/</span><span style="color: #339933;">,</span> <span style="color: #3366CC;">&quot;&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span></div></td></tr></tbody></table></div>
<p>You can see on line 3 no comma is needed, because I just split up the chained calls for better readability. This may mislead the one who wants to understand the code. On the next line though a comma has to end the line because it is the end of the variable declaration. Additionally the code above has a comment after each variable declaration, which makes it easy to read and can be understood faster. If that was not the case it would be much harder to read the code. Just remember the one reading the code doesn&#8217;t want to read every line carefully, this is just very very time consuming. Therefore following certains rules can help to make this easier.<br />
Another issue this code shows very well is that you first have to scan the code and understand the indentions, this sometimes means reading all the way up to the first variable declaration. It&#8217;s just unnecessary brain work.<br />
So let&#8217;s make the source code easier to read, use a var statement for every variable:</p>
<div class="codecolorer-container javascript twitlight" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:435px;"><table cellspacing="0" cellpadding="0"><tbody><tr><td style="padding:5px;text-align:center;color:#888888;background-color:#EEEEEE;border-right: 1px solid #9F9F9F;font: normal 12px/1.4em Monaco, Lucida Console, monospace;"><div>1<br />2<br />3<br />4<br />5<br />6<br />7<br />8<br /></div></td><td><div class="javascript codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap"><span style="color: #006600; font-style: italic;">// DO</span><br />
<span style="color: #003366; font-weight: bold;">var</span> g <span style="color: #339933;">=</span> <span style="color: #000066; font-weight: bold;">this</span>.<span style="color: #660066;">getObject</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">&quot;group.name&quot;</span><span style="color: #339933;">,</span> test<span style="color: #009900;">&#41;</span> <span style="color: #339933;">||</span> groupName<span style="color: #339933;">;</span><br />
<span style="color: #006600; font-style: italic;">// Trim and replace all special chars, to have a proper test ID.</span><br />
<span style="color: #003366; font-weight: bold;">var</span> group <span style="color: #339933;">=</span> g.<span style="color: #660066;">replace</span><span style="color: #009900;">&#40;</span><span style="color: #009966; font-style: italic;">/^s*/</span><span style="color: #339933;">,</span> <span style="color: #3366CC;">&quot;&quot;</span><span style="color: #009900;">&#41;</span>.<span style="color: #660066;">replace</span><span style="color: #009900;">&#40;</span><span style="color: #009966; font-style: italic;">/s*$/</span><span style="color: #339933;">,</span> <span style="color: #3366CC;">&quot;&quot;</span><span style="color: #009900;">&#41;</span><br />
.<span style="color: #660066;">replace</span><span style="color: #009900;">&#40;</span><span style="color: #009966; font-style: italic;">/[^0-9a-zA-Z]/g</span><span style="color: #339933;">,</span> <span style="color: #3366CC;">&quot;_&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
<span style="color: #006600; font-style: italic;">// Replace multiple underscores with just one.</span><br />
<span style="color: #003366; font-weight: bold;">var</span> ret <span style="color: #339933;">=</span> <span style="color: #009900;">&#40;</span><span style="color: #3366CC;">&quot;ID_&quot;</span> <span style="color: #339933;">+</span> group<span style="color: #009900;">&#41;</span>.<span style="color: #660066;">replace</span><span style="color: #009900;">&#40;</span><span style="color: #009966; font-style: italic;">/_+/g</span><span style="color: #339933;">,</span> <span style="color: #3366CC;">&quot;_&quot;</span><span style="color: #009900;">&#41;</span><br />
.<span style="color: #660066;">replace</span><span style="color: #009900;">&#40;</span><span style="color: #009966; font-style: italic;">/[_]+$/</span><span style="color: #339933;">,</span> <span style="color: #3366CC;">&quot;&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span></div></td></tr></tbody></table></div>
<p>This also makes understanding the indentions much easier. Doesn&#8217;t it? I just looked at the draft of this blog article and thought that the &#8220;wrong&#8221; source code above was wrong formatted because I didn&#8217;t understand the indention just by looking at the code (without thinking too much). Looking at the &#8220;fixed&#8221; source made my brain not start to think, it just looked all very clear to me.</p>
<p>In another real life example it took me three hours (three fucking hours) to find the bug. It was the following code in the dojo-mobile <a href="http://github.com/wolframkriesing/dojo-mobile/blob/eae475575b0b1991fa66597c4faed94513e4a14c/build/build.js#L25">build script you can find on github</a>. And I was debugging the code, reading it over and over again. I just didn&#8217;t find the bug. Try the following, scan the code quickly just once and read on.</p>
<div class="codecolorer-container javascript twitlight" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:435px;"><table cellspacing="0" cellpadding="0"><tbody><tr><td style="padding:5px;text-align:center;color:#888888;background-color:#EEEEEE;border-right: 1px solid #9F9F9F;font: normal 12px/1.4em Monaco, Lucida Console, monospace;"><div>1<br />2<br />3<br />4<br />5<br />6<br /></div></td><td><div class="javascript codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap"><span style="color: #006600; font-style: italic;">// DONT</span><br />
<span style="color: #003366; font-weight: bold;">var</span> parts <span style="color: #339933;">=</span> feature.<span style="color: #660066;">split</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">&quot;-&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
ns <span style="color: #339933;">=</span> parts<span style="color: #009900;">&#91;</span><span style="color: #CC0000;">0</span><span style="color: #009900;">&#93;</span><span style="color: #339933;">,</span> <span style="color: #006600; font-style: italic;">// The namespace of the feature, like &quot;oo&quot; in &quot;oo&quot; or &quot;oo-declare.</span><br />
f <span style="color: #339933;">=</span> parts.<span style="color: #660066;">length</span><span style="color: #339933;">&amp;</span>gt<span style="color: #339933;">;</span><span style="color: #CC0000;">1</span> <span style="color: #339933;">?</span> parts<span style="color: #009900;">&#91;</span><span style="color: #CC0000;">1</span><span style="color: #009900;">&#93;</span> <span style="color: #339933;">:</span> <span style="color: #003366; font-weight: bold;">null</span><span style="color: #339933;">,</span> <span style="color: #006600; font-style: italic;">// The exact feature if given (&quot;oo-declare&quot;).</span><br />
ret <span style="color: #339933;">=</span> <span style="color: #009900;">&#91;</span><span style="color: #009900;">&#93;</span><span style="color: #339933;">,</span><br />
data <span style="color: #339933;">=</span> globals.<span style="color: #660066;">profileData</span><span style="color: #339933;">;</span></div></td></tr></tbody></table></div>
<p>Did you see it on first sight? Scan the code again. Believe me I read the code over and over again, I even thought there might be a variable declaration issue. But since my return variables are always named &#8220;ret&#8221; and my code was heavily asynchronously programmed I didn&#8217;t find out which declaration was the faulty one. It is fixed now (<a href="http://github.com/wolframkriesing/dojo-mobile/blob/master/tools/build.js">on github</a>)!</p>
<div class="codecolorer-container javascript twitlight" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:435px;"><table cellspacing="0" cellpadding="0"><tbody><tr><td style="padding:5px;text-align:center;color:#888888;background-color:#EEEEEE;border-right: 1px solid #9F9F9F;font: normal 12px/1.4em Monaco, Lucida Console, monospace;"><div>1<br />2<br />3<br />4<br />5<br />6<br /></div></td><td><div class="javascript codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap"><span style="color: #006600; font-style: italic;">// DO</span><br />
<span style="color: #003366; font-weight: bold;">var</span> parts <span style="color: #339933;">=</span> feature.<span style="color: #660066;">split</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">&quot;-&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
<span style="color: #003366; font-weight: bold;">var</span> ns <span style="color: #339933;">=</span> parts<span style="color: #009900;">&#91;</span><span style="color: #CC0000;">0</span><span style="color: #009900;">&#93;</span><span style="color: #339933;">;</span> <span style="color: #006600; font-style: italic;">// The namespace of the feature, like &quot;oo&quot; in &quot;oo&quot; or &quot;oo-declare.</span><br />
<span style="color: #003366; font-weight: bold;">var</span> f <span style="color: #339933;">=</span> parts.<span style="color: #660066;">length</span><span style="color: #339933;">&amp;</span>gt<span style="color: #339933;">;</span><span style="color: #CC0000;">1</span> <span style="color: #339933;">?</span> parts<span style="color: #009900;">&#91;</span><span style="color: #CC0000;">1</span><span style="color: #009900;">&#93;</span> <span style="color: #339933;">:</span> <span style="color: #003366; font-weight: bold;">null</span><span style="color: #339933;">;</span> <span style="color: #006600; font-style: italic;">// The exact feature if given (&quot;oo-declare&quot;).</span><br />
<span style="color: #003366; font-weight: bold;">var</span> ret <span style="color: #339933;">=</span> <span style="color: #009900;">&#91;</span><span style="color: #009900;">&#93;</span><span style="color: #339933;">;</span><br />
<span style="color: #003366; font-weight: bold;">var</span> data <span style="color: #339933;">=</span> globals.<span style="color: #660066;">profileData</span><span style="color: #339933;">;</span></div></td></tr></tbody></table></div>
<p>Yet another example. This one actually just proved again that I definitely have to forget about the old way. You can also <a href="http://github.com/wolframkriesing/dojo-mobile/blob/eae475575b0b1991fa66597c4faed94513e4a14c/build/build.js#L67">find it on github</a> and feel free to follow the path of commits. My learning curve, if you will :).</p>
<div class="codecolorer-container javascript twitlight" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:435px;"><table cellspacing="0" cellpadding="0"><tbody><tr><td style="padding:5px;text-align:center;color:#888888;background-color:#EEEEEE;border-right: 1px solid #9F9F9F;font: normal 12px/1.4em Monaco, Lucida Console, monospace;"><div>1<br />2<br />3<br />4<br /></div></td><td><div class="javascript codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap"><span style="color: #006600; font-style: italic;">// DONT</span><br />
<span style="color: #003366; font-weight: bold;">var</span> depsData <span style="color: #339933;">=</span> _depsDataStack.<span style="color: #660066;">pop</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">,</span><br />
dir <span style="color: #339933;">=</span> depsData.<span style="color: #660066;">directory</span><span style="color: #339933;">;</span><br />
file <span style="color: #339933;">=</span> depsData.<span style="color: #660066;">file</span><span style="color: #339933;">;</span></div></td></tr></tbody></table></div>
<p>This one was not so hard to find, I agree. But still it was a bastard and stole my time. By sticking to a better convention I could have avoided it. Lesson learned.</p>
<h2>Closing Thoughts</h2>
<p>Just when preparing this article (which was supposed to be much shorter) I read through Douglas Crockford&#8217;s <a href="http://www.JSLint.com/lint.html">JSLint rules</a> and funnily I found this one in there:</p>
<blockquote><p>It is recommended that a single var statement be used per function.</p></blockquote>
<p>I can say I disagree and I guess I gave some reasoning.</p>
<p>Though on the other hand <a href="http://javascript.crockford.com/code.html">I found this</a> on the Crockford pages :)</p>
<blockquote><p>It is preferred that each variable be given its own line and comment. They should be listed in alphabetical order.</p></blockquote>
<p>Performance-wise, as stated above, I would leave the task to a compiler or alike tool to concatenate multiple variable statements into one, if it has an effect at all. The productivity and happiness of the programmers and not to loose time which could have been saved by just sticking to a simple convention is worth a lot more than pre-optimized code.<br />
I am looking forward to feedback.</p>
]]></content:encoded>
			<wfw:commentRss>http://uxebu.com/blog/2010/04/02/one-var-statement-for-one-variable/feed/</wfw:commentRss>
		<slash:comments>13</slash:comments>
		</item>
	</channel>
</rss>

