<?xml version="1.0" encoding="UTF-8"?>
<?xml-stylesheet type="text/xsl" media="screen" href="/~d/styles/rss2full.xsl"?><?xml-stylesheet type="text/css" media="screen" href="http://feeds.feedburner.com/~d/styles/itemcontent.css"?><rss 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/" xmlns:feedburner="http://rssnamespace.org/feedburner/ext/1.0" version="2.0">

<channel>
	<title>JavascriptKata</title>
	
	<link>http://www.javascriptkata.com</link>
	<description>helping you with javascript since 2007</description>
	<lastBuildDate>Wed, 23 May 2012 15:04:44 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.2.1</generator>
		<atom10:link xmlns:atom10="http://www.w3.org/2005/Atom" rel="self" type="application/rss+xml" href="http://feeds.feedburner.com/JavascriptKata" /><feedburner:info uri="javascriptkata" /><atom10:link xmlns:atom10="http://www.w3.org/2005/Atom" rel="hub" href="http://pubsubhubbub.appspot.com/" /><image><link>http://www.javascriptkata.com</link><url>http://www.javascriptkata.com/wp-content/themes/javascriptkata1.3/images/jk_logo_small.png</url><title>Javascript Kata</title></image><feedburner:emailServiceId>JavascriptKata</feedburner:emailServiceId><feedburner:feedburnerHostname>http://feedburner.google.com</feedburner:feedburnerHostname><item>
		<title>Stop Javascript,  use Coffeescript</title>
		<link>http://feedproxy.google.com/~r/JavascriptKata/~3/z1K48QTW9gg/</link>
		<comments>http://www.javascriptkata.com/2012/05/22/stop-javascript-use-coffeescript/#comments</comments>
		<pubDate>Tue, 22 May 2012 14:49:48 +0000</pubDate>
		<dc:creator>dsimard</dc:creator>
				<category><![CDATA[coffeescript]]></category>
		<category><![CDATA[white belt]]></category>

		<guid isPermaLink="false">http://www.javascriptkata.com/?p=430</guid>
		<description><![CDATA[I write this blog since 2007, my first post (which I&#8217;m not too proud of after 5 years) was written on March 19, 2007. In those 5 years, a lot happened. Personally, I have burst periods when I write posts for 2 weeks then I disappear. On the web, there are two things that turned [...]]]></description>
			<content:encoded><![CDATA[
<p><a href="http://feedads.g.doubleclick.net/~a/lw8v9MyQZn1-GWv7Nm3pjvUjljc/0/da"><img src="http://feedads.g.doubleclick.net/~a/lw8v9MyQZn1-GWv7Nm3pjvUjljc/0/di" border="0" ismap="true"></img></a><br/>
<a href="http://feedads.g.doubleclick.net/~a/lw8v9MyQZn1-GWv7Nm3pjvUjljc/1/da"><img src="http://feedads.g.doubleclick.net/~a/lw8v9MyQZn1-GWv7Nm3pjvUjljc/1/di" border="0" ismap="true"></img></a></p><p>I write this blog since 2007, my <a href="http://www.javascriptkata.com/2007/03/19/5-reasons-to-write-object-oriented-oo-javascript/" title="5 reasons to write object-oriented (oo) javascript">first post</a> (which I&#8217;m not too proud of after 5 years) was written on March 19, 2007. In those 5 years, a lot happened. Personally, I have burst periods when I write posts for 2 weeks then I disappear. On the web, there are two things that turned the javascript world upside-down :</p>
<ol>
<li><a href="http://nodejs.org/" target="_blank">Node.js</a></li>
<li><a href="http://coffeescript.org/" target="_blank">Coffeescript</a></li>
</ol>
<p>Everyone agrees on the former, nobody agrees on the latter.</p>
<p>After years of javascript, <strong>I took the decision to go 99.9% coffeescript and avoid javascript</strong> as much as I can. Here are 3 to 5 reasons why I took this decision :</p>
<h3>Coffeescript is idented</h3>
<p>My first relationship with an indented language was with COBOL in 1998. I was deeply into Delphi/Pascal at this time this identation simply didn&#8217;t make sense. The first time I wrote HTML, it wasn&#8217;t indented. It didn&#8217;t take long before I was completely lost in a maze of opening and closing tags.</p>
<p>So one day, I took the decision to always indent my files the same way, even if I had no obligation to. In fact, I think we all took that decision because I never see unindented languages being unindented. It would be suicide.</p>
<p>It ends up that <strong>I&#8217;m indenting my code anyway</strong>, why not leave out a couple of useless characters while I&#8217;m it?</p>
<h3>Arrows</h3>
<p>I pretty much hate the word <em>function</em> because it&#8217;s long to write and it&#8217;s plastered everywhere the moment you begin to write well structured javascript. Arrows are shorter and combined with indentation, you transform a callback</p>
<p><script src="https://gist.github.com/2645929.js?file=no arrows.js"></script></p>
<p>in </p>
<p><script src="https://gist.github.com/2645929.js?file=arrows.coffee"></script></p>
<h3>We are writing code, not emoticons</h3>
<p>I&#8217;m talking about <code>; , ( ) { } :</code>. This kind of stuff should only be used to write emoticons.</p>
<p><script src="https://gist.github.com/2645929.js?file=emoticon.js"></script></p>
<p>transforms to</p>
<p><script src="https://gist.github.com/2645929.js?file=emoticon.coffee"></script></p>
<h3>Shortcuts</h3>
<p>You want to execute a function only if it is not null and is actually a function? In classic javascript, I have to think about it but I would do something like :</p>
<p><script src="https://gist.github.com/2645929.js?file=null function.js"></script></p>
<p>I would have to test it somewhere to make sure that <em>typeof</em> returns <em>function</em> and not <em>Function</em> (capitalized). Let&#8217;s forget about this ugly code and write :</p>
<p><script src="https://gist.github.com/2645929.js?file=null function.coffee"></script></p>
<p>This is just an example, <a href="http://coffeescript.org/" target="_blank">learn coffeescript</a> to know more.</p>
<h3>String interpolation</h3>
<p><script src="https://gist.github.com/2645929.js?file=plus.js"></script></p>
<p>Really? I have to use <strong>+</strong>? I even used arrays to make it clearer not too long ago :</p>
<p><script src="https://gist.github.com/2645929.js?file=array.js"></script></p>
<p>Let&#8217;s add some decency :</p>
<p><script src="https://gist.github.com/2645929.js?file=interpolation.coffee"></script></p>
<h3>Ashkenas (creator of coffeescript) is my brother</h3>
<p>I met MACournoyer 2-3 times. He is the guy who wrote <a href="http://2aca0652n1wbqg6df9v7scfyaz.hop.clickbank.net/" target="_blank"><strong>the best book about creating a programming language</strong></a>. This incredible book inspired Ashkenas to create coffeescript. As you can see, we have a very close relationship, he is literally like a brother to me!</p>
<div class="feedflare">
<a href="http://feeds.feedburner.com/~ff/JavascriptKata?a=z1K48QTW9gg:0WO80YVFYNQ:F7zBnMyn0Lo"><img src="http://feeds.feedburner.com/~ff/JavascriptKata?i=z1K48QTW9gg:0WO80YVFYNQ:F7zBnMyn0Lo" border="0"></img></a> <a href="http://feeds.feedburner.com/~ff/JavascriptKata?a=z1K48QTW9gg:0WO80YVFYNQ:V_sGLiPBpWU"><img src="http://feeds.feedburner.com/~ff/JavascriptKata?i=z1K48QTW9gg:0WO80YVFYNQ:V_sGLiPBpWU" border="0"></img></a> <a href="http://feeds.feedburner.com/~ff/JavascriptKata?a=z1K48QTW9gg:0WO80YVFYNQ:cGdyc7Q-1BI"><img src="http://feeds.feedburner.com/~ff/JavascriptKata?d=cGdyc7Q-1BI" border="0"></img></a> <a href="http://feeds.feedburner.com/~ff/JavascriptKata?a=z1K48QTW9gg:0WO80YVFYNQ:gIN9vFwOqvQ"><img src="http://feeds.feedburner.com/~ff/JavascriptKata?i=z1K48QTW9gg:0WO80YVFYNQ:gIN9vFwOqvQ" border="0"></img></a> <a href="http://feeds.feedburner.com/~ff/JavascriptKata?a=z1K48QTW9gg:0WO80YVFYNQ:qj6IDK7rITs"><img src="http://feeds.feedburner.com/~ff/JavascriptKata?d=qj6IDK7rITs" border="0"></img></a>
</div>]]></content:encoded>
			<wfw:commentRss>http://www.javascriptkata.com/2012/05/22/stop-javascript-use-coffeescript/feed/</wfw:commentRss>
		<slash:comments>7</slash:comments>
		<feedburner:origLink>http://www.javascriptkata.com/2012/05/22/stop-javascript-use-coffeescript/?utm_source=rss&amp;utm_medium=rss&amp;utm_campaign=stop-javascript-use-coffeescript</feedburner:origLink></item>
		<item>
		<title>How to write javascript code to fit in lines of 80 chars</title>
		<link>http://feedproxy.google.com/~r/JavascriptKata/~3/42KIRiL0x9c/</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><a href="http://feedads.g.doubleclick.net/~a/DG9cPuEfyJiDOE6mKUYEI3wfyFk/0/da"><img src="http://feedads.g.doubleclick.net/~a/DG9cPuEfyJiDOE6mKUYEI3wfyFk/0/di" border="0" ismap="true"></img></a><br/>
<a href="http://feedads.g.doubleclick.net/~a/DG9cPuEfyJiDOE6mKUYEI3wfyFk/1/da"><img src="http://feedads.g.doubleclick.net/~a/DG9cPuEfyJiDOE6mKUYEI3wfyFk/1/di" border="0" ismap="true"></img></a></p><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>
<div class="feedflare">
<a href="http://feeds.feedburner.com/~ff/JavascriptKata?a=42KIRiL0x9c:PbJl8SKrXuA:F7zBnMyn0Lo"><img src="http://feeds.feedburner.com/~ff/JavascriptKata?i=42KIRiL0x9c:PbJl8SKrXuA:F7zBnMyn0Lo" border="0"></img></a> <a href="http://feeds.feedburner.com/~ff/JavascriptKata?a=42KIRiL0x9c:PbJl8SKrXuA:V_sGLiPBpWU"><img src="http://feeds.feedburner.com/~ff/JavascriptKata?i=42KIRiL0x9c:PbJl8SKrXuA:V_sGLiPBpWU" border="0"></img></a> <a href="http://feeds.feedburner.com/~ff/JavascriptKata?a=42KIRiL0x9c:PbJl8SKrXuA:cGdyc7Q-1BI"><img src="http://feeds.feedburner.com/~ff/JavascriptKata?d=cGdyc7Q-1BI" border="0"></img></a> <a href="http://feeds.feedburner.com/~ff/JavascriptKata?a=42KIRiL0x9c:PbJl8SKrXuA:gIN9vFwOqvQ"><img src="http://feeds.feedburner.com/~ff/JavascriptKata?i=42KIRiL0x9c:PbJl8SKrXuA:gIN9vFwOqvQ" border="0"></img></a> <a href="http://feeds.feedburner.com/~ff/JavascriptKata?a=42KIRiL0x9c:PbJl8SKrXuA:qj6IDK7rITs"><img src="http://feeds.feedburner.com/~ff/JavascriptKata?d=qj6IDK7rITs" border="0"></img></a>
</div>]]></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>
		<feedburner:origLink>http://www.javascriptkata.com/2012/04/30/how-to-write-javascript-code-to-fit-in-lines-of-80-chars/?utm_source=rss&amp;utm_medium=rss&amp;utm_campaign=how-to-write-javascript-code-to-fit-in-lines-of-80-chars</feedburner:origLink></item>
		<item>
		<title>How to make sure undefined is not defined</title>
		<link>http://feedproxy.google.com/~r/JavascriptKata/~3/HsIIMBlkRuM/</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><a href="http://feedads.g.doubleclick.net/~a/Edx5CqJCDYGhZ-ZuSW_jCV4fALs/0/da"><img src="http://feedads.g.doubleclick.net/~a/Edx5CqJCDYGhZ-ZuSW_jCV4fALs/0/di" border="0" ismap="true"></img></a><br/>
<a href="http://feedads.g.doubleclick.net/~a/Edx5CqJCDYGhZ-ZuSW_jCV4fALs/1/da"><img src="http://feedads.g.doubleclick.net/~a/Edx5CqJCDYGhZ-ZuSW_jCV4fALs/1/di" border="0" ismap="true"></img></a></p><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>
<div class="feedflare">
<a href="http://feeds.feedburner.com/~ff/JavascriptKata?a=HsIIMBlkRuM:6vWD9VkfuDg:F7zBnMyn0Lo"><img src="http://feeds.feedburner.com/~ff/JavascriptKata?i=HsIIMBlkRuM:6vWD9VkfuDg:F7zBnMyn0Lo" border="0"></img></a> <a href="http://feeds.feedburner.com/~ff/JavascriptKata?a=HsIIMBlkRuM:6vWD9VkfuDg:V_sGLiPBpWU"><img src="http://feeds.feedburner.com/~ff/JavascriptKata?i=HsIIMBlkRuM:6vWD9VkfuDg:V_sGLiPBpWU" border="0"></img></a> <a href="http://feeds.feedburner.com/~ff/JavascriptKata?a=HsIIMBlkRuM:6vWD9VkfuDg:cGdyc7Q-1BI"><img src="http://feeds.feedburner.com/~ff/JavascriptKata?d=cGdyc7Q-1BI" border="0"></img></a> <a href="http://feeds.feedburner.com/~ff/JavascriptKata?a=HsIIMBlkRuM:6vWD9VkfuDg:gIN9vFwOqvQ"><img src="http://feeds.feedburner.com/~ff/JavascriptKata?i=HsIIMBlkRuM:6vWD9VkfuDg:gIN9vFwOqvQ" border="0"></img></a> <a href="http://feeds.feedburner.com/~ff/JavascriptKata?a=HsIIMBlkRuM:6vWD9VkfuDg:qj6IDK7rITs"><img src="http://feeds.feedburner.com/~ff/JavascriptKata?d=qj6IDK7rITs" border="0"></img></a>
</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>
		<feedburner:origLink>http://www.javascriptkata.com/2011/09/13/how-to-make-sure-undefined-is-not-defined/?utm_source=rss&amp;utm_medium=rss&amp;utm_campaign=how-to-make-sure-undefined-is-not-defined</feedburner:origLink></item>
		<item>
		<title>Creating namespaces with self-invoking functions</title>
		<link>http://feedproxy.google.com/~r/JavascriptKata/~3/kYwf6jejsr4/</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><a href="http://feedads.g.doubleclick.net/~a/3VENSudDcCN1OG4L_li44DeMna0/0/da"><img src="http://feedads.g.doubleclick.net/~a/3VENSudDcCN1OG4L_li44DeMna0/0/di" border="0" ismap="true"></img></a><br/>
<a href="http://feedads.g.doubleclick.net/~a/3VENSudDcCN1OG4L_li44DeMna0/1/da"><img src="http://feedads.g.doubleclick.net/~a/3VENSudDcCN1OG4L_li44DeMna0/1/di" border="0" ismap="true"></img></a></p><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>
<div class="feedflare">
<a href="http://feeds.feedburner.com/~ff/JavascriptKata?a=kYwf6jejsr4:K5Ra6SUazfs:F7zBnMyn0Lo"><img src="http://feeds.feedburner.com/~ff/JavascriptKata?i=kYwf6jejsr4:K5Ra6SUazfs:F7zBnMyn0Lo" border="0"></img></a> <a href="http://feeds.feedburner.com/~ff/JavascriptKata?a=kYwf6jejsr4:K5Ra6SUazfs:V_sGLiPBpWU"><img src="http://feeds.feedburner.com/~ff/JavascriptKata?i=kYwf6jejsr4:K5Ra6SUazfs:V_sGLiPBpWU" border="0"></img></a> <a href="http://feeds.feedburner.com/~ff/JavascriptKata?a=kYwf6jejsr4:K5Ra6SUazfs:cGdyc7Q-1BI"><img src="http://feeds.feedburner.com/~ff/JavascriptKata?d=cGdyc7Q-1BI" border="0"></img></a> <a href="http://feeds.feedburner.com/~ff/JavascriptKata?a=kYwf6jejsr4:K5Ra6SUazfs:gIN9vFwOqvQ"><img src="http://feeds.feedburner.com/~ff/JavascriptKata?i=kYwf6jejsr4:K5Ra6SUazfs:gIN9vFwOqvQ" border="0"></img></a> <a href="http://feeds.feedburner.com/~ff/JavascriptKata?a=kYwf6jejsr4:K5Ra6SUazfs:qj6IDK7rITs"><img src="http://feeds.feedburner.com/~ff/JavascriptKata?d=qj6IDK7rITs" border="0"></img></a>
</div>]]></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>
		<feedburner:origLink>http://www.javascriptkata.com/2011/09/05/creating-namespaces-with-self-invoking-functions/?utm_source=rss&amp;utm_medium=rss&amp;utm_campaign=creating-namespaces-with-self-invoking-functions</feedburner:origLink></item>
		<item>
		<title>Self-invoking functions explained line by line</title>
		<link>http://feedproxy.google.com/~r/JavascriptKata/~3/9Er2oLKiDDw/</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><a href="http://feedads.g.doubleclick.net/~a/tEOnr5QXvJLGWbRGyeX2L4as6PQ/0/da"><img src="http://feedads.g.doubleclick.net/~a/tEOnr5QXvJLGWbRGyeX2L4as6PQ/0/di" border="0" ismap="true"></img></a><br/>
