<?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 &#187; technical level</title>
	<atom:link href="http://www.javascriptkata.com/category/level/feed/" rel="self" type="application/rss+xml" />
	<link>http://www.javascriptkata.com</link>
	<description>helping you with javascript since 2007</description>
	<lastBuildDate>Wed, 14 Sep 2011 14:07:48 +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 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>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>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>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>
		<item>
		<title>An undo/redo library for your app</title>
		<link>http://www.javascriptkata.com/2010/04/26/an-undoredo-library-for-your-app/?utm_source=rss&#038;utm_medium=rss&#038;utm_campaign=an-undoredo-library-for-your-app</link>
		<comments>http://www.javascriptkata.com/2010/04/26/an-undoredo-library-for-your-app/#comments</comments>
		<pubDate>Mon, 26 Apr 2010 20:26:00 +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=206</guid>
		<description><![CDATA[I wrote about undo before and I pushed it farther. What about an undo/redo system? I extended the v.01 of the undo and I now handle undo and redo in the same object. Annoucing redo in jskata.undo I made a new version of jskata.undo still hosted on GitHub. If you look closer, it is now [...]]]></description>
			<content:encoded><![CDATA[<p>I wrote about <a href="http://www.javascriptkata.com/2010/03/29/how-to-write-an-simple-undo-system-for-your-app/">undo</a> before and I pushed it farther. What about an <strong>undo/redo system</strong>? I extended the <a href="http://github.com/dsimard/jskataUndo/blob/v0.1/jskataUndo.js">v.01</a> of the undo and I now handle undo and redo in the same object.</p>
<h3>Annoucing redo in jskata.undo</h3>
<p>I made a new version of <em>jskata.undo</em> still <a href="http://github.com/dsimard/jskata/blob/master/src/jskata.undo.js">hosted on GitHub</a>. If you look closer, it is now part of a library called <a href="http://github.com/dsimard/jskata">jskata</a> that is not officially announced. My next post will talk about it in details.</p>
<h3>Demo</h3>
<p>Take a look at the <a href="http://dsimard.github.com/jskata/undo" target="_blank">demo</a>. The javascript of the demo is available <a href="http://github.com/dsimard/jskata/blob/gh-pages/undo.js#files" target="_blank">here</a>. You can see the <a href="http://github.com/dsimard/jskata/blob/master/doc/jskata.undo.textile#files">complete doc on GitHub</a>.</p>
<h3>How does it work?</h3>
<h4>Execute an action that you can undo/redo</h4>
<p>Doing something requires calling <em>execute</em> with 2 functions as parameters : the <em>do</em> and the <em>undo</em>.<br />
<script src="http://gist.github.com/379819.js"></script></p>
<h4>Undo and redo the last action</h4>
<p>Very easy!<br />
<script src="http://gist.github.com/379830.js?file=call+undo+and+redo.js"></script></p>
<h4>Events and properties</h4>
<p>For the moment, jskata.undo has just one event : <em>onChange</em>.<br />
<script src="http://gist.github.com/379834.js?file=jskata.undo.onchange.js"></script></p>
<p>There are 2 properties : <em>canUndo()</em> and <em>canRedo()</em>.<br />
<script src="http://gist.github.com/379840.js?file=canUndo+and+canRedo.js"></script></p>
]]></content:encoded>
			<wfw:commentRss>http://www.javascriptkata.com/2010/04/26/an-undoredo-library-for-your-app/feed/</wfw:commentRss>
		<slash:comments>7</slash:comments>
		</item>
		<item>
		<title>Error with hashes in IE but not in Firefox</title>
		<link>http://www.javascriptkata.com/2010/04/06/error-with-hashes-in-ie-but-not-in-firefox/?utm_source=rss&#038;utm_medium=rss&#038;utm_campaign=error-with-hashes-in-ie-but-not-in-firefox</link>
		<comments>http://www.javascriptkata.com/2010/04/06/error-with-hashes-in-ie-but-not-in-firefox/#comments</comments>
		<pubDate>Tue, 06 Apr 2010 12:55:59 +0000</pubDate>
		<dc:creator>dsimard</dc:creator>
				<category><![CDATA[white belt]]></category>

		<guid isPermaLink="false">http://www.javascriptkata.com/?p=181</guid>
		<description><![CDATA[There&#8217;s a common error that happens quite a lot on IE only and never on Firefox. You search it, you check all your variables, you comment your code to the bare minimum but you can never find it. Stop looking! Here&#8217;s the answer. You probably just forgot a comma (,) at the end of a [...]]]></description>
			<content:encoded><![CDATA[<p>There&#8217;s a common error that happens quite a lot on IE only and never on Firefox. You search it, you check all your variables, you comment your code to the bare minimum <strong>but you can never find it</strong>.</p>
<p><strong>Stop looking! Here&#8217;s the answer.</strong></p>
<p>You probably just forgot a comma (,) at the end of a hash. Here&#8217;s an example :</p>
<div class="dean_ch" style="white-space: wrap;">
<ol>
<li class="li1">
<div class="de1"><span class="kw2">var</span> val = <span class="br0">&#123;</span></div>
</li>
<li class="li1">
<div class="de1">&nbsp; cat:<span class="st0">&quot;Mistigri&quot;</span>,</div>
</li>
<li class="li1">
<div class="de1">&nbsp; dog:<span class="st0">&quot;Rex&quot;</span>,</div>
</li>
<li class="li1">
<div class="de1">&nbsp; butler:<span class="st0">&quot;Jeeves&quot;</span>, <span class="co1">// There&#8217;s an extra comma</span></div>
</li>
<li class="li2">
<div class="de2"><span class="br0">&#125;</span></div>
</li>
</ol>
</div>
<p>You see the extra comma at the string &#8220;Jeeves&#8221;? This what causes the problem. <strong>Firefox handles an extra comma nicely but Internet explorer doesn&#8217;t</strong>. To correct the error, just write :</p>
<div class="dean_ch" style="white-space: wrap;">
<ol>
<li class="li1">
<div class="de1"><span class="kw2">var</span> val = <span class="br0">&#123;</span></div>
</li>
<li class="li1">
<div class="de1">&nbsp; cat:<span class="st0">&quot;Mistigri&quot;</span>,</div>
</li>
<li class="li1">
<div class="de1">&nbsp; dog:<span class="st0">&quot;Rex&quot;</span>,</div>
</li>
<li class="li1">
<div class="de1">&nbsp; butler:<span class="st0">&quot;Jeeves&quot;</span> <span class="co1">// There&#8217;s no extra comma</span></div>
</li>
<li class="li2">
<div class="de2"><span class="br0">&#125;</span></div>
</li>
</ol>
</div>
<h3>JSLint to the rescue</h3>
<p><a href="http://programmieraffe.de/">Programmieraffe</a> told me about <a href="http://www.jslint.com/">JSLint</a>. You paste your javascript code and it verifies it for you. Not only for extra-commas but everything else.</p>
]]></content:encoded>
			<wfw:commentRss>http://www.javascriptkata.com/2010/04/06/error-with-hashes-in-ie-but-not-in-firefox/feed/</wfw:commentRss>
		<slash:comments>11</slash:comments>
		</item>
		<item>
		<title>How to write a simple undo system for your app</title>
		<link>http://www.javascriptkata.com/2010/03/29/how-to-write-an-simple-undo-system-for-your-app/?utm_source=rss&#038;utm_medium=rss&#038;utm_campaign=how-to-write-an-simple-undo-system-for-your-app</link>
		<comments>http://www.javascriptkata.com/2010/03/29/how-to-write-an-simple-undo-system-for-your-app/#comments</comments>
		<pubDate>Mon, 29 Mar 2010 14:20:39 +0000</pubDate>
		<dc:creator>dsimard</dc:creator>
				<category><![CDATA[ajax]]></category>
		<category><![CDATA[black belt]]></category>
		<category><![CDATA[function]]></category>
		<category><![CDATA[how to]]></category>

		<guid isPermaLink="false">http://www.javascriptkata.com/?p=188</guid>
		<description><![CDATA[I really like undos. If I could undo that that last beer... Unfortunately, I can't. But, <strong>I can offer undo to users of my application</strong>. ]]></description>
			<content:encoded><![CDATA[<p>I really like undos. If I could undo that that last beer&#8230; Unfortunately, I can&#8217;t. But, <strong>I can offer undo to users of my application</strong>. </p>
<p>When I thought about undo before, I thought about a complicated Rails plugin that would keep an anonymous ID linking to any table in the database with an &#8220;action&#8221; field that would contain the action to undo. Pretty complicated stuff for something as simple.</p>
<h3>The javascript solution</h3>
<p>One day while I was writing some javascript, the solution struck me : <strong>I could handle this completely with javascript</strong>. The basic idea is that whenever an action is completed, I can save its undo function in an array and call it whenever I need it.</p>
<p>Example, if I&#8217;m saving a user in a database with Ajax :</p>
<ol>
<li>I send the data to server to save a new user</li>
<li>The server returns the ID of the new user</li>
<li>I create a function with an ajax request that will send a delete of this user to the server</li>
<li>I add the previous function to a stack of <em>undo</em> functions to be able to call it later</li>
</ol>
<p>That, when I call that last function, it will undo that user creation without having to keep it in memory server-side. </p>
<h3>Introducing jsKata.undo</h3>
<p>I wrote a little something called <a href="http://github.com/dsimard/jskata/blob/master/src/jskata.undo.js">jsKata.undo on GitHub</a> that contains the logic describe earlier. It&#8217;s quite simple but it may grow over time. It has no requirement, not even jQuery. It&#8217;s very simple to use.</p>
<p>The <a href="http://github.com/dsimard/jskata/blob/master/doc/jskata.undo.textile">complete doc</a> is on GitHub.</p>
<h4>1. Adding an action that can you can undo</h4>
<p>I&#8217;ll use the &#8220;add a user&#8221; example with jQuery.</p>
<div class="dean_ch" style="white-space: wrap;">
<ol>
<li class="li1">
<div class="de1">$.<span class="me1">post</span><span class="br0">&#40;</span></div>
</li>
<li class="li1">
<div class="de1">&nbsp; <span class="st0">&quot;http://yoursite.com/users/new&quot;</span>, <span class="co1">// The url to add a user</span></div>
</li>
<li class="li1">
<div class="de1">&nbsp; <span class="br0">&#123;</span><span class="kw3">name</span>:<span class="st0">&quot;John Boucher&quot;</span><span class="br0">&#125;</span>, <span class="co1">// The name of the user</span></div>
</li>
<li class="li1">
<div class="de1">&nbsp; <span class="kw2">function</span><span class="br0">&#40;</span>newUser, textStatus, XMLHttpRequest<span class="br0">&#41;</span> <span class="br0">&#123;</span> </div>
</li>
<li class="li2">
<div class="de2">&nbsp; &nbsp; <span class="co1">// This is called when the post is over</span></div>
</li>
<li class="li1">
<div class="de1">&nbsp; &nbsp; <span class="kw2">var</span> newUserId = newUser.<span class="me1">id</span>;</div>
</li>
<li class="li1">
<div class="de1">&nbsp;</div>
</li>
<li class="li1">
<div class="de1">&nbsp; &nbsp; <span class="co1">// We begin by creating a function to delete the user</span></div>
</li>
<li class="li1">
<div class="de1">&nbsp; &nbsp; <span class="kw2">var</span> undoNewUser = <span class="kw2">function</span><span class="br0">&#40;</span><span class="br0">&#41;</span> <span class="br0">&#123;</span></div>
</li>
<li class="li2">
<div class="de2">&nbsp; &nbsp; &nbsp; $.<span class="me1">post</span><span class="br0">&#40;</span></div>
</li>
<li class="li1">
<div class="de1">&nbsp; &nbsp; &nbsp; &nbsp; <span class="st0">&quot;http://yoursite.com/users/delete&quot;</span>, <span class="co1">// The url to delete a user</span></div>
</li>
<li class="li1">
<div class="de1">&nbsp; &nbsp; &nbsp; &nbsp; <span class="br0">&#123;</span>id:newUserId<span class="br0">&#125;</span> <span class="co1">// The ID of the new user to delete</span></div>
</li>
<li class="li1">
<div class="de1">&nbsp; &nbsp; &nbsp; <span class="br0">&#41;</span>;</div>
</li>
<li class="li1">
<div class="de1">&nbsp; &nbsp; <span class="br0">&#125;</span></div>
</li>
<li class="li2">
<div class="de2">&nbsp;</div>
</li>
<li class="li1">
<div class="de1">&nbsp; &nbsp; <span class="co1">// We add the undo function to the stack</span></div>
</li>
<li class="li1">
<div class="de1">&nbsp; &nbsp; jskataUndo.<span class="me1">push</span><span class="br0">&#40;</span>undoNewUser<span class="br0">&#41;</span>;</div>
</li>
<li class="li1">
<div class="de1">&nbsp; <span class="br0">&#125;</span></div>
</li>
<li class="li1">
<div class="de1"><span class="br0">&#41;</span>;</div>
</li>
</ol>
</div>
<h4>2. Undo the last action you made</h4>
<div class="dean_ch" style="white-space: wrap;">
<ol>
<li class="li1">
<div class="de1">jsKata.<span class="me1">undo</span>.<span class="me1">undo</span><span class="br0">&#40;</span><span class="br0">&#41;</span>;</div>
</li>
</ol>
</div>
<h4>3. Events</h4>
<p>Each time there&#8217;s a change, an <em>onChange</em> event is called. To assign yours, simply write :</p>
<div class="dean_ch" style="white-space: wrap;">
<ol>
<li class="li1">
<div class="de1">jsKata.<span class="me1">undo</span>.<span class="me1">onChange</span> = <span class="kw2">function</span><span class="br0">&#40;</span><span class="br0">&#41;</span> <span class="br0">&#123;</span></div>
</li>
<li class="li1">
<div class="de1">&nbsp; <span class="co1">// Show the undo button</span></div>
</li>
<li class="li1">
<div class="de1">&nbsp; &nbsp;$<span class="br0">&#40;</span><span class="st0">&quot;#undoButton&quot;</span><span class="br0">&#41;</span>.<span class="me1">show</span><span class="br0">&#40;</span><span class="br0">&#41;</span>;</div>
</li>
<li class="li1">
<div class="de1"><span class="br0">&#125;</span></div>
</li>
</ol>
</div>
<p>There&#8217;s also an <em>onEmpty</em> event that is called when the stack of undoable actions is empty.</p>
<div class="dean_ch" style="white-space: wrap;">
<ol>
<li class="li1">
<div class="de1">jsKata.<span class="me1">undo</span>.<span class="me1">onEmpty</span> = <span class="kw2">function</span><span class="br0">&#40;</span><span class="br0">&#41;</span> <span class="br0">&#123;</span></div>
</li>
<li class="li1">
<div class="de1">&nbsp; <span class="co1">// Hide the undo button</span></div>
</li>
<li class="li1">
<div class="de1">&nbsp; &nbsp;$<span class="br0">&#40;</span><span class="st0">&quot;#undoButton&quot;</span><span class="br0">&#41;</span>.<span class="me1">hide</span><span class="br0">&#40;</span><span class="br0">&#41;</span>;</div>
</li>
<li class="li1">
<div class="de1"><span class="br0">&#125;</span></div>
</li>
</ol>
</div>
<h3>Complete demo</h3>
<p>There is a <a href="http://dsimard.github.com/jskata_examples/undo">complete demo on GitHub</a> as well as the <a href="http://github.com/dsimard/jskata_examples/blob/gh-pages/undo.html">HTML and Javascript</a> code for it. Take a look!</p>
]]></content:encoded>
			<wfw:commentRss>http://www.javascriptkata.com/2010/03/29/how-to-write-an-simple-undo-system-for-your-app/feed/</wfw:commentRss>
		<slash:comments>10</slash:comments>
		</item>
		<item>
		<title>Two google-like jquery calendars</title>
		<link>http://www.javascriptkata.com/2010/03/15/two-google-like-jquery-calendars/?utm_source=rss&#038;utm_medium=rss&#038;utm_campaign=two-google-like-jquery-calendars</link>
		<comments>http://www.javascriptkata.com/2010/03/15/two-google-like-jquery-calendars/#comments</comments>
		<pubDate>Mon, 15 Mar 2010 16:10:26 +0000</pubDate>
		<dc:creator>dsimard</dc:creator>
				<category><![CDATA[jquery]]></category>
		<category><![CDATA[white belt]]></category>

		<guid isPermaLink="false">http://www.javascriptkata.com/?p=183</guid>
		<description><![CDATA[Sometimes, it feels to me that javascript was invented for the only purpose of writing calendars to select a date.]]></description>
			<content:encoded><![CDATA[<p>Sometimes, it feels to me that <strong>javascript was invented for the only purpose of writing calendars</strong> to select a date. I wrote one myself (because I had to, not because I wanted to) in a previous job. I use a new calendar that is &#8220;better than the other one&#8221; on each of my project. It seems like the world will never run out of javascript calendars. In fact, you probably are writing a new one as you read this.</p>
<p>I want to talk about a different kind of calendar. The really hard one to write : <strong>calendars for displaying events</strong> in jquery.</p>
<h3>The pretty jquery-week-calendar</h3>
<p>Written by <a href="http://github.com/robmonie">Rob Monie</a>, this is the first one I tried. I was amazed about how easy it was to integrate with the current development of <a href="http://www.timmyontime.com" target="_blank">Timmy</a> (you can see an early beta when you&#8217;re logged <a href="http://www.timmyontime.com/dashboard/calendar" target="_blank">here</a>). You can <a href="http://robmonie.github.com/jquery-week-calendar/full_demo/weekcalendar_full_demo.html" target="_blank">take a look at the demo</a>. Unfortunately, I had unexplicable bugs and I didn&#8217;t take the time to understand them. Please, don&#8217;t do like me and <a href="http://github.com/robmonie/jquery-week-calendar" target="_blank">participate to the code on GitHub</a>.</p>
<ul>
<li>Looks good</li>
<li>Easy to integrate</li>
<li>May be buggy when drag-dropping/resizing</li>
</ul>
<h3>The robust fullcalendar</h3>
<p>We switched to this calendar, written by <a href="http://github.com/arshaw" target="_blank">Adam Shaw</a>, because it had three different views (month, week, day) and because we had some problem with the other calendar. It doesn&#8217;t look as good as the week-calendar (<a href="http://arshaw.com/fullcalendar/">demo</a>). You can also <a href="http://github.com/arshaw/fullcalendar" target="_blank">participate on GitHub</a>.</p>
<ul>
<li>The default look is OK</li>
<li>Harder to work with</li>
<li>Display may seem slow (on firefox)</li>
<li>More robust than week-calendar</li>
</ul>
<h3>Finally</h3>
<p>These guys have made a great job doing something really hard. Just think about two overlapping events or when an event is two minutes long or hundreds of special cases that could happen. Kudos to them for giving their time and talent to us.</p>
<h3>A word about git</h3>
<p>Our team (me and Frank, the <a href="http://www.rubyfleebie.com">RubyFleebie</a> guy) worked with SVN for a couple of years until Git stole the spotlight. For a couple of months, we worked with it and enjoyed it. Until we had to do &#8220;more advanced&#8221; things (branching, merging which is pretty basic to my sense) and things got incredibly hard. We switched to <a href="http://mercurial.selenic.com/">Mercurial</a> a couple weeks ago which feels more like the perfect mix between SVN and Git. So if, like us, you feel puzzled by Git, give Mercurial a chance.</p>
]]></content:encoded>
			<wfw:commentRss>http://www.javascriptkata.com/2010/03/15/two-google-like-jquery-calendars/feed/</wfw:commentRss>
		<slash:comments>3</slash:comments>
		</item>
		<item>
		<title>How to write a singleton class in javascript</title>
		<link>http://www.javascriptkata.com/2009/09/30/how-to-write-a-singleton-class-in-javascript/?utm_source=rss&#038;utm_medium=rss&#038;utm_campaign=how-to-write-a-singleton-class-in-javascript</link>
		<comments>http://www.javascriptkata.com/2009/09/30/how-to-write-a-singleton-class-in-javascript/#comments</comments>
		<pubDate>Wed, 30 Sep 2009 12:59:54 +0000</pubDate>
		<dc:creator>dsimard</dc:creator>
				<category><![CDATA[black belt]]></category>
		<category><![CDATA[function]]></category>
		<category><![CDATA[object-oriented]]></category>

		<guid isPermaLink="false">http://www.javascriptkata.com/?p=166</guid>
		<description><![CDATA[If I look at my stats, a lot of people are wondering how to write a singleton class. I already <a href="http://www.javascriptkata.com/2007/04/04/how-to-make-a-singleton/" target="_blank">wrote about it before</a> but my old solution exposed the instance of the class so more than one instance could be created thus making it not completly a singleton class.]]></description>
			<content:encoded><![CDATA[<div style="color:red;font-weight:bold;margin-bottom:1em;">[UPDATE : This post is outdated. Check out the <a href="http://www.javascriptkata.com/2010/10/20/alternatives-to-singletons-in-javascript/">new post on singletons</a>.]</div>
<p>If I look at my stats, a lot of people are wondering how to write a singleton class. I already <a href="http://www.javascriptkata.com/2007/04/04/how-to-make-a-singleton/" target="_blank">wrote about it before</a> but my old solution exposed the instance of the class so more than one instance could be created thus making it not completly a singleton class.</p>
<p>Here&#8217;s the new solution using a <a href="http://www.javascriptkata.com/2009/09/23/how-to-create-an-object-with-private-variables-and-methods/">private variable</a> for the instance.</p>
<p><script src="http://gist.github.com/1163955.js"></script></p>
]]></content:encoded>
			<wfw:commentRss>http://www.javascriptkata.com/2009/09/30/how-to-write-a-singleton-class-in-javascript/feed/</wfw:commentRss>
		<slash:comments>11</slash:comments>
		</item>
		<item>
		<title>How to create an object with private variables and methods</title>
		<link>http://www.javascriptkata.com/2009/09/23/how-to-create-an-object-with-private-variables-and-methods/?utm_source=rss&#038;utm_medium=rss&#038;utm_campaign=how-to-create-an-object-with-private-variables-and-methods</link>
		<comments>http://www.javascriptkata.com/2009/09/23/how-to-create-an-object-with-private-variables-and-methods/#comments</comments>
		<pubDate>Wed, 23 Sep 2009 12:50:52 +0000</pubDate>
		<dc:creator>dsimard</dc:creator>
				<category><![CDATA[black belt]]></category>
		<category><![CDATA[object-oriented]]></category>

		<guid isPermaLink="false">http://www.javascriptkata.com/?p=128</guid>
		<description><![CDATA[In short, you can use private variables when you return another scope when declaring a class.]]></description>
			<content:encoded><![CDATA[<p>In short, you can use private variables when you return another scope when declaring a class.</p>
<div class="dean_ch" style="white-space: wrap;">
<ol>
<li class="li1">
<div class="de1"><span class="kw2">function</span> Cats<span class="br0">&#40;</span><span class="br0">&#41;</span> <span class="br0">&#123;</span></div>
</li>
<li class="li1">
<div class="de1">&nbsp; <span class="kw2">var</span> nameList = <span class="br0">&#91;</span><span class="br0">&#93;</span>; <span class="co1">// private var</span></div>
</li>
<li class="li1">
<div class="de1">&nbsp; &nbsp; &nbsp; &nbsp; </div>
</li>
<li class="li1">
<div class="de1">&nbsp; <span class="co1">// This is where you define another scope!</span></div>
</li>
<li class="li2">
<div class="de2">&nbsp; <span class="kw1">return</span> <span class="br0">&#123;</span></div>
</li>
<li class="li1">
<div class="de1">&nbsp; &nbsp; add:<span class="kw2">function</span><span class="br0">&#40;</span><span class="kw3">name</span><span class="br0">&#41;</span> <span class="br0">&#123;</span></div>
</li>
<li class="li1">
<div class="de1">&nbsp; &nbsp; &nbsp; nameList.<span class="me1">push</span><span class="br0">&#40;</span><span class="kw3">name</span><span class="br0">&#41;</span>;</div>
</li>
<li class="li1">
<div class="de1">&nbsp; &nbsp; <span class="br0">&#125;</span></div>
</li>
<li class="li1">
<div class="de1">&nbsp; <span class="br0">&#125;</span>&nbsp; &nbsp; &nbsp;</div>
</li>
<li class="li2">
<div class="de2"><span class="br0">&#125;</span></div>
</li>
</ol>
</div>
<h3>How does it work?</h3>
<p>The magic lies in creating a different scope at the end of the class definition that does not include private variables. Then, private members are <strong>available in this scope and not outside of it</strong>, thanks to the <a href="http://www.javascriptkata.com/2007/04/10/the-power-of-closures-in-javascript/" target="_blank">power of closures</a>.</p>
<h3>Differences between private and public</h3>
<p>These two classes definition shows the difference between the a class where all members are public versus a class where some members are private.</p>
<p>This is a class where <strong>all members are public</strong>.</p>
<div class="dean_ch" style="white-space: wrap;">
<ol>
<li class="li1">
<div class="de1"><span class="kw2">function</span> PublicCats<span class="br0">&#40;</span><span class="br0">&#41;</span> <span class="br0">&#123;</span></div>
</li>
<li class="li1">
<div class="de1">&nbsp; <span class="co1">// This is the list of cat names</span></div>
</li>
<li class="li1">
<div class="de1">&nbsp; <span class="kw1">this</span>.<span class="me1">nameList</span> = <span class="br0">&#91;</span><span class="br0">&#93;</span>;</div>
</li>
<li class="li1">
<div class="de1">&nbsp; </div>
</li>
<li class="li2">
<div class="de2">&nbsp; <span class="co1">// This is a method that I would like to be private but can&#8217;t</span></div>
</li>
<li class="li1">
<div class="de1">&nbsp; <span class="co1">// It returns the last cat of the list</span></div>
</li>
<li class="li1">
<div class="de1">&nbsp; <span class="kw1">this</span>.<span class="me1">lastCat</span> = <span class="kw2">function</span><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; <span class="kw1">return</span> <span class="kw1">this</span>.<span class="me1">nameList</span><span class="br0">&#91;</span><span class="kw1">this</span>.<span class="me1">nameList</span>.<span class="me1">length</span><span class="nu0">-1</span><span class="br0">&#93;</span>;</div>
</li>
<li class="li1">
<div class="de1">&nbsp; <span class="br0">&#125;</span></div>
</li>
<li class="li2">
<div class="de2">&nbsp; </div>
</li>
<li class="li1">
<div class="de1">&nbsp; <span class="co1">// Return the list of names</span></div>
</li>
<li class="li1">
<div class="de1">&nbsp; <span class="kw1">this</span>.<span class="me1">names</span> = <span class="kw2">function</span><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; <span class="kw1">return</span> <span class="kw1">this</span>.<span class="me1">nameList</span>;</div>
</li>
<li class="li1">
<div class="de1">&nbsp; <span class="br0">&#125;</span></div>
</li>
<li class="li2">
<div class="de2">&nbsp; </div>
</li>
<li class="li1">
<div class="de1">&nbsp; <span class="co1">// Add a name to the list</span></div>
</li>
<li class="li1">
<div class="de1">&nbsp; <span class="kw1">this</span>.<span class="me1">add</span> = <span class="kw2">function</span><span class="br0">&#40;</span><span class="kw3">name</span><span class="br0">&#41;</span> <span class="br0">&#123;</span></div>
</li>
<li class="li1">
<div class="de1">&nbsp; &nbsp; <span class="kw1">this</span>.<span class="me1">nameList</span>.<span class="me1">push</span><span class="br0">&#40;</span><span class="kw3">name</span><span class="br0">&#41;</span>;</div>
</li>
<li class="li1">
<div class="de1">&nbsp; &nbsp; </div>
</li>
<li class="li2">
<div class="de2">&nbsp; &nbsp; <span class="co1">// Return the last cat just added</span></div>
</li>
<li class="li1">
<div class="de1">&nbsp; &nbsp; <span class="kw1">return</span> <span class="kw1">this</span>.<span class="me1">lastCat</span><span class="br0">&#40;</span><span class="br0">&#41;</span>;</div>
</li>
<li class="li1">
<div class="de1">&nbsp; <span class="br0">&#125;</span> &nbsp;</div>
</li>
<li class="li1">
<div class="de1"><span class="br0">&#125;</span></div>
</li>
</ol>
</div>
<p>This is the corresponding class where <strong>some members are private</strong>.</p>
<div class="dean_ch" style="white-space: wrap;">
<ol>
<li class="li1">
<div class="de1"><span class="kw2">function</span> PrivateCats<span class="br0">&#40;</span><span class="br0">&#41;</span> <span class="br0">&#123;</span></div>
</li>
<li class="li1">
<div class="de1">&nbsp; <span class="co1">// This is the list of cat names</span></div>
</li>
<li class="li1">
<div class="de1">&nbsp; <span class="kw2">var</span> nameList = <span class="br0">&#91;</span><span class="br0">&#93;</span>;</div>
</li>
<li class="li1">
<div class="de1">&nbsp; </div>
</li>
<li class="li2">
<div class="de2">&nbsp; <span class="co1">// This is a private method</span></div>
</li>
<li class="li1">
<div class="de1">&nbsp; <span class="kw2">var</span> lastCat = <span class="kw2">function</span><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; <span class="co1">// Note : I don&#8217;t use &quot;this&quot; to access private variables</span></div>
</li>
<li class="li1">
<div class="de1">&nbsp; &nbsp; <span class="co1">// thanks to the power of closures!</span></div>
</li>
<li class="li1">
<div class="de1">&nbsp; &nbsp; <span class="kw1">return</span> nameList<span class="br0">&#91;</span>nameList.<span class="me1">length</span><span class="nu0">-1</span><span class="br0">&#93;</span>;</div>
</li>
<li class="li2">
<div class="de2">&nbsp; <span class="br0">&#125;</span></div>
</li>
<li class="li1">
<div class="de1">&nbsp; </div>
</li>
<li class="li1">
<div class="de1">&nbsp; <span class="co1">// These are our public methods!</span></div>
</li>
<li class="li1">
<div class="de1">&nbsp; <span class="co1">// This is where we create another scope to</span></div>
</li>
<li class="li1">
<div class="de1">&nbsp; <span class="co1">// avoid external objects to use the private variables.</span></div>
</li>
<li class="li2">
<div class="de2">&nbsp; <span class="kw1">return</span> <span class="br0">&#123;</span></div>
</li>
<li class="li1">
<div class="de1">&nbsp; &nbsp; add:<span class="kw2">function</span><span class="br0">&#40;</span><span class="kw3">name</span><span class="br0">&#41;</span> <span class="br0">&#123;</span></div>
</li>
<li class="li1">
<div class="de1">&nbsp; &nbsp; &nbsp; <span class="co1">// Note : once again, I don&#8217;t use &quot;this&quot; </span></div>
</li>
<li class="li1">
<div class="de1">&nbsp; &nbsp; &nbsp; <span class="co1">// to access the private variables and methods</span></div>
</li>
<li class="li1">
<div class="de1">&nbsp; &nbsp; &nbsp; nameList.<span class="me1">push</span><span class="br0">&#40;</span><span class="kw3">name</span><span class="br0">&#41;</span>;</div>
</li>
<li class="li2">
<div class="de2">&nbsp; &nbsp; &nbsp; <span class="kw1">return</span> lastCat<span class="br0">&#40;</span><span class="br0">&#41;</span>;</div>
</li>
<li class="li1">
<div class="de1">&nbsp; &nbsp; <span class="br0">&#125;</span>,</div>
</li>
<li class="li1">
<div class="de1">&nbsp; &nbsp; names:<span class="kw2">function</span><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; <span class="kw1">return</span> nameList;</div>
</li>
<li class="li1">
<div class="de1">&nbsp; &nbsp; <span class="br0">&#125;</span></div>
</li>
<li class="li2">
<div class="de2">&nbsp; <span class="br0">&#125;</span> &nbsp;</div>
</li>
<li class="li1">
<div class="de1"><span class="br0">&#125;</span></div>
</li>
</ol>
</div>
<p>In the above code, line 15 makes all the difference between the two classes.</p>
<h3>On GitHub</h3>
<p>Get all the <a href="http://github.com/dsimard/jskata_examples/blob/master/private/private.js">code on GitHub</a> and see it live in action on <a href="http://dsimard.github.com/jskata_examples/private.html">my GitHub page</a>.</p>
]]></content:encoded>
			<wfw:commentRss>http://www.javascriptkata.com/2009/09/23/how-to-create-an-object-with-private-variables-and-methods/feed/</wfw:commentRss>
		<slash:comments>14</slash:comments>
		</item>
	</channel>
</rss>

