<?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>JavascriptKata</title>
	<atom:link href="http://www.javascriptkata.com/feed/" rel="self" type="application/rss+xml" />
	<link>http://www.javascriptkata.com</link>
	<description>helping you with javascript since 2007</description>
	<lastBuildDate>Thu, 03 May 2012 20:43:04 +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>How to write javascript code to fit in lines of 80 chars</title>
		<link>http://www.javascriptkata.com/2012/04/30/how-to-write-javascript-code-to-fit-in-lines-of-80-chars/?utm_source=rss&#038;utm_medium=rss&#038;utm_campaign=how-to-write-javascript-code-to-fit-in-lines-of-80-chars</link>
		<comments>http://www.javascriptkata.com/2012/04/30/how-to-write-javascript-code-to-fit-in-lines-of-80-chars/#comments</comments>
		<pubDate>Mon, 30 Apr 2012 14:03:28 +0000</pubDate>
		<dc:creator>dsimard</dc:creator>
				<category><![CDATA[how to]]></category>
		<category><![CDATA[white belt]]></category>

		<guid isPermaLink="false">http://www.javascriptkata.com/?p=211</guid>
		<description><![CDATA[Nobody likes to scroll horizontally, even programmers]]></description>
			<content:encoded><![CDATA[<p>I recently read <a href="http://javascript.crockford.com/code.html">Code Conventions for the JavaScript Programming Language</a> from <a href="http://crockford.com/">Douglas Crockford</a>, the guy that <em>discovered</em> JSON. I was really surprised to see such an old convention as <strong>Avoid lines longer than 80 characters</strong>. For the past years, I didn&#8217;t follow this convention until a couple of months ago when I accidentally clicked the option to show the 80 chars limit on <a href="http://projects.gnome.org/gedit/">gEdit</a> and didn&#8217;t bother to unclick it. Without noticing it, I began writing with a limit of 80 chars and <strong>it made my code much clearer</strong>. Plus, <strong>nobody likes to scroll horizontally</strong>, even programmers.</p>
<h3>Functions</h3>
<p>This one is simple. You should just avoid having a long list of arguments by putting them on 2 lines with a tab (or 2 spaces) on the second line.</p>
<p><script src="https://gist.github.com/2509740.js?file=lots%20of%20arguments.js%20"></script></p>
<h3>Use hashes for optional arguments</h3>
<p>A good way of avoiding long list of argument is to put optional arguments in a hash. Suppose that the first two arguments are required and the other ones are optional. You could write something like this.</p>
<p><script src="https://gist.github.com/2509740.js?file=optional_args.js"></script></p>
<h3>Chains</h3>
<p>In jQuery, we often have to write a long chain of command. Here is how I write it to make it clearer.</p>
<p><script src="https://gist.github.com/2509740.js?file=chaining.js"></script></p>
<h3>One-liner IF</h3>
<p>The more I code, the less lines of code I want. I write a lot of Ruby and I like one-liners. Here&#8217;s a little tip on one-liners ifs in javascript.</p>
<p><script src="https://gist.github.com/2509740.js?file=one-liner%20if.js"></script></p>
<h3>Tabs are dead, use 2 spaces</h3>
<p>When I was a brand new programmer, tabs were all the rage. The bigger your tab space, the better. I was using a 8 spaces tab length at the time. And those were hardcore tabs too, not pseudo-tabs created by adding spaces one after the other. The best programmers were even uselessly indenting some statements as in this :</p>
<p><script src="https://gist.github.com/2509740.js?file=best_programmers_from_the_90s.pas"></script></p>
<p>These days, most editors have the &#8220;Insert spaces instead of tabs&#8221; and &#8220;Tab width&#8221; options. Look for them and do the following :<br />
1. Check the box beside the &#8220;Insert spaces instead of tabs&#8221;<br />
2. Write &#8220;2&#8243; inside the &#8220;Tab width&#8221;</p>
]]></content:encoded>
			<wfw:commentRss>http://www.javascriptkata.com/2012/04/30/how-to-write-javascript-code-to-fit-in-lines-of-80-chars/feed/</wfw:commentRss>
		<slash:comments>2</slash:comments>
		</item>
		<item>
		<title>How to make sure undefined is not defined</title>
		<link>http://www.javascriptkata.com/2011/09/13/how-to-make-sure-undefined-is-not-defined/?utm_source=rss&#038;utm_medium=rss&#038;utm_campaign=how-to-make-sure-undefined-is-not-defined</link>
		<comments>http://www.javascriptkata.com/2011/09/13/how-to-make-sure-undefined-is-not-defined/#comments</comments>
		<pubDate>Tue, 13 Sep 2011 13:59:30 +0000</pubDate>
		<dc:creator>dsimard</dc:creator>
				<category><![CDATA[black belt]]></category>
		<category><![CDATA[how to]]></category>

		<guid isPermaLink="false">http://www.javascriptkata.com/?p=365</guid>
		<description><![CDATA[Think about it, you write a plugin or a library (let&#8217;s name it jsKata) and your code contains checks to see if certain things are undefined. Here&#8217;s an example : if (obj.name === undefined) { &#160; obj.name = &#34;This object has no name&#34;; } This code works well until you include another script (found on [...]]]></description>
			<content:encoded><![CDATA[<p>Think about it, you write a plugin or a library (let&#8217;s name it <a href="http://github.com/dsimard/jskata" target="_blank">jsKata</a>) and your code contains checks to see if certain things are <em>undefined</em>.</p>
<p>Here&#8217;s an example :</p>
<div class="dean_ch" style="white-space: wrap;">
<ol>
<li class="li1">
<div class="de1">if (obj.name === undefined) {
</div>
</li>
<li class="li1">
<div class="de1">&nbsp; obj.name = &quot;This object has no name&quot;;
</div>
</li>
<li class="li1">
<div class="de1">}</div>
</li>
</ol>
</div>
<p>This code works well until you include another script (found on an obscure website) and it breaks your previous code. By debugging the code, you see the value of <em>undefined</em> is no more <em>undefined</em> but <em>false</em>.</p>
<p>Yes, I know that you thought it was impossible to define <em>undefined</em> but it is, you just have to write <em>undefined = false</em>.</p>
<h3>Redefine undefined</h3>
<p>There&#8217;s two way of redefining undefined and they both use <a href="http://www.javascriptkata.com/2011/08/22/self-invoking-functions-explained-line-by-line/" title="Self-invoking functions explained line by line" target="_blank">self-invoking functions</a>.</p>
<h4>Method 1 : Scope your code</h4>
<div class="dean_ch" style="white-space: wrap;">
<ol>
<li class="li1">
<div class="de1">(function(undefined) {
</div>
</li>
<li class="li1">
<div class="de1">&nbsp; /* your complete code here */
</div>
</li>
<li class="li1">
<div class="de1">})();</div>
</li>
</ol>
</div>
<p>As you see, the self-invoking function has one parameter named <em>undefined</em>. At line 3, the self-invoking functions is called <strong>without any parameter</strong>. It results that the parameter has the value <em>undefined</em> (or it is defined to <em>undefined</em> if you prefer).</p>
<p>Take a look at this javascript :<br />
<script src="https://gist.github.com/1209870.js?file=fiddle.js"></script></p>
<p>It will display <em>false</em> and then display <em>undefined</em> even if undefined was globally defined at first.</p>
<p>This is the HTML that goes with it :<br />
<script src="https://gist.github.com/1209870.js?file=fiddle.html"></script></p>
<p>You can also execute it <a href="http://jsfiddle.net/gh/gist/jquery/1.6/1209870/" target="_blank">here</a>.</p>
<h4>Method 2 : Globally redefine undefined</h4>
<p>There&#8217;s another method but I don&#8217;t like it that much. It redefines undefined but nothing protects it from being badly redefined in another chunk of code.</p>
<p><script src="https://gist.github.com/1213798.js?file=fiddle.js"></script></p>
<p><em>undefined</em> is a global variable accessible using the global <em>window</em> object. Line 2 defines <em>undefined</em> and line 3 display the value of <em>undefined</em> as <em>false</em>. On line 4, I use a <a href="http://www.javascriptkata.com/2011/08/22/self-invoking-functions-explained-line-by-line/" title="Self-invoking functions explained line by line" target="_blank">self-invoking function</a> as method 1 does but now, I define the global <em>undefined</em> variable through the global <em>window</em> object.</p>
<p>The HTML of this example :<br />
<script src="https://gist.github.com/1213798.js?file=fiddle.js"></script></p>
<p>You can also execute it on <a href="http://jsfiddle.net/gh/gist/jquery/1.6/1213798/" target="_blank">jsFiddle</a>.</p>
<h3>Other methods</h3>
<p>Some commentors pointed me other ways of doing it.</p>
<p>You can use <strong>typeof</strong> :</p>
<div class="dean_ch" style="white-space: wrap;">
<ol>
<li class="li1">
<div class="de1">typeof undefined == &quot;undefined&quot;</div>
</li>
</ol>
</div>
<p>Or you could use <strong>void(0)</strong>, it always returns <em>undefined</em></p>
<div class="dean_ch" style="white-space: wrap;">
<ol>
<li class="li1">
<div class="de1">undefined === void(0) // this returns true</div>
</li>
</ol>
</div>
]]></content:encoded>
			<wfw:commentRss>http://www.javascriptkata.com/2011/09/13/how-to-make-sure-undefined-is-not-defined/feed/</wfw:commentRss>
		<slash:comments>11</slash:comments>
		</item>
		<item>
		<title>Creating namespaces with self-invoking functions</title>
		<link>http://www.javascriptkata.com/2011/09/05/creating-namespaces-with-self-invoking-functions/?utm_source=rss&#038;utm_medium=rss&#038;utm_campaign=creating-namespaces-with-self-invoking-functions</link>
		<comments>http://www.javascriptkata.com/2011/09/05/creating-namespaces-with-self-invoking-functions/#comments</comments>
		<pubDate>Mon, 05 Sep 2011 14:40:41 +0000</pubDate>
		<dc:creator>dsimard</dc:creator>
				<category><![CDATA[Uncategorized]]></category>

		<guid isPermaLink="false">http://www.javascriptkata.com/?p=350</guid>
		<description><![CDATA[When a project&#8217;s javascript grows, it can be easy to lose ourselves, buried under thousands of lines of js code. That&#8217;s why I like to create namespaces to keep everything together. Let&#8217;s say I have functions to work with cats and I create a namespace for it because there are lots of other functions related [...]]]></description>
			<content:encoded><![CDATA[<p>When a project&#8217;s javascript grows, it can be easy to lose ourselves, buried under thousands of lines of js code. That&#8217;s why I like to create namespaces to keep everything together. </p>
<p>Let&#8217;s say I have functions to work with cats and I create a namespace for it because there are lots of other functions related to dogs.</p>
<p><script src="https://gist.github.com/1195071.js"> </script></p>
<p>As you see, to access the cat list I must use the complete name including the namespaces like this : <strong>Animal.Cat.list</strong>. You can guess that it will get pretty long as the code grows.</p>
<p>Let&#8217;s make this call shorter.</p>
<p><script src="https://gist.github.com/1195073.js"></script></p>
<p>Now, I only have to write <strong>c.list</strong> because <em>c</em> is an alias of the <em>Animal.Cat</em> namespace. Thanks to <a href="http://www.javascriptkata.com/2011/08/22/self-invoking-functions-explained-line-by-line/" title="Self-invoking functions explained line by line">self-invocation</a>!</p>
<p>You probably noticed that the code is longer but this is just an example. In a real situation, this technique would make your code much clearer.</p>
<p>Don&#8217;t forget to <a href="http://eepurl.com/fmsZA">subscribe to my newsletter</a>!</p>
]]></content:encoded>
			<wfw:commentRss>http://www.javascriptkata.com/2011/09/05/creating-namespaces-with-self-invoking-functions/feed/</wfw:commentRss>
		<slash:comments>10</slash:comments>
		</item>
		<item>
		<title>Self-invoking functions explained line by line</title>
		<link>http://www.javascriptkata.com/2011/08/22/self-invoking-functions-explained-line-by-line/?utm_source=rss&#038;utm_medium=rss&#038;utm_campaign=self-invoking-functions-explained-line-by-line</link>
		<comments>http://www.javascriptkata.com/2011/08/22/self-invoking-functions-explained-line-by-line/#comments</comments>
		<pubDate>Tue, 23 Aug 2011 02:16:04 +0000</pubDate>
		<dc:creator>dsimard</dc:creator>
				<category><![CDATA[black belt]]></category>

		<guid isPermaLink="false">http://www.javascriptkata.com/?p=343</guid>
		<description><![CDATA[I never knew it was possible to love a syntax but since, I fell in love with the self-invoking function which can be summarized like this : (function(){})(). Yes this is a valid syntax though it doesn&#8217;t do much in this form (it creates a function that does nothing and call it). Some people call [...]]]></description>
			<content:encoded><![CDATA[<p>I never knew it was possible to love a syntax but since, I fell in love with the <strong>self-invoking function</strong> which can be summarized like this : <em>(function(){})()</em>. Yes this is a valid syntax though it doesn&#8217;t do much in this form (it creates a function that does nothing and call it).</p>
<p>Some people call it <em>self-invocation</em> or <em>self-executing</em> but I don&#8217;t think it has an official name.</p>
<h3>Let&#8217;s break it down</h3>
<p>It will become clearer if I execute some code and write it on more than one line :</p>
<div class="dean_ch" style="white-space: wrap;">
<ol>
<li class="li1">
<div class="de1">(
</div>
</li>
<li class="li1">
<div class="de1">&nbsp; function() {
</div>
</li>
<li class="li1">
<div class="de1">&nbsp; &nbsp; console.log(&quot;this line is called&quot;);
</div>
</li>
<li class="li1">
<div class="de1">&nbsp; }
</div>
</li>
<li class="li2">
<div class="de2">)
</div>
</li>
<li class="li1">
<div class="de1">();</div>
</li>
</ol>
</div>
<p><strong>Line 1 : (</strong><br />
This first parenthesis is used as <em>grouping operator</em>. Read more <a href="http://kangax.github.com/nfe/" target="_blank">here</a> and <a href="http://rx4ajax-jscore.com/ecmacore/more/express.html#group" target="_blank">here</a>.</p>
<p><strong>Line 2 : function() {</strong><br />
This create an anonymous function. I could write something like <strong>function doSomething() {</strong> but in the case of a self-invoking function, this is useless because the function is in its own little scope (line 1) and can&#8217;t be called from outside of it.</p>
<p><strong>Line 3 : console.log(&#8220;this line is called&#8221;);</strong><br />
This is the code that will be invoked.</p>
<p><strong>Line 4 : }</strong><br />
This clauses the function opened at line 2.</p>
<p><strong>Line 5 : )</strong><br />
Clauses the parens opened at line 1.</p>
<p><strong>Line 6 : ();</strong><br />
We call the function created at line 2 and returned within the scope from line 1 to 4.</p>
<p>Let&#8217;s write it in a more compact syntax :</p>
<div class="dean_ch" style="white-space: wrap;">
<ol>
<li class="li1">
<div class="de1">(function() {
</div>
</li>
<li class="li1">
<div class="de1">&nbsp; console.log(&quot;this line is called&quot;);
</div>
</li>
<li class="li1">
<div class="de1">})()</div>
</li>
</ol>
</div>
<p>See my post about <a href="http://www.javascriptkata.com/2011/09/05/creating-namespaces-with-self-invoking-functions/" title="Creating namespaces with self-invoking functions">using self-invoking functions to create namespaces</a>.</p>
<p>Don&#8217;t forget to <a href="http://eepurl.com/fmsZA">subscribe to my newsletter</a>!</p>
]]></content:encoded>
			<wfw:commentRss>http://www.javascriptkata.com/2011/08/22/self-invoking-functions-explained-line-by-line/feed/</wfw:commentRss>
		<slash:comments>11</slash:comments>
		</item>
		<item>
		<title>Ready.js – continuous integration using jslint, nodejs and git</title>
		<link>http://www.javascriptkata.com/2010/10/28/ready-js-prepare-your-javascript-for-production/?utm_source=rss&#038;utm_medium=rss&#038;utm_campaign=ready-js-prepare-your-javascript-for-production</link>
		<comments>http://www.javascriptkata.com/2010/10/28/ready-js-prepare-your-javascript-for-production/#comments</comments>
		<pubDate>Thu, 28 Oct 2010 20:33:17 +0000</pubDate>
		<dc:creator>dsimard</dc:creator>
				<category><![CDATA[jskata]]></category>
		<category><![CDATA[ready.js]]></category>

		<guid isPermaLink="false">http://www.javascriptkata.com/?p=310</guid>
		<description><![CDATA[Ready.js is a tool to prepare your javascript files for production.]]></description>
			<content:encoded><![CDATA[<p><a href="http://github.com/dsimard/ready.js" target="_blank">Ready.js</a> is a tool to prepare your javascript files for production. It can be used in every type of web project (Rails, Django, node, etc). It does four things :</p>
<ul>
<li>Check if your javascript are valid with jslint.</li>
<li>Minify your javascript with Closure Compiler (optimize and minify your code).</li>
<li>Watch your javascript files for jslint while you&#8217;re coding.</li>
<li>Create an aggregated file of all your javascripts.</li>
</ul>
<p>Ready.js is written in <a href="http://nodejs.org/" target="_blank">node.js</a> and the source code is <a href="http://github.com/dsimard/ready.js" target="_blank">available on github</a>.</p>
<h3>How to install it</h3>
<p>First, all you need it to install git and <a href="http://nodejs.org/#build" target="_blank">node.js</a>. Then, you do the following :</p>
<ol>
<li>run <code>git submodule add git://github.com/dsimard/ready.js.git ready.js</code>
</li>
<li>run <code>cd ready.js &amp;&amp; git submodule init &amp;&amp; git submodule update &amp;&amp; cd ..</code>
</li>
<li>
<p>Create config file in <em>your_project/ready.conf.js</em> :</p>
<pre><code> { src : "./javascripts", // the source dir of js files
   dest : "./minified", // the destination of your minified files
   minifiedExtension : "min", // Extension of the minified file
   runGCompiler : true, // if should run GoogleCompiler
   aggregateTo : "./minified/all.js" // Which file to aggregate all javascript files
 }
</code></pre>
</li>
<li>
<p>run <code>echo 'node ready.js/ready.js ready.conf.js' &gt;&gt; .git/hooks/pre-commit</code></p>
</li>
</ol>
<p>Then, <strong>every time you commit</strong>, ready.js will be run. You can see alternative installations on the <a href="http://github.com/dsimard/ready.js" target="_blank">github page</a>.</p>
<h3>Why this tool?</h3>
<p>First, I wanted to write something in node.js. Javascript is a long time favorite of mine and I&#8217;m really excited about node. Second, I wanted to have a build tool for <a href="http://github.com/dsimard/jskata" target="_blank">jsKata</a> and I hated all of them. Third, I don&#8217;t like the <em>&#8220;cache&#8221;</em> principle in Rails and wanted to have something else.</p>
<p><strong>Try <a href="http://github.com/dsimard/jskata">Ready.js</a> today!</strong></p>
]]></content:encoded>
			<wfw:commentRss>http://www.javascriptkata.com/2010/10/28/ready-js-prepare-your-javascript-for-production/feed/</wfw:commentRss>
		<slash:comments>17</slash:comments>
		</item>
		<item>
		<title>Alternatives to singletons in javascript</title>
		<link>http://www.javascriptkata.com/2010/10/20/alternatives-to-singletons-in-javascript/?utm_source=rss&#038;utm_medium=rss&#038;utm_campaign=alternatives-to-singletons-in-javascript</link>
		<comments>http://www.javascriptkata.com/2010/10/20/alternatives-to-singletons-in-javascript/#comments</comments>
		<pubDate>Wed, 20 Oct 2010 12:00:25 +0000</pubDate>
		<dc:creator>dsimard</dc:creator>
				<category><![CDATA[jskata]]></category>
		<category><![CDATA[libraries]]></category>

		<guid isPermaLink="false">http://www.javascriptkata.com/?p=302</guid>
		<description><![CDATA[Singletons are quite popular on this blog. A lot of people are coming here from that search. In a sense, I&#8217;m happy to have such a considerable traffic from google. On the other hand, I&#8217;m not proud that this post is my most popular. Why? Because I don&#8217;t believe in singletons in javascript. This concept [...]]]></description>
			<content:encoded><![CDATA[<p>Singletons are quite popular on this blog. A lot of people are coming here from that search. In a sense, I&#8217;m happy to have such a considerable traffic from google. On the other hand, I&#8217;m not proud that this post is my most popular. Why? Because <strong>I don&#8217;t believe in singletons in javascript</strong>. This concept belongs to more object-oriented languages, even though we can have an half-baked OO in javascript. Singletons are just too much of a hassle compared to a simpler solution.</p>
<h3>What is a singleton?</h3>
<p>A singleton is a <a href="http://en.wikipedia.org/wiki/Singleton_pattern" target="_blank">design pattern</a>. When instanciating a singleton class, it will always return the same instance of the class. You can&#8217;t create more than one instance of the class no matter how many time you call the &#8220;new&#8221; operator.</p>
<p>Some people think that singleton shouldn&#8217;t be a pattern. Personally, I never use it in javascript because they are not &#8220;javascript-friendly&#8221; (in fact, the whole concept of OO is not javascript-friendly). Here&#8217;s what I do.</p>
<h3>The alternatives</h3>
<p>Just between you and me, when we want to use a singleton, it&#8217;s probably because we want to encapsulate a certain part of the code to be easier to read and understand. </p>
<h3>Alternative #1 : simple and clean</h3>
<p>The easiest way of encapsulating functionalities is to create an object. Sure, it&#8217;s &#8220;untyped&#8221; but types don&#8217;t mean a lot in a dynamic languge like javascript. You have to learn to work with it. Here&#8217;s how I do it (<a href="http://gist.github.com/635109" target="_blank">view on github</a>).</p>
<p><script src="http://gist.github.com/635109.js?file=Simple%20singleton.js"></script></p>
<h3>Alternative #2 : more complex</h3>
<p>Let&#8217;s say you have a big project and you want to really separate every functionalities in &#8220;namespaces&#8221;, it would be easier to use a more complex technique.</p>
<p>You can take a look at <a href="http://github.com/dsimard/jskata/tree/master/src/" target="_blank">jsKata</a> where every library I wrote has its own namespace (jsKata.undo or jsKata.timezone). jQuery is also using this technique (<a href="http://github.com/jquery/jquery/blob/master/src/core.js" target="_blank">source code</a>).</p>
<p>Let&#8217;s say you have a 2 levels deep namespace but you use the alternative #1 as described ealier. You would end up with this (<a href="http://gist.github.com/635138" target="_blank">view on github</a>).</p>
<p><script src="http://gist.github.com/635138.js?file=2%20levels%20deep.js"></script></p>
<p>As you can see at line #6, the reference to your <em>msg</em> variable is pretty long. If you have a lot of code, it would become redundant.</p>
<p>If you want to keep your code readable, it&#8217;s a good idea to use a self-invoking function like this (<a href="http://gist.github.com/634414" target="_blank">see on github</a>).</p>
<p><script src="http://gist.github.com/634414.js?file=singletons%20in%20javascript.js"></script></p>
<p>Let&#8217;s study it line by line.</p>
<p><strong>var instance = (function() {<br />
  //&#8230; code is here&#8230;<br />
})();<br />
</strong></p>
<p>This is the self-invoking part of the technique. You see that <em>function(){}</em> is in parenthesis. So the result of the parenthesis is a <em>function</em> object (and not the result of the function). After these parenthesis, there&#8217;s an empty set of parenthesis. It means that the object function in parenthesis is called.</p>
<p><strong>var i = {<br />
  // &#8230; more code &#8230;<br />
}</strong></p>
<p>This creates a scope. At this moment, the <em>i</em> variable is used as a shorthand for <em>myApplication.instance</em>.</p>
<p><strong>hello : function() {<br />
  alert(i.msg);<br />
}<br />
</strong></p>
<p>As you can see, we can use <em>i</em> in the <em>hello</em> function to get the <em>msg</em> variable.</p>
<p><strong>return i;</strong></p>
<p>Finally, by returning <em>i</em> as the result of the function call, it gets assigned to <em>myApplication.instance</em>.</p>
<h3>No more singletons please</h3>
<p>When you know these techniques, I can&#8217;t see any reason of using singletons anymore. Forget about using javascript as if it was a complete object-oriented language and start using it as it is.</p>
]]></content:encoded>
			<wfw:commentRss>http://www.javascriptkata.com/2010/10/20/alternatives-to-singletons-in-javascript/feed/</wfw:commentRss>
		<slash:comments>17</slash:comments>
		</item>
		<item>
		<title>Auto-detect timezones, time zone offsets and daylight saving in javascript</title>
		<link>http://www.javascriptkata.com/2010/10/14/auto-detect-timezones-time-zone-offsets-and-daylight-saving-in-javascript/?utm_source=rss&#038;utm_medium=rss&#038;utm_campaign=auto-detect-timezones-time-zone-offsets-and-daylight-saving-in-javascript</link>
		<comments>http://www.javascriptkata.com/2010/10/14/auto-detect-timezones-time-zone-offsets-and-daylight-saving-in-javascript/#comments</comments>
		<pubDate>Thu, 14 Oct 2010 18:57:44 +0000</pubDate>
		<dc:creator>dsimard</dc:creator>
				<category><![CDATA[jskata]]></category>

		<guid isPermaLink="false">http://www.javascriptkata.com/?p=296</guid>
		<description><![CDATA[I had to work with timezone on the project I&#8217;m working on and I hate it. So I created jsKata.timezone to deal with them (view documentation). How does it work? In short, this library doesn&#8217;t do a lot but saves you a lot of troubles. It returns the standard time offset of the user&#8217;s browser. [...]]]></description>
			<content:encoded><![CDATA[<p>I had to work with timezone on the project I&#8217;m working on and <a href="http://timetrackingim.posterous.com/timezones-are-a-pain-in-the-butt">I hate it</a>. So I created <a href="http://github.com/dsimard/jskata/blob/master/src/jskata.timezone.js" target="_blank">jsKata.timezone</a> to deal with them (view <a href="http://github.com/dsimard/jskata/wiki/jskata.timezone">documentation</a>).</p>
<h3>How does it work?</h3>
<p>In short, this library doesn&#8217;t do a lot but saves you a lot of troubles. It returns the standard time offset of the user&#8217;s browser. From there, you can narrow the list of possible time zones for a user. To have the standard timezone offset simple call :</p>
<div class="dean_ch" style="white-space: wrap;">
<ol>
<li class="li1">
<div class="de1"><span class="kw2">var</span> standardOffset = jsKata.<span class="me1">timezone</span>.<span class="me1">st</span><span class="br0">&#40;</span><span class="br0">&#41;</span>;</div>
</li>
<li class="li1">
<div class="de1"><span class="coMULTI">/* or you can also call a shorter version */</span> </div>
</li>
<li class="li1">
<div class="de1">standardOffset = jsk.<span class="me1">tz</span>.<span class="me1">st</span><span class="br0">&#40;</span><span class="br0">&#41;</span>;</div>
</li>
</ol>
</div>
<p>You can see a <a href="http://dsimard.github.com/jskata/timezone.html" target="_blank">demo</a>.</p>
<h3>Other functions</h3>
<div class="dean_ch" style="white-space: wrap;">
<ol>
<li class="li1">
<div class="de1"><span class="co1">// Return the daylight saving time offset</span></div>
</li>
<li class="li1">
<div class="de1"><span class="kw2">var</span> daylightSavingOffset = jsKata.<span class="me1">timezone</span>.<span class="me1">dst</span><span class="br0">&#40;</span><span class="br0">&#41;</span>;</div>
</li>
<li class="li1">
<div class="de1">&nbsp;</div>
</li>
<li class="li1">
<div class="de1"><span class="co1">// If the time zone has daylight saving</span></div>
</li>
<li class="li2">
<div class="de2">jsk.<span class="me1">tz</span>.<span class="me1">hasDst</span><span class="br0">&#40;</span><span class="br0">&#41;</span>;</div>
</li>
<li class="li1">
<div class="de1">&nbsp;</div>
</li>
<li class="li1">
<div class="de1"><span class="co1">// Return the standard time zone as a string (ex : -0500)</span></div>
</li>
<li class="li1">
<div class="de1">jsk.<span class="me1">tz</span>.<span class="me1">stToString</span><span class="br0">&#40;</span><span class="br0">&#41;</span>;</div>
</li>
<li class="li1">
<div class="de1">&nbsp;</div>
</li>
<li class="li2">
<div class="de2"><span class="co1">// Return the daylight saving time zone as a string (ex : -0400)</span></div>
</li>
<li class="li1">
<div class="de1">jsk.<span class="me1">tz</span>.<span class="me1">dstToString</span><span class="br0">&#40;</span><span class="br0">&#41;</span>;</div>
</li>
</ol>
</div>
<h3>Under the hood</h3>
<p>You can&#8217;t have a named time zone in javascript (example : eastern time or central time), you can only have a time zone offset which is represented by universal time (UTC) minus the distance in minutes to it by calling <em>dateVariable.getTimezoneOffset()</em>. It means that if the time zone offset is -1 hours of UTC, javascript will give you 60. Why is it inverted in javascript? I have no idea.</p>
<p>In winter, it&#8217;s always <strong>standard</strong> time. In summer, it&#8217;s <strong>daylight saving</strong> time which is standard time minus 60 minutes&#8230; but not for every country. Plus, summer and winter are inverted in the southern hemisphere. That&#8217;s a lot of exceptions and that&#8217;s why I created the <a href="http://github.com/dsimard/jskata/blob/v0.3/src/jskata.timezone.js">jsKata.timezone library</a>. </p>
]]></content:encoded>
			<wfw:commentRss>http://www.javascriptkata.com/2010/10/14/auto-detect-timezones-time-zone-offsets-and-daylight-saving-in-javascript/feed/</wfw:commentRss>
		<slash:comments>15</slash:comments>
		</item>
		<item>
		<title>NoFreeze : a library that avoids freezing in javascript</title>
		<link>http://www.javascriptkata.com/2010/08/10/nofreeze-a-library-that-avoids-freezing-in-javascript/?utm_source=rss&#038;utm_medium=rss&#038;utm_campaign=nofreeze-a-library-that-avoids-freezing-in-javascript</link>
		<comments>http://www.javascriptkata.com/2010/08/10/nofreeze-a-library-that-avoids-freezing-in-javascript/#comments</comments>
		<pubDate>Tue, 10 Aug 2010 19:47:40 +0000</pubDate>
		<dc:creator>dsimard</dc:creator>
				<category><![CDATA[black belt]]></category>
		<category><![CDATA[jskata]]></category>
		<category><![CDATA[libraries]]></category>

		<guid isPermaLink="false">http://www.javascriptkata.com/?p=274</guid>
		<description><![CDATA[The <a href="http://support.mozilla.com/en-us/kb/Warning+Unresponsive+script" target="_blank">unresponsive warning</a> happens a couple of time a year but every time, it's frustrating. It prompts the message "<em>A script on this page may be busy, or it may have stopped responding. You can stop the script now, or you can continue to see if the script will complete</em>".]]></description>
			<content:encoded><![CDATA[<p>The <a href="http://support.mozilla.com/en-us/kb/Warning+Unresponsive+script" target="_blank">unresponsive warning</a> happens a couple of time a year but every time, it&#8217;s frustrating. It prompts the message &#8220;<em>A script on this page may be busy, or it may have stopped responding. You can stop the script now, or you can continue to see if the script will complete</em>&#8220;.</p>
<p>I <a href="http://www.javascriptkata.com/2010/06/15/announcing-the-jskata-libraries/">launched jskata</a> not a long time ago and mostly talked about the <a href="http://dsimard.github.com/jskata/undo.html">undo</a> feature. Now, I will talk about the jskata <strong>NoFreeze</strong> library to <strong>avoid freezing by splitting long processes into smaller ones</strong>.</p>
<p>You can look at the <a href="http://dsimard.github.com/jskata/nofreeze.html" target="_blank">demo</a>, the <a href="http://github.com/dsimard/jskata/blob/master/doc/jskata.nofreeze.textile#readme" target="_blank">documentation</a> or the <a href="http://github.com/dsimard/jskata/blob/master/src/jskata.nofreeze.js" target="_blank">source</a>.</p>
<h3>Old plain loop</h3>
<p>This script loops the <em>index</em> variable from 0 to 1000000 and writes the current value in <em>document.title</em>. Unfortunately, it will freeze the page until the whole process is done.</p>
<div class="dean_ch" style="white-space: wrap;">
<ol>
<li class="li1">
<div class="de1"><span class="kw2">var</span> index = <span class="nu0">0</span>; <span class="co1">// index</span></div>
</li>
<li class="li1">
<div class="de1"><span class="kw1">for</span><span class="br0">&#40;</span>index = <span class="nu0">0</span>; index &lt;= <span class="nu0">1000000</span>; index++<span class="br0">&#41;</span> <span class="br0">&#123;</span></div>
</li>
<li class="li1">
<div class="de1">&nbsp; &nbsp; document.<span class="me1">title</span> = index;</div>
</li>
<li class="li1">
<div class="de1"><span class="br0">&#125;</span></div>
</li>
</ol>
</div>
<h3>How to write a responsive for</h3>
<p>I&#8217;m using <a href="http://github.com/dsimard/jskata/blob/master/src/jskata.nofreeze.js" target="_blank">jsKata.nofreeze</a> library to avoid the freeze. This script will do the same as above. But, the <strong>page will remain responsive</strong> during the whole process.</p>
<div class="dean_ch" style="white-space: wrap;">
<ol>
<li class="li1">
<div class="de1"><span class="kw2">var</span> index = <span class="nu0">0</span>; <span class="co1">// index</span></div>
</li>
<li class="li1">
<div class="de1">jsKata.<span class="me1">nofreeze</span>.<span class="me1">forloop</span><span class="br0">&#40;</span></div>
</li>
<li class="li1">
<div class="de1">&nbsp; &nbsp; <span class="co1">// the condition</span></div>
</li>
<li class="li1">
<div class="de1">&nbsp; &nbsp; <span class="kw2">function</span><span class="br0">&#40;</span><span class="br0">&#41;</span> <span class="br0">&#123;</span> <span class="kw1">return</span> index &lt;= <span class="nu0">1000000</span>; &nbsp;<span class="br0">&#125;</span>, </div>
</li>
<li class="li2">
<div class="de2">&nbsp; &nbsp; <span class="co1">// the incrementor</span></div>
</li>
<li class="li1">
<div class="de1">&nbsp; &nbsp; <span class="kw2">function</span><span class="br0">&#40;</span><span class="br0">&#41;</span> <span class="br0">&#123;</span> index++; <span class="br0">&#125;</span>,</div>
</li>
<li class="li1">
<div class="de1">&nbsp; &nbsp; <span class="co1">// this is what will be executed</span></div>
</li>
<li class="li1">
<div class="de1">&nbsp; &nbsp; <span class="kw2">function</span> fct<span class="br0">&#40;</span><span class="br0">&#41;</span> <span class="br0">&#123;</span></div>
</li>
<li class="li1">
<div class="de1">&nbsp; &nbsp; &nbsp; &nbsp; document.<span class="me1">title</span> = index;</div>
</li>
<li class="li2">
<div class="de2">&nbsp; &nbsp; <span class="br0">&#125;</span></div>
</li>
<li class="li1">
<div class="de1"><span class="br0">&#41;</span>;</div>
</li>
</ol>
</div>
<p>It uses <a href="http://www.javascriptkata.com/2007/04/10/the-power-of-closures-in-javascript/">closures</a> to keep a resemblance to a good ol&#8217; <em>for</em> loop.</p>
<h3>Other loops : infinite and each</h3>
<p>There are two other loops available. <strong>Infinite</strong> will loop indefinitely until you stop it. <strong>Each</strong> will loop through the properties of an object and loop in each one.</p>
<p>Take a look at the <a href="http://github.com/dsimard/jskata/blob/master/doc/jskata.nofreeze.textile" target="_blank">documentation</a> to know more about them.</p>
<h3>How to stop a process</h3>
<p>As you see, every functions (forloop, infinite and each) return an object containing a <em>stop</em> function. So to stop a single process, just call it.</p>
<div class="dean_ch" style="white-space: wrap;">
<ol>
<li class="li1">
<div class="de1"><span class="kw2">var</span> loop = jsKata.<span class="me1">nofreeze</span>.<span class="me1">infinite</span><span class="br0">&#40;</span><span class="kw2">function</span><span class="br0">&#40;</span><span class="br0">&#41;</span> <span class="br0">&#123;</span><span class="co1">//do nothing});</span></div>
</li>
<li class="li1">
<div class="de1">loop.<span class="kw3">stop</span><span class="br0">&#40;</span><span class="br0">&#41;</span>;</div>
</li>
</ol>
</div>
<p>If you want to <strong>stop all the processes of a page</strong>, make this call :</p>
<div class="dean_ch" style="white-space: wrap;">
<ol>
<li class="li1">
<div class="de1">jsKata.<span class="me1">nofreeze</span>.<span class="kw3">stop</span><span class="br0">&#40;</span><span class="br0">&#41;</span>;</div>
</li>
</ol>
</div>
<h3>More to come&#8230;</h3>
<p>There&#8217;s much more you can do with that library but I wanted to keep it simple for this post. Next, I&#8217;ll talk about multi-process and other kinds of loop.</p>
]]></content:encoded>
			<wfw:commentRss>http://www.javascriptkata.com/2010/08/10/nofreeze-a-library-that-avoids-freezing-in-javascript/feed/</wfw:commentRss>
		<slash:comments>17</slash:comments>
		</item>
		<item>
		<title>Announcing the jsKata libraries</title>
		<link>http://www.javascriptkata.com/2010/06/15/announcing-the-jskata-libraries/?utm_source=rss&#038;utm_medium=rss&#038;utm_campaign=announcing-the-jskata-libraries</link>
		<comments>http://www.javascriptkata.com/2010/06/15/announcing-the-jskata-libraries/#comments</comments>
		<pubDate>Tue, 15 Jun 2010 12:00:56 +0000</pubDate>
		<dc:creator>dsimard</dc:creator>
				<category><![CDATA[Uncategorized]]></category>

		<guid isPermaLink="false">http://www.javascriptkata.com/?p=253</guid>
		<description><![CDATA[I&#8217;m officially announcing the jsKata librairies. Why? I just wanted to write some public code and share it with others. Javascript has a lot of pains in the butt and jQuery is not the answer to every problem. jsKata is the answer to some of the problems that I face everyday while writing javascript. A [...]]]></description>
			<content:encoded><![CDATA[<p>I&#8217;m officially announcing the <a href="http://dsimard.github.com/jskata/" target="_blank">jsKata librairies</a>.</p>
<h3>Why?</h3>
<p>I just wanted to write some public code and share it with others. Javascript has a lot of pains in the butt and jQuery is not the answer to every problem. <em>jsKata</em> is the answer to some of the problems that I face everyday while writing javascript.</p>
<h3>A manifesto?</h3>
<p>I wrote a manifesto because it helps me focusing on what and how I want to achieve with <em>jsKata</em>. This is the manifesto of <a href="http://github.com/dsimard/jskata/blob/v0.2/README.textile">version 0.2</a>.</p>
<ul>
<li><strong>No internal dependence</strong> : every library can be used independently “as is”.</li>
<li><strong>No external dependence</strong> : don’t depend on external libraries.</li>
<li><strong>Everything is public</strong> : you know what you’re doing</li>
<li><strong>Avoid objects</strong> : use closures</li>
<li><strong>No unnecessary validation</strong> : if something goes wrong, an error will pop</li>
<li><strong>No error catching</strong> : if an error pop, it goes all the way up</li>
<li><strong>No DOM</strong> : jQuery already exists</li>
<li><strong>No plugins</strong> : if a developer wants to add something, he will find a way around</li>
<li><strong>Write good documentation</strong> : document as I code</li>
<li><strong>Promote</strong> : a good library is nothing without users</li>
</ul>
<h3>What are the libraries available?</h3>
<p>I don&#8217;t consider <em>jsKata</em> to be a library but more a set of libraries because each one can be used independently (see #1 in manifesto). For the moment, there are two librairies.</p>
<p><strong>Undo &#038; redo</strong><br />
I wrote about <a href="http://www.javascriptkata.com/2010/03/29/how-to-write-an-simple-undo-system-for-your-app/">undo</a> and <a href="http://www.javascriptkata.com/2010/04/26/an-undoredo-library-for-your-app/">undo &#038; redo</a> before. I took the code and put it in <em>jsKata</em>. You can look at the <a href="http://github.com/dsimard/jskata/blob/master/src/jskata.undo.js" target="_blank">code</a> or try the <a href="http://dsimard.github.com/jskata/undo.html">demo</a>.</p>
<p><strong>No freeze</strong><br />
This librairy is to avoid unresponsive script warning when you have a really long loop. It cuts a loop to create digestible chunks without a warning. I&#8217;ll write more about this one in the future but meanwhile, you can <a href="http://github.com/dsimard/jskata/blob/master/src/jskata.nofreeze.js">look at the code</a> or try the <a href="http://dsimard.github.com/jskata/nofreeze.html" target="_blank">demo</a>.</p>
<h3>GitHub</h3>
<p>All the code is <a href="http://github.com/dsimard/jskata">hosted on GitHub</a>. I hope you will enjoy it!</p>
]]></content:encoded>
			<wfw:commentRss>http://www.javascriptkata.com/2010/06/15/announcing-the-jskata-libraries/feed/</wfw:commentRss>
		<slash:comments>5</slash:comments>
		</item>
		<item>
		<title>How to de-anonymize your anonymous functions</title>
		<link>http://www.javascriptkata.com/2010/05/19/how-to-de-anonymize-your-anonymous-functions/?utm_source=rss&#038;utm_medium=rss&#038;utm_campaign=how-to-de-anonymize-your-anonymous-functions</link>
		<comments>http://www.javascriptkata.com/2010/05/19/how-to-de-anonymize-your-anonymous-functions/#comments</comments>
		<pubDate>Wed, 19 May 2010 17:31:52 +0000</pubDate>
		<dc:creator>dsimard</dc:creator>
				<category><![CDATA[black belt]]></category>
		<category><![CDATA[function]]></category>
		<category><![CDATA[how to]]></category>

		<guid isPermaLink="false">http://www.javascriptkata.com/?p=246</guid>
		<description><![CDATA[First of all, de-anonymized functions are called <strong>named functions</strong> and they look a lot like "regular" functions (in fact, they are).]]></description>
			<content:encoded><![CDATA[<p>First of all, de-anonymized functions are called <strong>named functions</strong> and they look a lot like &#8220;regular&#8221; functions (in fact, they are).</p>
<h3>Why should I de-anonymize?</h3>
<p>For <strong>debugging</strong>. Instead of having a stack trace filled with <strong>?()</strong> (representing an anonymous function), it is nicely printed and easier to follow.</p>
<p>For <strong>self-reference</strong>. Example, when you want an anonymous function to recall itself recursively, you would have to call <a href="https://developer.mozilla.org/En/Core_JavaScript_1.5_Reference/Functions_and_function_scope/Arguments/Callee" target="_blank">arguments.callee</a>. It&#8217;s odd <del datetime="2010-05-20T14:23:32+00:00">and it&#8217;s <strong>deprecated</strong> anyway</del> (people pointed to me that it&#8217;s not really deprecated but it throws an error in <a href="http://ejohn.org/blog/ecmascript-5-strict-mode-json-and-more/">ECMAScript 5 strict</a>).</p>
<h3>Stack trace of anonymous functions&#8230;</h3>
<p>If you run this in your <a href="http://getfirebug.com">firebug console</a> (every javascripter should have firebug).</p>
<p><script src="http://gist.github.com/406349.js?file=anonymous+functions.js"></script></p>
<p>You get a stack trace that looks like this :<br />
func()<br />
func()<br />
func()</p>
<h3>&#8230; versus stack trace of named functions</h3>
<p><script src="http://gist.github.com/406354.js?file=named+functions.js"></script></p>
<p>You get a stack trace that looks like this :<br />
f1()<br />
f2()<br />
f3()</p>
<p>As you can see, it&#8217;s a lot easier to debug and to understand who&#8217;s calling who.</p>
<p><strong>Warning</strong> : There&#8217;s a <a href="http://yura.thinkweb2.com/named-function-expressions/#jscript-bugs">bug in IE</a> with named functions.</p>
<h3>Recursive anonymous function</h3>
<p>Let&#8217;s create a recursive anonymous function that counts to 10.</p>
<p><script src="http://gist.github.com/406360.js?file=recursive+count+to+10+anonymous.js"></script></p>
<p>And let&#8217;s create it but <strong>using a named function</strong>.</p>
<p><script src="http://gist.github.com/406362.js?file=recursive+count+to+10+with+named+functions.js"></script></p>
<p>Once again, the code is much clearer and it&#8217;s not using a deprecated element.</p>
<h3>A note about closures to advanced javascripters</h3>
<p>I just wanted to say that in the last example, you could have written this :<br />
<script src="http://gist.github.com/406379.js?file=CountTo10closure.js"></script></p>
<p>As you can see at <strong>line 4</strong>, I call the <em>count</em> function which is not the name of the function but the variable to which the function is assigned. It works, but it may not be the best thing to do. You can learn more about closure on that <a href="http://www.javascriptkata.com/2007/04/10/the-power-of-closures-in-javascript/">previous post</a> and I&#8217;ll write more about it.</p>
]]></content:encoded>
			<wfw:commentRss>http://www.javascriptkata.com/2010/05/19/how-to-de-anonymize-your-anonymous-functions/feed/</wfw:commentRss>
		<slash:comments>7</slash:comments>
		</item>
	</channel>
</rss>