<a href="http://feedads.g.doubleclick.net/~a/tEOnr5QXvJLGWbRGyeX2L4as6PQ/1/da"><img src="http://feedads.g.doubleclick.net/~a/tEOnr5QXvJLGWbRGyeX2L4as6PQ/1/di" border="0" ismap="true"></img></a></p><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>
<div class="feedflare">
<a href="http://feeds.feedburner.com/~ff/JavascriptKata?a=9Er2oLKiDDw:-REEM091vL8:F7zBnMyn0Lo"><img src="http://feeds.feedburner.com/~ff/JavascriptKata?i=9Er2oLKiDDw:-REEM091vL8:F7zBnMyn0Lo" border="0"></img></a> <a href="http://feeds.feedburner.com/~ff/JavascriptKata?a=9Er2oLKiDDw:-REEM091vL8:V_sGLiPBpWU"><img src="http://feeds.feedburner.com/~ff/JavascriptKata?i=9Er2oLKiDDw:-REEM091vL8:V_sGLiPBpWU" border="0"></img></a> <a href="http://feeds.feedburner.com/~ff/JavascriptKata?a=9Er2oLKiDDw:-REEM091vL8:cGdyc7Q-1BI"><img src="http://feeds.feedburner.com/~ff/JavascriptKata?d=cGdyc7Q-1BI" border="0"></img></a> <a href="http://feeds.feedburner.com/~ff/JavascriptKata?a=9Er2oLKiDDw:-REEM091vL8:gIN9vFwOqvQ"><img src="http://feeds.feedburner.com/~ff/JavascriptKata?i=9Er2oLKiDDw:-REEM091vL8:gIN9vFwOqvQ" border="0"></img></a> <a href="http://feeds.feedburner.com/~ff/JavascriptKata?a=9Er2oLKiDDw:-REEM091vL8:qj6IDK7rITs"><img src="http://feeds.feedburner.com/~ff/JavascriptKata?d=qj6IDK7rITs" border="0"></img></a>
</div>]]></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>
		<feedburner:origLink>http://www.javascriptkata.com/2011/08/22/self-invoking-functions-explained-line-by-line/?utm_source=rss&amp;utm_medium=rss&amp;utm_campaign=self-invoking-functions-explained-line-by-line</feedburner:origLink></item>
		<item>
		<title>Ready.js – continuous integration using jslint, nodejs and git</title>
		<link>http://feedproxy.google.com/~r/JavascriptKata/~3/OgECc7xW8K4/</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://feedads.g.doubleclick.net/~a/Y8o-qSCvXC8bsLE4GehRuPaW0NI/0/da"><img src="http://feedads.g.doubleclick.net/~a/Y8o-qSCvXC8bsLE4GehRuPaW0NI/0/di" border="0" ismap="true"></img></a><br/>
