<?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; prototype</title>
	<atom:link href="http://www.javascriptkata.com/category/prototype/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 inherit classes in javascript</title>
		<link>http://www.javascriptkata.com/2007/05/22/how-to-inherit-classes-in-javascript/?utm_source=rss&#038;utm_medium=rss&#038;utm_campaign=how-to-inherit-classes-in-javascript</link>
		<comments>http://www.javascriptkata.com/2007/05/22/how-to-inherit-classes-in-javascript/#comments</comments>
		<pubDate>Tue, 22 May 2007 15:36:23 +0000</pubDate>
		<dc:creator>dsimard</dc:creator>
				<category><![CDATA[object-oriented]]></category>
		<category><![CDATA[prototype]]></category>
		<category><![CDATA[white belt]]></category>

		<guid isPermaLink="false">http://javascriptkata.timmyontime.com/?p=41</guid>
		<description><![CDATA[First of all, since I made the front page of Ajaxian, a lot of new readers joined in. Welcome to all of you. I would also take a moment to explain that Javascript Kata is a technical blog about javascript and though I will talk here and there about ajax, it is not focused on [...]]]></description>
			<content:encoded><![CDATA[<p>First of all, since I made the front page of <a href="http://ajaxian.com/" title="Ajaxian" target="_blank">Ajaxian</a>, a lot of new readers joined in. Welcome to all of you. I would also take a moment to explain that Javascript Kata is a technical blog about javascript and though I will talk here and there about ajax, it is not focused on that subject. It is more about the good javascript techniques that should come along with all the ajax you have to do. I update this site 2 or 3 times a week. Keep reading!</p>
<p>There are hundreds of ways to do inheritance in javascript but a single one is <span style="font-weight: bold">simpler, cleanier and prettier</span> than all the other ones.</p>
<p><span style="font-style: italic">Note!<br />
Before reading this article, you should take a look at </span><a href="http://www.javascriptkata.com/2007/03/23/how-to-create-objects-in-object-oriented-javascript/" title="How to create objects in object-oriented javascript">How to create objects in object-oriented javascript</a><span style="font-style: italic">.<br />
</span></p>
<h3>A one-liner</h3>
<p>To inherit a class in javascript, it&#8217;s a one-liner</p>
<div class="dean_ch" style="white-space: wrap;">
<ol>
<li class="li1">
<div class="de1">TheSubClass.<span class="me1">prototype</span> = <span class="kw2">new</span> TheParentClass<span class="br0">&#40;</span><span class="br0">&#41;</span>;</div>
</li>
</ol>
</div>
<p>As simple as that!</p>
<h3>Where to write the one-liner</h3>
<p>The problem with that one-liner is <span style="font-weight: bold">where should it goes?</span> Once again, the answer is simple : <span style="font-weight: bold">after the constructor of the sub-class</span>. It may look strange but it is extremely effective.</p>
<div class="dean_ch" style="white-space: wrap;">
<ol>
<li class="li1">
<div class="de1"><span class="coMULTI">/* The constructor of the Mammal class */</span></div>
</li>
<li class="li1">
<div class="de1"><span class="kw2">function</span> Mammal<span class="br0">&#40;</span><span class="br0">&#41;</span> <span class="br0">&#123;</span></div>
</li>
<li class="li1">
<div class="de1"><span class="br0">&#125;</span></div>
</li>
<li class="li1">
<div class="de1">&nbsp;</div>
</li>
<li class="li2">
<div class="de2"><span class="coMULTI">/* The constructor of the Cat class */</span></div>
</li>
<li class="li1">
<div class="de1"><span class="kw2">function</span> Cat<span class="br0">&#40;</span><span class="br0">&#41;</span> <span class="br0">&#123;</span></div>
</li>
<li class="li1">
<div class="de1"><span class="br0">&#125;</span></div>
</li>
<li class="li1">
<div class="de1"><span class="co1">// The magic that inherits Cat from Mammal is here!!!!!</span></div>
</li>
<li class="li1">
<div class="de1">Cat.<span class="me1">prototype</span> = <span class="kw2">new</span> Mammal<span class="br0">&#40;</span><span class="br0">&#41;</span>;</div>
</li>
</ol>
</div>
<h3>Is this true inheritance?</h3>
<p>In the hundreds of other ways of inheriting classes in javascript, I think that this is the only one that is a true inheritance. What do I mean by true inheritance? I mean that javascript recognizes it as a sub-class of the class. Check this out!</p>
<div class="dean_ch" style="white-space: wrap;">
<ol>
<li class="li1">
<div class="de1"><span class="coMULTI">/* Above code goes here */</span></div>
</li>
<li class="li1">
<div class="de1">&nbsp;</div>
</li>
<li class="li1">
<div class="de1"><span class="co1">// Create a cat</span></div>
</li>
<li class="li1">
<div class="de1"><span class="kw2">var</span> theCat = <span class="kw2">new</span> Cat<span class="br0">&#40;</span><span class="br0">&#41;</span>;</div>
</li>
<li class="li2">
<div class="de2">&nbsp;</div>
</li>
<li class="li1">
<div class="de1"><span class="co1">// Check if the cat is an instance of the Cat class</span></div>
</li>
<li class="li1">
<div class="de1"><span class="kw1">if</span> <span class="br0">&#40;</span>theCat <span class="kw1">instanceof</span> Cat<span class="br0">&#41;</span> <span class="br0">&#123;</span></div>
</li>
<li class="li1">
<div class="de1">&nbsp; &nbsp; <span class="kw3">alert</span><span class="br0">&#40;</span><span class="st0">&quot;theCat is an instance of the Cat class&quot;</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>
<li class="li1">
<div class="de1"><span class="co1">// Check if the cat is an instance of the Mammal class</span></div>
</li>
<li class="li1">
<div class="de1"><span class="kw1">if</span> <span class="br0">&#40;</span>theCat <span class="kw1">instanceof</span> Mammal<span class="br0">&#41;</span> <span class="br0">&#123;</span></div>
</li>
<li class="li1">
<div class="de1">&nbsp; &nbsp; <span class="kw3">alert</span><span class="br0">&#40;</span><span class="st0">&quot;theCat is an instance of the Mammal class&quot;</span><span class="br0">&#41;</span>;</div>
</li>
<li class="li1">
<div class="de1"><span class="br0">&#125;</span></div>
</li>
</ol>
</div>
<p>If you execute this code, you&#8217;ll see that the cat is an instance of the <span style="font-style: italic">Cat</span> class and the <span style="font-style: italic">Mammal</span> class.</p>
]]></content:encoded>
			<wfw:commentRss>http://www.javascriptkata.com/2007/05/22/how-to-inherit-classes-in-javascript/feed/</wfw:commentRss>
		<slash:comments>37</slash:comments>
		</item>
		<item>
		<title>What are javascript prototypes? (longer answer)</title>
		<link>http://www.javascriptkata.com/2007/03/25/what-are-javascript-prototypes-longer-answer/?utm_source=rss&#038;utm_medium=rss&#038;utm_campaign=what-are-javascript-prototypes-longer-answer</link>
		<comments>http://www.javascriptkata.com/2007/03/25/what-are-javascript-prototypes-longer-answer/#comments</comments>
		<pubDate>Sun, 25 Mar 2007 20:38:34 +0000</pubDate>
		<dc:creator>dsimard</dc:creator>
				<category><![CDATA[prototype]]></category>
		<category><![CDATA[white belt]]></category>

		<guid isPermaLink="false">http://javascriptkata.timmyontime.com/?p=12</guid>
		<description><![CDATA[[I've also written the short answer (for advanced javascripters)] Javascript is using prototypes and is the only language I know that is doing it. What is the idea behind it? Simple. With prototypes, you can extend (add methods/properties) any class you want anywhere you want anytime you want even if you are not the owner [...]]]></description>
			<content:encoded><![CDATA[<p><em>[I've also written the <a href="http://www.javascriptkata.com/2007/03/21/what-are-javascript-prototypes-short-answer-for-advanced-javascripters/">short answer</a> (for advanced javascripters)]  </em></p>
<p>Javascript is using prototypes and is the only language I know that is doing it. What is the idea behind it? Simple. With prototypes, you can extend (add methods/properties) any class you want anywhere you want anytime you want even if you are not the owner of that object. Object-oriented purist will be shocked but I am more than pleased with that.</p>
<h3>Why using prototypes?</h3>
<p><span style="font-weight: bold">It&#8217;s memory-friendly</span><br />
By adding a method to a class prototype, you are creating a single occurence of the function that is referenced by every objects of that type.<span style="font-weight: bold"></span><br />
<span style="font-weight: bold"><br />
It&#8217;s easy</span><br />
To add a method to a class, no need to create a new class. Juste write <span style="font-style: italic">TheClass.prototype.theMethod = function() {//code here}</span> and it&#8217;s done.</p>
<p><span style="font-weight: bold">It&#8217;s fun!<br />
<span style="font-weight: bold"></span></span>Maybe not as fun as drinking kool-aid but compared to the complexivity of other languages, we have a champ.</p>
<h3>How to use prototypes</h3>
<p><span style="font-weight: bold"></span>Simple. Write the [NameOfTheClass].prototype.[NameOfTheExtension].</p>
<p>You want to add a <a href="http://www.javascriptkata.com/2007/03/21/how-to-extend-javascript-classes/" title="trim function to the String">trim function to the String</a> object? (thanks <a href="http://www.serberus.net/" target="_blank">Greg</a> for the correction)</p>
<p>[source:javascript]<br />
String.prototype.trim = function()<br />
{<br />
return this.replace(/^\s*|\s*$/g, &#8220;&#8221;);<br />
}<br />
[/source]</p>
<p>You want to add an <span style="font-style: italic">oldValue</span> property to the string</p>
<p>String.prototype.oldValue = &#8220;the old value&#8221;;</p>
<p><span style="color: #ff0000; font-weight: bold">Beware!</span> All the new String objects and the one already declared will have a property with the value &#8220;the old value&#8221; inside. I can&#8217;t think of a particular reason why one would do this. If you have one, <span style="font-weight: bold">please comment</span>.</p>
]]></content:encoded>
			<wfw:commentRss>http://www.javascriptkata.com/2007/03/25/what-are-javascript-prototypes-longer-answer/feed/</wfw:commentRss>
		<slash:comments>7</slash:comments>
		</item>
		<item>
		<title>How to create objects in object-oriented javascript</title>
		<link>http://www.javascriptkata.com/2007/03/23/how-to-create-objects-in-object-oriented-javascript/?utm_source=rss&#038;utm_medium=rss&#038;utm_campaign=how-to-create-objects-in-object-oriented-javascript</link>
		<comments>http://www.javascriptkata.com/2007/03/23/how-to-create-objects-in-object-oriented-javascript/#comments</comments>
		<pubDate>Fri, 23 Mar 2007 12:25:30 +0000</pubDate>
		<dc:creator>dsimard</dc:creator>
				<category><![CDATA[object-oriented]]></category>
		<category><![CDATA[prototype]]></category>
		<category><![CDATA[white belt]]></category>

		<guid isPermaLink="false">http://javascriptkata.timmyontime.com/?p=11</guid>
		<description><![CDATA[[UPDATE : This post is outdated. Check out the new post on how to create objects.] Javascript is a functional programming language thus having no &#8220;real&#8221; objects. You can write it the way you want : procedural spag, functional, object or whatever word you know. Why should I write it object-oriented? Because, OO has proven [...]]]></description>
			<content:encoded><![CDATA[<p><strong style="color:red;">[UPDATE : This post is outdated. Check out the new post on <a href="http://www.javascriptkata.com/2009/09/23/how-to-create-an-object-with-private-variables-and-methods/">how to create objects</a>.]</strong></p>
<p>Javascript is a functional programming language thus having no &#8220;real&#8221; objects. You can write it the way you want : procedural spag, functional, object or whatever word you know. <span style="font-weight: bold">Why should I write it object-oriented?</span> Because, OO has proven many times its ease of use and great encapsulation. In this paper, I won&#8217;t talk about <a href="http://www.javascriptkata.com/2007/03/19/5-reasons-to-write-object-oriented-oo-javascript/"><span style="font-weight: bold">why</span></a> but about <span style="font-weight: bold">how</span>.</p>
<p>There are hundreds of way of writing OO javascript, I tried a lot of the most commons and I finally adopted one : oo using prototypes.</p>
<h3>1. Creating an empty class</h3>
<p>I like cats so here&#8217;s a complete example for a cat that meows.</p>
<p>First, I create an empty class.</p>
<p>[source:javascript]<br />
function Cat() {<br />
}<br />
[/source]</p>
<p>Hmmmm&#8230; that looks a lot like a function. In fact, it&#8217;s a function. <span style="font-weight: bold">Why classes are function? </span>Because javascript is a functional language. More on that later (maybe).</p>
<h3>2. Creating the constructor</h3>
<p>When I have a cat that meows, I want to see its name. I add a name property that is initialized in the constructor of the class.</p>
<p>[source:javascript]<br />
function Cat(name) {<br />
	this.name = name;<br />
}<br />
[/source]</p>
<p>What is <span style="font-weight: bold">this</span>? This, is a reference on the current instance of the object. By calling <span style="font-style: italic">this.name = name</span>, we instantiate an public variable for the object that has the value <span style="font-style: italic">name</span> (the name of the cat).</p>
<h3>3. Adding a instance method</h3>
<p>As I said earlier, I want the cat to meow. So, I will add a <span style="font-style: italic">meow() </span>method to the class that will be available to every instance.</p>
<p>[source:javascript]<br />
function Cat(name) {<br />
	this.name = name;<br />
}</p>
<p>Cat.prototype.meow = function() {<br />
	alert(&#8220;meow!&#8221;);<br />
}<br />
[/source]</p>
<p>I used the class&#8217; <span style="font-style: italic">prototype</span>. This is one of the hundred ways of adding a method but it&#8217;s the best way because we create a <span style="font-weight: bold">single reference for all the objects of that class</span>.</p>
<h3>4. Using variables of an object</h3>
<p>Hey, didn&#8217;t you read the specs? I said that I wanted to see the name of the meowing cat.</p>
<p>[source:javascript]<br />
Cat.prototype.meow = function() {<br />
	alert(this.name + &#8221; : meow!&#8221;);<br />
}<br />
[/source]</p>
<p>Did you see? I used the <span style="font-style: italic">this</span> (reference on the current instance of the object). By doing that, I simply call the variable that I defined in my constructor (see point #2).</p>
<h3>5. Making the cat meow</h3>
<p>Now I&#8217;m ready to make the cat meow. On the <span style="font-style: italic">onload</span> property of the <span style="font-style: italic">body</span> element, I call a function named <span style="font-style: italic">bodyOnload</span>.</p>
<p>&lt;html&gt;<br />
&lt;body onload=&#8217;bodyOnLoad();&#8217; /&gt;<br />
&lt;/html&gt;</p>
<p>In the bodyOnLoad function, I create a Cat object and call the <span style="font-style: italic">meow()</span> method.</p>
<p>[source:javascript]<br />
function bodyOnLoad() {<br />
	var mistigri = new Cat(&#8216;Mistigri&#8217;);<br />
	mistigri.meow();<br />
}<br />
[/source]</p>
<p>I should see an message box with the message <span style="font-weight: bold">&#8220;Mistigri : meow!&#8221;</span> inside.</p>
<h3>In conclusion</h3>
<p>Ok, that was an easy one but though it may seem obvious to you, it is not alway as clear for some of my fellows. The important thing I wanted to show was how to create a class method (see point #3). This is the best/safest/fastest way of doing this.</p>
]]></content:encoded>
			<wfw:commentRss>http://www.javascriptkata.com/2007/03/23/how-to-create-objects-in-object-oriented-javascript/feed/</wfw:commentRss>
		<slash:comments>15</slash:comments>
		</item>
		<item>
		<title>How to extend javascript classes</title>
		<link>http://www.javascriptkata.com/2007/03/21/how-to-extend-javascript-classes/?utm_source=rss&#038;utm_medium=rss&#038;utm_campaign=how-to-extend-javascript-classes</link>
		<comments>http://www.javascriptkata.com/2007/03/21/how-to-extend-javascript-classes/#comments</comments>
		<pubDate>Wed, 21 Mar 2007 22:07:04 +0000</pubDate>
		<dc:creator>dsimard</dc:creator>
				<category><![CDATA[black belt]]></category>
		<category><![CDATA[prototype]]></category>

		<guid isPermaLink="false">http://javascriptkata.timmyontime.com/?p=8</guid>
		<description><![CDATA[The intrinsec objects of javascript (String, Number, Date, etc) are missing a lot of handy methods. God knows why. Example, you don&#8217;t have a trim() function on a String object. Maybe the developers thought that it was easy enough to write theString.replace(/^\s*&#124;\s*$/g, &#8220;&#8221;) to trim a string but that&#8217;s not the kind of ugly code [...]]]></description>
			<content:encoded><![CDATA[<p>The intrinsec objects of javascript (String, Number, Date, etc) are missing a lot of handy methods. God knows why. Example, you don&#8217;t have a <span style="font-style: italic">trim()</span> function on a <span style="font-style: italic">String</span> object. Maybe the developers thought that it was easy enough to write <em><span style="font-style: italic">theString.replace(</span>/^\s*|\s*$/g<span style="font-style: italic">, &#8220;&#8221;)</span></em> to trim a string but that&#8217;s not the kind of ugly code I want to see everywhere in my projects. It&#8217;s unesthetical. To do this, I have to use <span style="font-style: italic">prototype</span>.</p>
<p>So I want to add a <span style="font-style: italic">trim()</span> method to all my objects that are <span style="font-style: italic">String</span> class.</p>
<p>[source:javascript]</p>
<p>String.prototype.trim = function()<br />
{<br />
return this.replace(/^\s*|\s*$/g, &#8220;&#8221;);<br />
}</p>
<p>[/source]</p>
<p>Easy.</p>
<p><span style="font-style: italic">Special note : I don&#8217;t want you to copy/paste that code. It&#8217;s for the purpose of the demonstration only.</span></p>
<p>I can do the same thing for every objects wheter they are intrinsic javascript classes, third-party classes or my own classes.</p>
]]></content:encoded>
			<wfw:commentRss>http://www.javascriptkata.com/2007/03/21/how-to-extend-javascript-classes/feed/</wfw:commentRss>
		<slash:comments>1</slash:comments>
		</item>
		<item>
		<title>What are javascript prototypes? (short answer for advanced javascripters)</title>
		<link>http://www.javascriptkata.com/2007/03/21/what-are-javascript-prototypes-short-answer-for-advanced-javascripters/?utm_source=rss&#038;utm_medium=rss&#038;utm_campaign=what-are-javascript-prototypes-short-answer-for-advanced-javascripters</link>
		<comments>http://www.javascriptkata.com/2007/03/21/what-are-javascript-prototypes-short-answer-for-advanced-javascripters/#comments</comments>
		<pubDate>Wed, 21 Mar 2007 22:03:22 +0000</pubDate>
		<dc:creator>dsimard</dc:creator>
				<category><![CDATA[black belt]]></category>
		<category><![CDATA[prototype]]></category>

		<guid isPermaLink="false">http://javascriptkata.timmyontime.com/?p=7</guid>
		<description><![CDATA[[I've also written a longer answer for beginners]Â  Prototypes can extend any class you want by adding a property or a method. By calling, [source:javascript] String.prototype.alertMe = function() { alert(this); } [/source] you are adding the method alertMe() to every String object of your application. It uses less memory because javascript creates only one instance [...]]]></description>
			<content:encoded><![CDATA[<p><em>[I've also written a <a href="http://www.javascriptkata.com/2007/03/25/what-are-javascript-prototypes-longer-answer/">longer answer</a> for beginners]Â </em></p>
<p>Prototypes can extend any class you want by adding a property or a method. By calling,</p>
<p>[source:javascript]<br />
String.prototype.alertMe = function() {<br />
alert(this);<br />
}<br />
[/source]</p>
<p>you are adding the method <span style="font-style: italic">alertMe()</span> to every String object of your application.</p>
<p>It uses less memory because javascript creates only one instance of the function and uses references to it.</p>
<p>Short answer done!</p>
]]></content:encoded>
			<wfw:commentRss>http://www.javascriptkata.com/2007/03/21/what-are-javascript-prototypes-short-answer-for-advanced-javascripters/feed/</wfw:commentRss>
		<slash:comments>3</slash:comments>
		</item>
	</channel>
</rss>

