<?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>Javascript Kata &#187; black belt</title>
	<atom:link href="http://www.javascriptkata.com/category/level/black-belt/feed/" rel="self" type="application/rss+xml" />
	<link>http://www.javascriptkata.com</link>
	<description>Advanced katas for javascripters</description>
	<lastBuildDate>Tue, 15 Jun 2010 14:21:03 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.0</generator>
		<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/</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>Dan</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>6</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/</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>Dan</dc:creator>
				<category><![CDATA[black belt]]></category>
		<category><![CDATA[jskata]]></category>
		<category><![CDATA[librairies]]></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>4</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/</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>Dan</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>9</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/</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>Dan</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[<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>
<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> names = <span class="br0">&#91;</span><span class="br0">&#93;</span>;</div>
</li>
<li class="li1">
<div class="de1">&nbsp; </div>
</li>
<li class="li1">
<div class="de1">&nbsp; <span class="kw1">this</span>.<span class="me1">singletonInstance</span> = <span class="kw2">null</span>;</div>
</li>
<li class="li2">
<div class="de2">&nbsp;</div>
</li>
<li class="li1">
<div class="de1">&nbsp; <span class="co1">// Get the instance of the Cats class</span></div>
</li>
<li class="li1">
<div class="de1">&nbsp; <span class="co1">// If there&#8217;s none, instanciate one</span></div>
</li>
<li class="li1">
<div class="de1">&nbsp; <span class="kw2">var</span> getInstance = <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">if</span> <span class="br0">&#40;</span>!<span class="kw1">this</span>.<span class="me1">singletonInstance</span><span class="br0">&#41;</span> <span class="br0">&#123;</span> </div>
</li>
<li class="li2">
<div class="de2">&nbsp; &nbsp; &nbsp; <span class="kw1">this</span>.<span class="me1">singletonInstance</span> = createInstance<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; <span class="kw1">return</span> <span class="kw1">this</span>.<span class="me1">singletonInstance</span>;</div>
</li>
<li class="li1">
<div class="de1">&nbsp; <span class="br0">&#125;</span></div>
</li>
<li class="li1">
<div class="de1">&nbsp; </div>
</li>
<li class="li2">
<div class="de2">&nbsp; <span class="co1">// Create an instance of the Cats class</span></div>
</li>
<li class="li1">
<div class="de1">&nbsp; <span class="kw2">var</span> createInstance = <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">// Here, you return all public methods and variables</span></div>
</li>
<li class="li1">
<div class="de1">&nbsp; &nbsp; <span class="kw1">return</span> <span class="br0">&#123;</span></div>
</li>
<li class="li1">
<div class="de1">&nbsp; &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="li2">
<div class="de2">&nbsp; &nbsp; &nbsp; &nbsp; names.<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; &nbsp; &nbsp; <span class="kw1">return</span> <span class="kw1">this</span>.<span class="me1">names</span><span class="br0">&#40;</span><span class="br0">&#41;</span>;</div>
</li>
<li class="li1">
<div class="de1">&nbsp; &nbsp; &nbsp; <span class="br0">&#125;</span>,</div>
</li>
<li class="li1">
<div class="de1">&nbsp; &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; &nbsp; <span class="kw1">return</span> names;</div>
</li>
<li class="li2">
<div class="de2">&nbsp; &nbsp; &nbsp; <span class="br0">&#125;</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></div>
</li>
<li class="li1">
<div class="de1">&nbsp; </div>
</li>
<li class="li1">
<div class="de1">&nbsp; <span class="kw1">return</span> getInstance<span class="br0">&#40;</span><span class="br0">&#41;</span>;</div>
</li>
<li class="li2">
<div class="de2"><span class="br0">&#125;</span></div>
</li>
</ol>
</div>
<h3>How to use it</h3>
<div class="dean_ch" style="white-space: wrap;">
<ol>
<li class="li1">
<div class="de1"><span class="kw2">function</span> run<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">// Add a new cat</span></div>
</li>
<li class="li1">
<div class="de1">&nbsp; <span class="kw2">var</span> cat1 = <span class="kw2">new</span> Cats<span class="br0">&#40;</span><span class="br0">&#41;</span>;</div>
</li>
<li class="li1">
<div class="de1">&nbsp; cat1.<span class="me1">add</span><span class="br0">&#40;</span><span class="st0">&quot;Mistigri&quot;</span><span class="br0">&#41;</span>;</div>
</li>
<li class="li2">
<div class="de2">&nbsp; jsKataEx.<span class="me1">assert</span><span class="br0">&#40;</span>cat1.<span class="me1">names</span><span class="br0">&#40;</span><span class="br0">&#41;</span>.<span class="me1">length</span> == <span class="nu0">1</span>, <span class="st0">&quot;cat1 contains 1 cat : &quot;</span> + cat1.<span class="me1">names</span><span class="br0">&#40;</span><span class="br0">&#41;</span>.<span class="me1">toString</span><span class="br0">&#40;</span><span class="br0">&#41;</span><span class="br0">&#41;</span>;</div>
</li>
<li class="li1">
<div class="de1">&nbsp; &nbsp;</div>
</li>
<li class="li1">
<div class="de1">&nbsp; <span class="co1">// Use another instance</span></div>
</li>
<li class="li1">
<div class="de1">&nbsp; <span class="kw2">var</span> cat2 = <span class="kw2">new</span> Cats<span class="br0">&#40;</span><span class="br0">&#41;</span>;</div>
</li>
<li class="li1">
<div class="de1">&nbsp; jsKataEx.<span class="me1">assert</span><span class="br0">&#40;</span>cat2.<span class="me1">names</span><span class="br0">&#40;</span><span class="br0">&#41;</span>.<span class="me1">length</span> == <span class="nu0">1</span>, <span class="st0">&quot;cat2 contains Mistigri added in cat1 : &quot;</span> + cat2.<span class="me1">names</span><span class="br0">&#40;</span><span class="br0">&#41;</span>.<span class="me1">toString</span><span class="br0">&#40;</span><span class="br0">&#41;</span><span class="br0">&#41;</span>;</div>
</li>
<li class="li2">
<div class="de2">&nbsp; &nbsp;</div>
</li>
<li class="li1">
<div class="de1">&nbsp; <span class="co1">// Add another cat in the other instance</span></div>
</li>
<li class="li1">
<div class="de1">&nbsp; cat2.<span class="me1">add</span><span class="br0">&#40;</span><span class="st0">&quot;Felix&quot;</span><span class="br0">&#41;</span>;</div>
</li>
<li class="li1">
<div class="de1">&nbsp; jsKataEx.<span class="me1">assert</span><span class="br0">&#40;</span>cat2.<span class="me1">names</span><span class="br0">&#40;</span><span class="br0">&#41;</span>.<span class="me1">length</span> == <span class="nu0">2</span>, <span class="st0">&quot;cat2 contains Mistigri and Felix&quot;</span> + cat2.<span class="me1">names</span><span class="br0">&#40;</span><span class="br0">&#41;</span>.<span class="me1">toString</span><span class="br0">&#40;</span><span class="br0">&#41;</span><span class="br0">&#41;</span>;</div>
</li>
<li class="li1">
<div class="de1">&nbsp; jsKataEx.<span class="me1">assert</span><span class="br0">&#40;</span>cat2.<span class="me1">names</span><span class="br0">&#40;</span><span class="br0">&#41;</span>.<span class="me1">length</span> == <span class="nu0">2</span>, <span class="st0">&quot;cat1 also contains Mistigri and Felix&quot;</span> + cat1.<span class="me1">names</span><span class="br0">&#40;</span><span class="br0">&#41;</span>.<span class="me1">toString</span><span class="br0">&#40;</span><span class="br0">&#41;</span><span class="br0">&#41;</span>;</div>
</li>
<li class="li2">
<div class="de2"><span class="br0">&#125;</span></div>
</li>
</ol>
</div>
<p>Get the <a href="http://github.com/dsimard/jskata_examples/blob/master/singleton/singleton.js" target="_blank">code on GitHub</a> and see it <a href="http://dsimard.github.com/jskata_examples/singleton.html" target="_blank">live in action</a>.</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>6</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/</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>Dan</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>11</slash:comments>
		</item>
		<item>
		<title>How to use JSON (updated with example)</title>
		<link>http://www.javascriptkata.com/2009/09/16/how-to-use-json-updated-with-example/</link>
		<comments>http://www.javascriptkata.com/2009/09/16/how-to-use-json-updated-with-example/#comments</comments>
		<pubDate>Wed, 16 Sep 2009 15:14:48 +0000</pubDate>
		<dc:creator>Dan</dc:creator>
				<category><![CDATA[ajax]]></category>
		<category><![CDATA[black belt]]></category>
		<category><![CDATA[librairies]]></category>

		<guid isPermaLink="false">http://www.javascriptkata.com/?p=91</guid>
		<description><![CDATA[This post is an update from the <a href="/2007/05/08/how-to-use-json/" target="_self">old post</a>. A lot of things changed since it was written and the information in the old one is a bit outdated.]]></description>
			<content:encoded><![CDATA[<p>This post is an update from the <a href="/2007/05/08/how-to-use-json/" target="_self">old post</a>. A lot of things changed since it was written and the information in the old one is a bit outdated.</p>
<p>First of all, <strong>you should never use an <em>eval()</em> when using JSON</strong> because of security reasons that I will talk about later in another post. [edit : jQuery uses <em>eval()</em> but there's a work-around that will be talked about in another post.]</p>
<h3>JSON and jQuery</h3>
<p>The simplest way to use JSON is though <a href="http://jquery.com/" target="_blank">jQuery</a>. I have been using for <a href="http://www.javascriptkata.com/2007/05/29/3-reasons-why-i-use-jquery/">more than 2 years</a> and I never was disappointed.</p>
<p>Let&#8217;s say I want to call <a href="http://www.flickr.com/services/api/flickr.photos.search.html" target="_blank">Flickr search</a> with JSON. It&#8217;s simple :</p>
<div class="dean_ch" style="white-space: wrap;">
<ol>
<li class="li1">
<div class="de1">$.<span class="me1">getJSON</span><span class="br0">&#40;</span></div>
</li>
<li class="li1">
<div class="de1">&nbsp; &nbsp; &nbsp; &nbsp; <span class="st0">&quot;http://api.flickr.com/services/rest/?jsoncallback=?&quot;</span>,</div>
</li>
<li class="li1">
<div class="de1">&nbsp; &nbsp; &nbsp; &nbsp; <span class="br0">&#123;</span> method : <span class="st0">&quot;flickr.photos.search&quot;</span>,</div>
</li>
<li class="li1">
<div class="de1">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; api_key : <span class="st0">&quot;4ef2fe2affcdd6e13218f5ddd0e2500d&quot;</span>,</div>
</li>
<li class="li2">
<div class="de2">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; format : <span class="st0">&quot;json&quot;</span>,</div>
</li>
<li class="li1">
<div class="de1">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; tags : <span class="st0">&quot;landscape&quot;</span>,</div>
</li>
<li class="li1">
<div class="de1">&nbsp; &nbsp; &nbsp; &nbsp; <span class="br0">&#125;</span>,</div>
</li>
<li class="li1">
<div class="de1">&nbsp; &nbsp; &nbsp; &nbsp; ajaxCallBack</div>
</li>
<li class="li1">
<div class="de1"><span class="br0">&#41;</span>;</div>
</li>
<li class="li2">
<div class="de2">&nbsp;</div>
</li>
</ol>
</div>
<p>The <em>$.getJSON</em> takes 3 parameters.</p>
<p><strong>1. The URL</strong><br />
The URL of the remote call.</p>
<p><strong>2. The parameters</strong><br />
The parameters to complete the call. On the <a href="http://www.flickr.com/services/api/flickr.photos.search.html" target="_blank">documentation page</a> about this Flickr method, you have a list of all the parameters you can use. For a JSON call on Flickr, you have to send the <strong>api_key</strong> and the <strong>format</strong>. I added the parameter <strong>tags</strong> to send me all photos that are tagged <em>landscape</em>.</p>
<p><strong>3. The callback</strong><br />
Most API services now requires a callback, a method that will be called when the JSON is loaded. It is more secure and easier to work with. At the end of the first parameter (the URL), I added <em>jsoncallback=?</em> (flickr called this parameter <em>jsoncallback</em> but it could be named differently on other services). This is the jQuery-way of saying &#8220;When you finish loading the JSON, call the method specificied by the third argument&#8221;.</p>
<p>You may wonder how Flickr knows which function to use on callback. It&#8217;s because in jQuery, there&#8217;s a special case for that. In the URL, you can see that the <em>jsoncallback</em> is written like that <strong>jsoncallback=?</strong>. The <strong>?</strong> is replaced by your own callback method (in my case, I called it <em>ajaxCallback</em>)</p>
<h3>The callback method</h3>
<p>The callback method I used looks like this :</p>
<div class="dean_ch" style="white-space: wrap;">
<ol>
<li class="li1">
<div class="de1"><span class="kw2">function</span> ajaxCallBack<span class="br0">&#40;</span>data<span class="br0">&#41;</span> <span class="br0">&#123;</span></div>
</li>
<li class="li1">
<div class="de1">&nbsp; &nbsp; &nbsp; &nbsp; <span class="co1">// Loop throught all photos and display them</span></div>
</li>
<li class="li1">
<div class="de1">&nbsp; &nbsp; &nbsp; &nbsp; <span class="co1">// it uses the jquery.each method </span></div>
</li>
<li class="li1">
<div class="de1">&nbsp; &nbsp; &nbsp; &nbsp; <span class="co1">// doc at http://docs.jquery.com/Utilities/jQuery.each</span></div>
</li>
<li class="li2">
<div class="de2">&nbsp; &nbsp; &nbsp; &nbsp; $.<span class="me1">each</span><span class="br0">&#40;</span>data.<span class="me1">photos</span>.<span class="me1">photo</span>, <span class="kw2">function</span><span class="br0">&#40;</span>photoIdx, photo<span class="br0">&#41;</span> <span class="br0">&#123;</span></div>
</li>
<li class="li1">
<div class="de1">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span class="co1">// Build the thumbnail url</span></div>
</li>
<li class="li1">
<div class="de1">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span class="kw2">var</span> thumb_url = <span class="br0">&#91;</span><span class="st0">&quot;http://farm&quot;</span>, photo.<span class="me1">farm</span>, <span class="st0">&quot;.static.flickr.com/&quot;</span>, </div>
</li>
<li class="li1">
<div class="de1">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; photo.<span class="me1">server</span>, <span class="st0">&quot;/&quot;</span>, photo.<span class="me1">id</span>, <span class="st0">&quot;_&quot;</span>, photo.<span class="me1">secret</span>, <span class="st0">&quot;_t.jpg&quot;</span><span class="br0">&#93;</span>.<span class="me1">join</span><span class="br0">&#40;</span><span class="st0">&quot;&quot;</span><span class="br0">&#41;</span>;</div>
</li>
<li class="li1">
<div class="de1">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; </div>
</li>
<li class="li2">
<div class="de2">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span class="co1">// Build the photo url</span></div>
</li>
<li class="li1">
<div class="de1">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span class="kw2">var</span> photo_url = <span class="br0">&#91;</span><span class="st0">&quot;http://www.flickr.com/photos/&quot;</span>, </div>
</li>
<li class="li1">
<div class="de1">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; photo.<span class="me1">owner</span>, <span class="st0">&quot;/&quot;</span>, photo.<span class="me1">id</span><span class="br0">&#93;</span>.<span class="me1">join</span><span class="br0">&#40;</span><span class="st0">&quot;&quot;</span><span class="br0">&#41;</span></div>
</li>
<li class="li1">
<div class="de1">&nbsp;</div>
</li>
<li class="li1">
<div class="de1">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span class="co1">// Build HTML</span></div>
</li>
<li class="li2">
<div class="de2">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; $<span class="br0">&#40;</span><span class="st0">&quot;body&quot;</span><span class="br0">&#41;</span>.<span class="me1">append</span><span class="br0">&#40;</span></div>
</li>
<li class="li1">
<div class="de1">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; $<span class="br0">&#40;</span><span class="st0">&quot;&lt;a&gt;&quot;</span><span class="br0">&#41;</span></div>
</li>
<li class="li1">
<div class="de1">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; .<span class="me1">attr</span><span class="br0">&#40;</span><span class="st0">&quot;href&quot;</span>, photo_url<span class="br0">&#41;</span></div>
</li>
<li class="li1">
<div class="de1">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; .<span class="me1">attr</span><span class="br0">&#40;</span><span class="st0">&quot;target&quot;</span>, <span class="st0">&quot;_blank&quot;</span><span class="br0">&#41;</span></div>
</li>
<li class="li1">
<div class="de1">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; .<span class="me1">append</span><span class="br0">&#40;</span></div>
</li>
<li class="li2">
<div class="de2">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; $<span class="br0">&#40;</span><span class="st0">&quot;&lt;img&gt;&quot;</span><span class="br0">&#41;</span>.<span class="me1">attr</span><span class="br0">&#40;</span><span class="st0">&quot;src&quot;</span>, thumb_url<span class="br0">&#41;</span></div>
</li>
<li class="li1">
<div class="de1">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span class="br0">&#41;</span></div>
</li>
<li class="li1">
<div class="de1">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span class="br0">&#41;</span></div>
</li>
<li class="li1">
<div class="de1">&nbsp; &nbsp; &nbsp; &nbsp; <span class="br0">&#125;</span><span class="br0">&#41;</span>;</div>
</li>
<li class="li1">
<div class="de1"><span class="br0">&#125;</span></div>
</li>
<li class="li2">
<div class="de2">&nbsp;</div>
</li>
</ol>
</div>
<p>If you know jQuery a little, it simply creates the HTML to show the thumbnails and a link to the photo.</p>
<h3>Get the code</h3>
<p>You can get the complete code for this example on <a href="http://github.com/dsimard/jskata_examples/tree/simple" target="_blank">GitHub</a> and a <a href="http://dsimard.github.com/jskata_examples/flickr_json.html">working example</a>.</p>
]]></content:encoded>
			<wfw:commentRss>http://www.javascriptkata.com/2009/09/16/how-to-use-json-updated-with-example/feed/</wfw:commentRss>
		<slash:comments>4</slash:comments>
		</item>
		<item>
		<title>How to have jQuery and prototype.js in the same project</title>
		<link>http://www.javascriptkata.com/2008/01/28/how-to-have-jquery-and-prototypejs-in-the-same-project/</link>
		<comments>http://www.javascriptkata.com/2008/01/28/how-to-have-jquery-and-prototypejs-in-the-same-project/#comments</comments>
		<pubDate>Mon, 28 Jan 2008 11:02:41 +0000</pubDate>
		<dc:creator>Dan</dc:creator>
				<category><![CDATA[black belt]]></category>
		<category><![CDATA[how to]]></category>
		<category><![CDATA[librairies]]></category>

		<guid isPermaLink="false">http://www.javascriptkata.com/2008/01/28/how-to-have-jquery-and-prototypejs-in-the-same-project/</guid>
		<description><![CDATA[I&#8217;m a big fan of jQuery. This librarie is just the best and simplest one around. I really noticed it when I wanted to get rid of jQuery in TimmyOnTime and try to use prototype.js instead, just to be more &#8220;rails-oriented&#8221; (that&#8217;s a pretty lame excuse don&#8217;t you think?) Why I didn&#8217;t like prototype.js There [...]]]></description>
			<content:encoded><![CDATA[<p>I&#8217;m a big fan of <a href="http://jquery.com/" target="_blank">jQuery</a>. This librarie is just the <strong>best</strong> and <strong>simplest</strong> one around. I really noticed it when I wanted to get rid of jQuery in <a href="http://timmyontime.com/" target="_blank">TimmyOnTime</a> and try to use <a href="http://www.prototypejs.org/" target="_blank">prototype.js</a> instead, just to be more &#8220;rails-oriented&#8221; (that&#8217;s a pretty lame excuse don&#8217;t you think?)</p>
<h3>Why I didn&#8217;t like prototype.js</h3>
<p>There was not a lot of javascript written for the project so I thought that getting rid of jQuery and using prototype.js would be easy. It turned out to be hell on earth. <strong>Prototype.js made me feel like I was back in the 90s writing C++</strong>. A simple click event turns out to be a awful lot of ugly code with ugly function names.</p>
<p>Example, <strong>if I wanted to show the content of a DIV I clicked</strong> in prototype.js, I would use things like <a href="http://www.prototypejs.org/api/event/observe" target="_blank">Event.observe</a>, <a href="http://www.prototypejs.org/api/function/bindAsEventListener" target="_blank">bindAsEventListener</a>, a mix of native DOM element and prototype.js element, and worst of all, an <a href="http://www.prototypejs.org/api">unintelligible documentation</a>.</p>
<p>jQuery as a complete different way of doing it in a single easy-to-understand line :<br />
<em>$(&#8220;#a_div&#8221;).click(function() { alert($(this).text()); });</em>.<br />
As simple as that!</p>
<h3>The problem</h3>
<p>I wanted to use some interface element in Rails that requires prototype.js so I had to have both librairies. The problem is that there&#8217;s a conflict between them. Prototype.js doesn&#8217;t seem to give a damn about it but jQuery is nicer with you. It offers a <a href="http://docs.jquery.com/Core/jQuery.noConflict" target="_blank">noConflict</a> method.</p>
<p><strong>With Rails</strong>, prototype.js is the default librairie so to override this, you could do</p>
<div class="dean_ch" style="white-space: wrap;">
<ol>
<li class="li1">
<div class="de1">&lt;%= javascript_include_tag &quot;prototype&quot;, &quot;jquery&quot; %&gt;
</div>
</li>
<li class="li1">
<div class="de1">&lt;script&gt;$j = jQuery.noConflict();&lt;/script&gt;</div>
</li>
</ol>
</div>
<p>This way, you will have to use <strong>$j</strong> instead of <strong>$</strong> to call jQuery.<strong>$</strong> would still call prototype.</p>
<p>Another thing you could do is to use <a href="http://ennerchi.com/projects/jrails" target="_blank">jQuery on Rails</a> and don&#8217;t give a damn about scriptaculous. I personally prefer to use librairies that are fully tested instead of using a &#8220;by-pass&#8221; that could break my code. It&#8217;s your choice&#8230;</p>
]]></content:encoded>
			<wfw:commentRss>http://www.javascriptkata.com/2008/01/28/how-to-have-jquery-and-prototypejs-in-the-same-project/feed/</wfw:commentRss>
		<slash:comments>5</slash:comments>
		</item>
		<item>
		<title>Ask Dan : More on javascript threading</title>
		<link>http://www.javascriptkata.com/2007/12/06/ask-dan-more-on-javascript-threading/</link>
		<comments>http://www.javascriptkata.com/2007/12/06/ask-dan-more-on-javascript-threading/#comments</comments>
		<pubDate>Thu, 06 Dec 2007 14:05:20 +0000</pubDate>
		<dc:creator>Dan</dc:creator>
				<category><![CDATA[Ask Dan a javascript question]]></category>
		<category><![CDATA[ajax]]></category>
		<category><![CDATA[black belt]]></category>

		<guid isPermaLink="false">http://javascriptkata.timmyontime.com/?p=31</guid>
		<description><![CDATA[From time to time, I receive emails from desperate people who want help with their javascript problem. I also receive a lot of emails of people wanting to help me with my &#8220;manly problems&#8221;. It&#8217;s very nice from them to care about me and I take time to reply to each of them but I [...]]]></description>
			<content:encoded><![CDATA[<p>From time to time, I receive emails from desperate people who want help with their javascript problem. I also receive a lot of emails of people wanting to help me with my &#8220;manly problems&#8221;. It&#8217;s very nice from them to care about me and I take time to reply to each of them but I don&#8217;t have that kind of problem for the moment.</p>
<p><strong>Stuart Cooper</strong> recently sent me a mail about threading in javascript and I turn to you, javascripters, to find the a solution for his problem. I <a href="http://refactormycode.com/codes/172-javascript-threading" target="_blank">sent the code on RefactorMyCode for refactorisation</a> and they will automatically show up in the comments of this post (the site is an idea of <a href="http://macournoyer.wordpress.com/" target="_blank">Marc-AndrÃ© Cournoyer</a> who is a very active developer from Montreal). <strong>Don&#8217;t be afraid to think outside the box</strong>. The solution may be completly different from what is already written.</p>
<hr /><em> I am experience trying to create a &#8220;cover page&#8221; that runs a ping to remote servers.In my code I am using setInterval() to run repeating pings (artificially quickly at the moment, ie 10 seconds rather than 1 min) and display the results to browser.  I have built my own XMLHttpRequest module (mainly as a learning exercise) and I don&#8217;t believe it to be the source of my ills.</em><em> </em><em>I have read through your article and the behaviour of alert() and confirm() fits exactly with what I am seeing when I run the following code :</em><em>[snippet]</em></p>
<p><em>function startup(){</em></p>
<p><em>setInterval(&#8220;pinger(&#8216;live&#8217;,0)&#8221;,10000);<br />
setInterval(&#8220;pinger(&#8216;standby&#8217;,1)&#8221;,10000);<br />
setInterval(&#8220;pinger(&#8216;dev&#8217;,2)&#8221;,10000);<br />
setInterval(&#8220;pinger(&#8216;test1&#8242;,3)&#8221;,10000);<br />
setInterval(&#8220;pinger(&#8216;test2&#8242;,4)&#8221;,10000);<br />
setInterval(&#8220;pinger(&#8216;test3&#8242;,5)&#8221;,10000);<br />
setInterval(&#8220;pinger(&#8216;test4&#8242;,6)&#8221;,10000);</em></p>
<p><em>}</em></p>
<p><em>function pinger(server,divit){</em></p>
<p><em>console=document.getElementById(&#8216;ping_div&#8217; + divit);<br />
console.innerHTML=&#8221;;<br />
sendRequest(&#8220;../php/ping_sys2.php?target=&#8221; + server);</em></p>
<p><em>}</em></p>
<p><em>[/snippet]</em></p>
<p><em>(each server has its own named div to return to)</em></p>
<p><em>What I am seeing when I run this is the final pinger response from the callback and nothing else.  I since added debugging into my XMLHttpRequest code so that it split out the server response progress.</em></p>
<p><em>[snippet]</em></p>
<p><em>readyXML=xmlReq.readyState;</em></p>
<p><em>[/snippet]</em></p>
<p><em>[snippet]</em></p>
<p><em>if(readyXML == 3){</em></p>
<p><em>data=&#8221;Serving &#8230;&#8221;;</em></p>
<p><em>}</em></p>
<p><em>if(readyXML == 2){</em></p>
<p><em>data=&#8221;Sent &#8230;&#8221;;</em></p>
<p><em>}</em></p>
<p><em>if(readyXML == 1){</em></p>
<p><em>data=&#8221;Opening &#8230;&#8221;;</em></p>
<p><em>}</em></p>
<p><em>[/snippet]</em></p>
<p><em>What I have observed is that when running the setInterval commands as above, all except the final request are &#8220;jammed&#8221; on readyState = 1 and appropriately responds with &#8220;Sending â€¦&#8221;</em></p>
<p><em>however,</em></p>
<p><em>when I do the following (which is messy)</em></p>
<p><em>[snippet]</em></p>
<p><em>function startup(){</em></p>
<p><em>setInterval(&#8220;pinger(&#8216;live&#8217;,0)&#8221;,1000);<br />
alert(&#8220;something&#8221;);<br />
setInterval(&#8220;pinger(&#8216;standby&#8217;,1)&#8221;,10000);<br />
alert(&#8220;something&#8221;);<br />
setInterval(&#8220;pinger(&#8216;dev&#8217;,2)&#8221;,10000);<br />
alert(&#8220;something&#8221;);<br />
setInterval(&#8220;pinger(&#8216;test1&#8242;,3)&#8221;,10000);<br />
alert(&#8220;something&#8221;);<br />
setInterval(&#8220;pinger(&#8216;test2&#8242;,4)&#8221;,10000);<br />
alert(&#8220;something&#8221;);<br />
setInterval(&#8220;pinger(&#8216;test3&#8242;,5)&#8221;,10000);<br />
alert(&#8220;something&#8221;);<br />
setInterval(&#8220;pinger(&#8216;test4&#8242;,6)&#8221;,10000);</em></p>
<p><em>}</em></p>
<p><em>[/snippet]</em></p>
<p><em>Each response is absolutely spot on and will continue to generate pings correctly.  So in this case the script alert interrupts are forcing the callback request to trigger, whereas without the alerts its only the last request that triggers a callback.  I have also tried using artificial timeout loops instead of alerts (which generate the odd browser &#8220;script running slowly&#8221; message) but to no avail.</em></p>
<p><em>At the moment I am almost resigned to having to create individual events (like rollovers, though I would much prefer the ping initialisation to occur window.onLoad()) that trigger the setInterval() â€¦ which also seems to work fine.</em></p>
<p><em>I am hoping that you may have come across a way of forcing the XMLHttpRequest to respond without forcing alerts or confirms on the user, in the time since the last update to the article.</em></p>
]]></content:encoded>
			<wfw:commentRss>http://www.javascriptkata.com/2007/12/06/ask-dan-more-on-javascript-threading/feed/</wfw:commentRss>
		<slash:comments>7</slash:comments>
		</item>
		<item>
		<title>Barcamp Montreal 3 and this site</title>
		<link>http://www.javascriptkata.com/2007/11/05/barcamp-montreal-3-and-this-site/</link>
		<comments>http://www.javascriptkata.com/2007/11/05/barcamp-montreal-3-and-this-site/#comments</comments>
		<pubDate>Mon, 05 Nov 2007 23:52:55 +0000</pubDate>
		<dc:creator>Dan</dc:creator>
				<category><![CDATA[black belt]]></category>

		<guid isPermaLink="false">http://javascriptkata.timmyontime.com/?p=54</guid>
		<description><![CDATA[Like most of you already knew, I&#8217;m working with Frank Lamontagne of Ruby Fleebie on a project called TimmyOnTime. We had the chance to present it at the last Barcamp Montreal and it went&#8230; really bad! If you want to know more about the whole adventure (and have a good laugh), click here to read [...]]]></description>
			<content:encoded><![CDATA[<p>Like most of you already knew, I&#8217;m working with Frank Lamontagne of <a href="http://www.rubyfleebie.com/" target="_blank">Ruby Fleebie</a> on a project called <a href="http://timmyontime.com/" target="_blank">TimmyOnTime</a>. We had the chance to present it at the last <a href="http://barcampmontreal.org/wiki/English:BarCampMontreal3">Barcamp Montreal</a> and it went&#8230; really bad! If you want to know more about the whole adventure (and have a good laugh), <a href="http://www.rubyfleebie.com/barcampmontreal3-and-acts-of-god/" target="_blank">click here</a> to read the story written by Frank. (By the way, I used a good ol&#8217; click here link just for the fun of it&#8230; I never did it before and frankly, I like it&#8230; almost&#8230;).</p>
<h3>What happens with Javascript Kata?</h3>
<p>I know, I  have been negligent with site. It&#8217;s really shameful to let a great site die and there&#8217;s no one to blame but me. Why I did that? I could give hundreds of reasons but they would all be lies. I really don&#8217;t know what happened.</p>
<p>I want to give another twist to JavascriptKata. I want to get back to a more tradional reference site because I don&#8217;t like the concept of a technical blog after all. I want the articles to be more persistent, not just a post that everyone forget about after a while.</p>
<p>But, there would be a RSS feed too. I would announce new articles on the site. I would also use it to debate on tricky things about javascript and then make an article out of it.</p>
<p>I really would like to hear you about this idea, please comment!</p>
]]></content:encoded>
			<wfw:commentRss>http://www.javascriptkata.com/2007/11/05/barcamp-montreal-3-and-this-site/feed/</wfw:commentRss>
		<slash:comments>6</slash:comments>
		</item>
		<item>
		<title>Ajax, javascript and threads : the final truth</title>
		<link>http://www.javascriptkata.com/2007/06/12/ajax-javascript-and-threads-the-final-truth/</link>
		<comments>http://www.javascriptkata.com/2007/06/12/ajax-javascript-and-threads-the-final-truth/#comments</comments>
		<pubDate>Tue, 12 Jun 2007 14:44:38 +0000</pubDate>
		<dc:creator>Dan</dc:creator>
				<category><![CDATA[ajax]]></category>
		<category><![CDATA[black belt]]></category>

		<guid isPermaLink="false">http://javascriptkata.timmyontime.com/?p=47</guid>
		<description><![CDATA[Since I have written Ajax and javascript donâ€™t use thread, one of my reader (BK) told me that I was wrong. Here&#8217;s the final truth. What I said If javascript runs some code that takes 5 seconds to execute and an ajax response arrives at 2 seconds, it will take 3 seconds before it will [...]]]></description>
			<content:encoded><![CDATA[<p>Since I have written <a href="http://www.javascriptkata.com/2007/06/04/ajax-and-javascript-dont-use-threads/">Ajax and javascript donâ€™t use thread</a>, one of my reader (BK) told me that I was wrong. Here&#8217;s the final truth.</p>
<h3>What I said</h3>
<blockquote><p>If javascript runs some code that takes 5 seconds to execute and an ajax response arrives at 2 seconds, it will take 3 seconds before it will be executed (before the callback function is called).</p></blockquote>
<p>Example. We have a f() function that is incredibly long to execute (5 seconds) because it has a lot of code.</p>
<p>1. function f() is started<br />
2. f() makes an ajax call<br />
3. f() is still executing since 2 seconds<br />
4. the response from the server arrives at the third second<br />
5. f() is still in execution for 3 seconds<br />
6. the callback function for ajax is called</p>
<h3>What he said</h3>
<blockquote><p> Briefly, say I have CallPage1, CallPage2 and DoSomething. Both CallPage are async.</p>
<p>CallPage1 called<br />
CallPage2 called<br />
DoSomething called<br />
CallPage1 Returned (DoSomething is now paused)<br />
DoSomething ended<br />
CallPage2 Returned</p></blockquote>
<p>The DoSomething() function is paused when the ajax response arrives. The callback for the ajax is called and only then, DoSomething() resumes its execution.</p>
<p>That didn&#8217;t make sense to me. Javascript is really sequential and it would not do that kind of complex stuff. But, I had to be sure.</p>
<h3>The test</h3>
<p>I made a simple page to test it all.</p>
<p>1. Loop from 0 to X (user defined). X should be a big number.<br />
2. The first loop, it makes an ajax request<br />
3. The loops will finish someday<br />
4. The callback for the ajax will be called someday</p>
<p>I didn&#8217;t know what to expect : will the callback function be called <strong>before</strong> or <strong>after</strong> the loop?</p>
<h3>The result</h3>
<p>I ran <a href="http://www.javascriptkata.com/wp-content/uploads/test.php">the script</a> a lot of times with a lot of numbers and it was always the same thing : <strong>the callback function waits for the loop to be over before it is executed</strong>. It means that the current function is not paused.</p>
<h3>Now it&#8217;s your turn</h3>
<p>I want you to take a look at <a href="http://www.javascriptkata.com/wp-content/uploads/test.php">the script</a> (<a href="http://www.javascriptkata.com/wp-content/uploads/test.txt" target="_blank">complete php code</a>) (I know it&#8217;s poorly written, it&#8217;s just a test) and tell me if I&#8217;m wrong or if I&#8217;m right.</p>
<p>Thanks to you all!</p>
]]></content:encoded>
			<wfw:commentRss>http://www.javascriptkata.com/2007/06/12/ajax-javascript-and-threads-the-final-truth/feed/</wfw:commentRss>
		<slash:comments>28</slash:comments>
		</item>
	</channel>
</rss>