<a href="http://feedads.g.doubleclick.net/~a/Y8o-qSCvXC8bsLE4GehRuPaW0NI/1/da"><img src="http://feedads.g.doubleclick.net/~a/Y8o-qSCvXC8bsLE4GehRuPaW0NI/1/di" border="0" ismap="true"></img></a></p><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>
<div class="feedflare">
<a href="http://feeds.feedburner.com/~ff/JavascriptKata?a=OgECc7xW8K4:9cqP_Pd_a64:F7zBnMyn0Lo"><img src="http://feeds.feedburner.com/~ff/JavascriptKata?i=OgECc7xW8K4:9cqP_Pd_a64:F7zBnMyn0Lo" border="0"></img></a> <a href="http://feeds.feedburner.com/~ff/JavascriptKata?a=OgECc7xW8K4:9cqP_Pd_a64:V_sGLiPBpWU"><img src="http://feeds.feedburner.com/~ff/JavascriptKata?i=OgECc7xW8K4:9cqP_Pd_a64:V_sGLiPBpWU" border="0"></img></a> <a href="http://feeds.feedburner.com/~ff/JavascriptKata?a=OgECc7xW8K4:9cqP_Pd_a64:cGdyc7Q-1BI"><img src="http://feeds.feedburner.com/~ff/JavascriptKata?d=cGdyc7Q-1BI" border="0"></img></a> <a href="http://feeds.feedburner.com/~ff/JavascriptKata?a=OgECc7xW8K4:9cqP_Pd_a64:gIN9vFwOqvQ"><img src="http://feeds.feedburner.com/~ff/JavascriptKata?i=OgECc7xW8K4:9cqP_Pd_a64:gIN9vFwOqvQ" border="0"></img></a> <a href="http://feeds.feedburner.com/~ff/JavascriptKata?a=OgECc7xW8K4:9cqP_Pd_a64:qj6IDK7rITs"><img src="http://feeds.feedburner.com/~ff/JavascriptKata?d=qj6IDK7rITs" border="0"></img></a>
</div>]]></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>
		<feedburner:origLink>http://www.javascriptkata.com/2010/10/28/ready-js-prepare-your-javascript-for-production/?utm_source=rss&amp;utm_medium=rss&amp;utm_campaign=ready-js-prepare-your-javascript-for-production</feedburner:origLink></item>
		<item>
		<title>Alternatives to singletons in javascript</title>
		<link>http://feedproxy.google.com/~r/JavascriptKata/~3/_919eIZ5X-A/</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><a href="http://feedads.g.doubleclick.net/~a/pHWz7j9ECmviyaXzFRCr8E56_00/0/da"><img src="http://feedads.g.doubleclick.net/~a/pHWz7j9ECmviyaXzFRCr8E56_00/0/di" border="0" ismap="true"></img></a><br/>
<a href="http://feedads.g.doubleclick.net/~a/pHWz7j9ECmviyaXzFRCr8E56_00/1/da"><img src="http://feedads.g.doubleclick.net/~a/pHWz7j9ECmviyaXzFRCr8E56_00/1/di" border="0" ismap="true"></img></a></p><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>
<div class="feedflare">
<a href="http://feeds.feedburner.com/~ff/JavascriptKata?a=_919eIZ5X-A:b_3Zt7th5-g:F7zBnMyn0Lo"><img src="http://feeds.feedburner.com/~ff/JavascriptKata?i=_919eIZ5X-A:b_3Zt7th5-g:F7zBnMyn0Lo" border="0"></img></a> <a href="http://feeds.feedburner.com/~ff/JavascriptKata?a=_919eIZ5X-A:b_3Zt7th5-g:V_sGLiPBpWU"><img src="http://feeds.feedburner.com/~ff/JavascriptKata?i=_919eIZ5X-A:b_3Zt7th5-g:V_sGLiPBpWU" border="0"></img></a> <a href="http://feeds.feedburner.com/~ff/JavascriptKata?a=_919eIZ5X-A:b_3Zt7th5-g:cGdyc7Q-1BI"><img src="http://feeds.feedburner.com/~ff/JavascriptKata?d=cGdyc7Q-1BI" border="0"></img></a> <a href="http://feeds.feedburner.com/~ff/JavascriptKata?a=_919eIZ5X-A:b_3Zt7th5-g:gIN9vFwOqvQ"><img src="http://feeds.feedburner.com/~ff/JavascriptKata?i=_919eIZ5X-A:b_3Zt7th5-g:gIN9vFwOqvQ" border="0"></img></a> <a href="http://feeds.feedburner.com/~ff/JavascriptKata?a=_919eIZ5X-A:b_3Zt7th5-g:qj6IDK7rITs"><img src="http://feeds.feedburner.com/~ff/JavascriptKata?d=qj6IDK7rITs" border="0"></img></a>
</div>]]></content:encoded>
			<wfw:commentRss>http://www.javascriptkata.com/2010/10/20/alternatives-to-singletons-in-javascript/feed/</wfw:commentRss>
		<slash:comments>17</slash:comments>
		<feedburner:origLink>http://www.javascriptkata.com/2010/10/20/alternatives-to-singletons-in-javascript/?utm_source=rss&amp;utm_medium=rss&amp;utm_campaign=alternatives-to-singletons-in-javascript</feedburner:origLink></item>
		<item>
		<title>Auto-detect timezones, time zone offsets and daylight saving in javascript</title>
		<link>http://feedproxy.google.com/~r/JavascriptKata/~3/XyXtJbjo5M4/</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><a href="http://feedads.g.doubleclick.net/~a/F-Esg_twMydzNCJxrFx1r8n85Iw/0/da"><img src="http://feedads.g.doubleclick.net/~a/F-Esg_twMydzNCJxrFx1r8n85Iw/0/di" border="0" ismap="true"></img></a><br/>
<a href="http://feedads.g.doubleclick.net/~a/F-Esg_twMydzNCJxrFx1r8n85Iw/1/da"><img src="http://feedads.g.doubleclick.net/~a/F-Esg_twMydzNCJxrFx1r8n85Iw/1/di" border="0" ismap="true"></img></a></p><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>
<div class="feedflare">
<a href="http://feeds.feedburner.com/~ff/JavascriptKata?a=XyXtJbjo5M4:3J-YeL8cmIs:F7zBnMyn0Lo"><img src="http://feeds.feedburner.com/~ff/JavascriptKata?i=XyXtJbjo5M4:3J-YeL8cmIs:F7zBnMyn0Lo" border="0"></img></a> <a href="http://feeds.feedburner.com/~ff/JavascriptKata?a=XyXtJbjo5M4:3J-YeL8cmIs:V_sGLiPBpWU"><img src="http://feeds.feedburner.com/~ff/JavascriptKata?i=XyXtJbjo5M4:3J-YeL8cmIs:V_sGLiPBpWU" border="0"></img></a> <a href="http://feeds.feedburner.com/~ff/JavascriptKata?a=XyXtJbjo5M4:3J-YeL8cmIs:cGdyc7Q-1BI"><img src="http://feeds.feedburner.com/~ff/JavascriptKata?d=cGdyc7Q-1BI" border="0"></img></a> <a href="http://feeds.feedburner.com/~ff/JavascriptKata?a=XyXtJbjo5M4:3J-YeL8cmIs:gIN9vFwOqvQ"><img src="http://feeds.feedburner.com/~ff/JavascriptKata?i=XyXtJbjo5M4:3J-YeL8cmIs:gIN9vFwOqvQ" border="0"></img></a> <a href="http://feeds.feedburner.com/~ff/JavascriptKata?a=XyXtJbjo5M4:3J-YeL8cmIs:qj6IDK7rITs"><img src="http://feeds.feedburner.com/~ff/JavascriptKata?d=qj6IDK7rITs" border="0"></img></a>
</div>]]></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>
		<feedburner:origLink>http://www.javascriptkata.com/2010/10/14/auto-detect-timezones-time-zone-offsets-and-daylight-saving-in-javascript/?utm_source=rss&amp;utm_medium=rss&amp;utm_campaign=auto-detect-timezones-time-zone-offsets-and-daylight-saving-in-javascript</feedburner:origLink></item>
		<item>
		<title>NoFreeze : a library that avoids freezing in javascript</title>
		<link>http://feedproxy.google.com/~r/JavascriptKata/~3/u5OwlGVt_hU/</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><a href="http://feedads.g.doubleclick.net/~a/toRVPPkzD2oc3CT-jG3QiNDNqQk/0/da"><img src="http://feedads.g.doubleclick.net/~a/toRVPPkzD2oc3CT-jG3QiNDNqQk/0/di" border="0" ismap="true"></img></a><br/>
<a href="http://feedads.g.doubleclick.net/~a/toRVPPkzD2oc3CT-jG3QiNDNqQk/1/da"><img src="http://feedads.g.doubleclick.net/~a/toRVPPkzD2oc3CT-jG3QiNDNqQk/1/di" border="0" ismap="true"></img></a></p><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>
<div class="feedflare">
<a href="http://feeds.feedburner.com/~ff/JavascriptKata?a=u5OwlGVt_hU:i5BtHhaZB2Q:F7zBnMyn0Lo"><img src="http://feeds.feedburner.com/~ff/JavascriptKata?i=u5OwlGVt_hU:i5BtHhaZB2Q:F7zBnMyn0Lo" border="0"></img></a> <a href="http://feeds.feedburner.com/~ff/JavascriptKata?a=u5OwlGVt_hU:i5BtHhaZB2Q:V_sGLiPBpWU"><img src="http://feeds.feedburner.com/~ff/JavascriptKata?i=u5OwlGVt_hU:i5BtHhaZB2Q:V_sGLiPBpWU" border="0"></img></a> <a href="http://feeds.feedburner.com/~ff/JavascriptKata?a=u5OwlGVt_hU:i5BtHhaZB2Q:cGdyc7Q-1BI"><img src="http://feeds.feedburner.com/~ff/JavascriptKata?d=cGdyc7Q-1BI" border="0"></img></a> <a href="http://feeds.feedburner.com/~ff/JavascriptKata?a=u5OwlGVt_hU:i5BtHhaZB2Q:gIN9vFwOqvQ"><img src="http://feeds.feedburner.com/~ff/JavascriptKata?i=u5OwlGVt_hU:i5BtHhaZB2Q:gIN9vFwOqvQ" border="0"></img></a> <a href="http://feeds.feedburner.com/~ff/JavascriptKata?a=u5OwlGVt_hU:i5BtHhaZB2Q:qj6IDK7rITs"><img src="http://feeds.feedburner.com/~ff/JavascriptKata?d=qj6IDK7rITs" border="0"></img></a>
</div>]]></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>
		<feedburner:origLink>http://www.javascriptkata.com/2010/08/10/nofreeze-a-library-that-avoids-freezing-in-javascript/?utm_source=rss&amp;utm_medium=rss&amp;utm_campaign=nofreeze-a-library-that-avoids-freezing-in-javascript</feedburner:origLink></item>
		<item>
		<title>Announcing the jsKata libraries</title>
		<link>http://feedproxy.google.com/~r/JavascriptKata/~3/nD5nf6PMGKU/</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><a href="http://feedads.g.doubleclick.net/~a/csmaE94sq-i2WkwhhRLOnkS6pnQ/0/da"><img src="http://feedads.g.doubleclick.net/~a/csmaE94sq-i2WkwhhRLOnkS6pnQ/0/di" border="0" ismap="true"></img></a><br/>
<a href="http://feedads.g.doubleclick.net/~a/csmaE94sq-i2WkwhhRLOnkS6pnQ/1/da"><img src="http://feedads.g.doubleclick.net/~a/csmaE94sq-i2WkwhhRLOnkS6pnQ/1/di" border="0" ismap="true"></img></a></p><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>
<div class="feedflare">
<a href="http://feeds.feedburner.com/~ff/JavascriptKata?a=nD5nf6PMGKU:7EWENK59wTo:F7zBnMyn0Lo"><img src="http://feeds.feedburner.com/~ff/JavascriptKata?i=nD5nf6PMGKU:7EWENK59wTo:F7zBnMyn0Lo" border="0"></img></a> <a href="http://feeds.feedburner.com/~ff/JavascriptKata?a=nD5nf6PMGKU:7EWENK59wTo:V_sGLiPBpWU"><img src="http://feeds.feedburner.com/~ff/JavascriptKata?i=nD5nf6PMGKU:7EWENK59wTo:V_sGLiPBpWU" border="0"></img></a> <a href="http://feeds.feedburner.com/~ff/JavascriptKata?a=nD5nf6PMGKU:7EWENK59wTo:cGdyc7Q-1BI"><img src="http://feeds.feedburner.com/~ff/JavascriptKata?d=cGdyc7Q-1BI" border="0"></img></a> <a href="http://feeds.feedburner.com/~ff/JavascriptKata?a=nD5nf6PMGKU:7EWENK59wTo:gIN9vFwOqvQ"><img src="http://feeds.feedburner.com/~ff/JavascriptKata?i=nD5nf6PMGKU:7EWENK59wTo:gIN9vFwOqvQ" border="0"></img></a> <a href="http://feeds.feedburner.com/~ff/JavascriptKata?a=nD5nf6PMGKU:7EWENK59wTo:qj6IDK7rITs"><img src="http://feeds.feedburner.com/~ff/JavascriptKata?d=qj6IDK7rITs" border="0"></img></a>
</div>]]></content:encoded>
			<wfw:commentRss>http://www.javascriptkata.com/2010/06/15/announcing-the-jskata-libraries/feed/</wfw:commentRss>
		<slash:comments>5</slash:comments>
		<feedburner:origLink>http://www.javascriptkata.com/2010/06/15/announcing-the-jskata-libraries/?utm_source=rss&amp;utm_medium=rss&amp;utm_campaign=announcing-the-jskata-libraries</feedburner:origLink></item>
	</channel>
</rss>

