<?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>DaveOnCode &#187; oop</title>
	<atom:link href="http://www.daveoncode.com/tag/oop/feed/" rel="self" type="application/rss+xml" />
	<link>http://www.daveoncode.com</link>
	<description>Objective C, iOS and more programming stuff</description>
	<lastBuildDate>Mon, 19 Dec 2011 12:11:12 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.3.1</generator>
		<item>
		<title>Custom JavaScript classes and packages using goog.provide() and goog.require()</title>
		<link>http://www.daveoncode.com/2009/12/01/custom-javascript-classes-packages-using-goog-provide-and-goog-require/</link>
		<comments>http://www.daveoncode.com/2009/12/01/custom-javascript-classes-packages-using-goog-provide-and-goog-require/#comments</comments>
		<pubDate>Tue, 01 Dec 2009 15:38:39 +0000</pubDate>
		<dc:creator>Davide Zanotti</dc:creator>
				<category><![CDATA[google closure]]></category>
		<category><![CDATA[closure]]></category>
		<category><![CDATA[javascript]]></category>
		<category><![CDATA[oop]]></category>

		<guid isPermaLink="false">http://www.daveoncode.com/?p=538</guid>
		<description><![CDATA[I will repeat it until the dead, the real power of Goolge Closure is the mechanism and the tools behind it, not the classes and methods written for you, but the possibility to write your own better JavaScript code! Today I&#8217;m gonna show you how to create and use your custom JavaScript classes and to [...]<div class="addthis_toolbox addthis_default_style addthis_32x32_style" addthis:url='http://www.daveoncode.com/2009/12/01/custom-javascript-classes-packages-using-goog-provide-and-goog-require/' addthis:title='Custom JavaScript classes and packages using goog.provide() and goog.require() ' ><a class="addthis_button_preferred_1"></a><a class="addthis_button_preferred_2"></a><a class="addthis_button_preferred_3"></a><a class="addthis_button_preferred_4"></a><a class="addthis_button_compact"></a></div>]]></description>
			<content:encoded><![CDATA[<p>I will repeat it until the dead, the real power of Goolge Closure is the mechanism and the tools behind it, not the classes and methods written for you, but the possibility to write your own better JavaScript code! Today I&#8217;m gonna show you how to create and use your custom JavaScript classes and to take the benefits of <strong>goog.provide()</strong> and <strong>goog.require()</strong> methods.<br />
Ok, let&#8217;s suppose we are working on a funny and exciting project, like an online  JavaScript game called &#8220;Js Monsters Battle&#8221; and let&#8217;s suppose the domain will be www.jsmonstersbattle.com. We love OOP, we love write js code, we love to have a well organized and document code, so we will begin by creating our packages, following the standard which establishes that the root of a package must be the reversed domain name, so in our example we will create this folders structure &#8220;/com/jsmonstersbattle/&#8221; and inside we will put our classes (and subpackages).<br />
Since our game is a game about monsters, we will create the core package &#8220;monsters&#8221; in that directory and we will write the first base class called Monster. We will got the following: &#8220;/com/jsmonstersbattle/monsters/monster.js&#8221;, three folders and one js file. Yes, one JavaScript file for class, forget those huge files containing millions of functions, we are going to write better JavaScript code and thinking more like a Java developer. If you are concerning about file size and performances, don&#8217;t worry about it, the compiler will reduce and optimize the code for you (just before release the project). Ok, let&#8217;s take a look inside the Monster class (monster.js):</p>
<div class="codecolorer-container javascript mac-classic" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:435px;height:300px;"><table cellspacing="0" cellpadding="0"><tbody><tr><td style="padding:5px;text-align:center;color:#888888;background-color:#EEEEEE;border-right: 1px solid #9F9F9F;font: normal 12px/1.4em Monaco, Lucida Console, monospace;"><div>1<br />2<br />3<br />4<br />5<br />6<br />7<br />8<br />9<br />10<br />11<br />12<br />13<br />14<br />15<br />16<br />17<br />18<br />19<br />20<br />21<br />22<br />23<br />24<br />25<br />26<br /></div></td><td><div class="javascript codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap">goog.<span style="color: #660066;">provide</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">&quot;com.jsmonstersbattle.monsters.Monster&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
<br />
<span style="color: #006600; font-style: italic;">/**<br />
&nbsp;* A common Monster<br />
&nbsp;* @constructor<br />
&nbsp;* @param {String} name<br />
&nbsp;* @param {String} age<br />
&nbsp;* @param {Number} level<br />
&nbsp;*/</span><br />
com.<span style="color: #660066;">jsmonstersbattle</span>.<span style="color: #660066;">monsters</span>.<span style="color: #660066;">Monster</span> <span style="color: #339933;">=</span> <span style="color: #003366; font-weight: bold;">function</span><span style="color: #009900;">&#40;</span><span style="color: #000066;">name</span><span style="color: #339933;">,</span> age<span style="color: #339933;">,</span> level<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span><br />
<br />
&nbsp; &nbsp; <span style="color: #006600; font-style: italic;">/** @private */</span> <span style="color: #000066; font-weight: bold;">this</span>._name <span style="color: #339933;">=</span> <span style="color: #000066;">name</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp; <span style="color: #006600; font-style: italic;">/** @private */</span> <span style="color: #000066; font-weight: bold;">this</span>._age <span style="color: #339933;">=</span> age<span style="color: #339933;">;</span><br />
&nbsp; &nbsp; <span style="color: #006600; font-style: italic;">/** @private */</span> <span style="color: #000066; font-weight: bold;">this</span>._level <span style="color: #339933;">=</span> level<span style="color: #339933;">;</span><br />
<br />
<span style="color: #009900;">&#125;</span><br />
<br />
<span style="color: #006600; font-style: italic;">/**<br />
* Returns an amount of damage points<br />
* @return {Number}<br />
*/</span><br />
com.<span style="color: #660066;">jsmonstersbattle</span>.<span style="color: #660066;">monsters</span>.<span style="color: #660066;">Monster</span>.<span style="color: #660066;">prototype</span>.<span style="color: #660066;">attack</span> <span style="color: #339933;">=</span> <span style="color: #003366; font-weight: bold;">function</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span><br />
<br />
&nbsp; &nbsp; <span style="color: #000066; font-weight: bold;">return</span> Math.<span style="color: #660066;">round</span><span style="color: #009900;">&#40;</span><span style="color: #000066; font-weight: bold;">this</span>._level <span style="color: #339933;">+</span> <span style="color: #000066; font-weight: bold;">this</span>._age <span style="color: #339933;">/</span> <span style="color: #CC0000;">100</span><span style="color: #009900;">&#41;</span> <span style="color: #339933;">*</span> <span style="color: #CC0000;">2000</span><span style="color: #339933;">;</span><br />
<br />
<span style="color: #009900;">&#125;</span></div></td></tr></tbody></table></div>
<p>In the code above I defined a basic class called <strong>Monster</strong>, which is available under the package (namespace) <strong>com.jsmonstersbattle.monsters</strong>, declared using <strong>goog.provide()</strong>, then I created a constructor (by using the special jsDoc notation /** @constructor */) which receives three arguments and sets three private Monster&#8217;s variable (/** @private */), finally I defined a method <strong>attack</strong> which returns a number of damage points. Ok, by following the Monster&#8217;s example, we can now create as many classes and packages as we like, interfaces (using /** @interface */ and /** @impements */) and subcalsses (using the method <strong>goog.inherits()</strong>), let&#8217;s see briefly these implementation:</p>
<p><strong>Extending a class:</strong></p>
<div class="codecolorer-container javascript mac-classic" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:435px;"><table cellspacing="0" cellpadding="0"><tbody><tr><td style="padding:5px;text-align:center;color:#888888;background-color:#EEEEEE;border-right: 1px solid #9F9F9F;font: normal 12px/1.4em Monaco, Lucida Console, monospace;"><div>1<br />2<br />3<br />4<br />5<br />6<br />7<br />8<br />9<br />10<br />11<br />12<br />13<br />14<br />15<br /></div></td><td><div class="javascript codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap"><span style="color: #006600; font-style: italic;">/**<br />
* @constructor<br />
* @extends com.jsmonstersbattle.monsters.Monster<br />
*/</span><br />
com.<span style="color: #660066;">jsmonstersbattle</span>.<span style="color: #660066;">monsters</span>.<span style="color: #660066;">UglyMonster</span> <span style="color: #339933;">=</span> <span style="color: #003366; font-weight: bold;">function</span><span style="color: #009900;">&#40;</span><span style="color: #000066;">name</span><span style="color: #339933;">,</span> age<span style="color: #339933;">,</span> level<span style="color: #339933;">,</span> uglyness<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span><br />
<br />
&nbsp; &nbsp; <span style="color: #006600; font-style: italic;">// call to Super</span><br />
&nbsp; &nbsp; com.<span style="color: #660066;">jsmonstersbattle</span>.<span style="color: #660066;">monsters</span>.<span style="color: #660066;">Monster</span>.<span style="color: #660066;">call</span><span style="color: #009900;">&#40;</span><span style="color: #000066; font-weight: bold;">this</span><span style="color: #339933;">,</span> <span style="color: #000066;">name</span><span style="color: #339933;">,</span> age<span style="color: #339933;">,</span> level<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
<br />
&nbsp; &nbsp; <span style="color: #006600; font-style: italic;">/** @private */</span> <span style="color: #000066; font-weight: bold;">this</span>._uglyness <span style="color: #339933;">=</span> uglyness<span style="color: #339933;">;</span><br />
<br />
<span style="color: #009900;">&#125;</span><span style="color: #339933;">;</span><br />
<br />
<span style="color: #006600; font-style: italic;">// you can read the following as: &quot;UglyMonster extends Monster&quot;</span><br />
goog.<span style="color: #660066;">inherits</span><span style="color: #009900;">&#40;</span>com.<span style="color: #660066;">jsmonstersbattle</span>.<span style="color: #660066;">monsters</span>.<span style="color: #660066;">UglyMonster</span><span style="color: #339933;">,</span> com.<span style="color: #660066;">jsmonstersbattle</span>.<span style="color: #660066;">monsters</span>.<span style="color: #660066;">Monster</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span></div></td></tr></tbody></table></div>
<p><strong>Creating and implementing an interface:</strong></p>
<div class="codecolorer-container javascript mac-classic" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:435px;"><table cellspacing="0" cellpadding="0"><tbody><tr><td style="padding:5px;text-align:center;color:#888888;background-color:#EEEEEE;border-right: 1px solid #9F9F9F;font: normal 12px/1.4em Monaco, Lucida Console, monospace;"><div>1<br />2<br />3<br />4<br />5<br />6<br />7<br />8<br />9<br />10<br />11<br />12<br />13<br />14<br />15<br />16<br />17<br />18<br /></div></td><td><div class="javascript codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap"><span style="color: #006600; font-style: italic;">/**<br />
&nbsp;* Evil interface.<br />
&nbsp;* @interface<br />
&nbsp;*/</span><br />
<span style="color: #003366; font-weight: bold;">function</span> IEvil<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span><span style="color: #009900;">&#125;</span><span style="color: #339933;">;</span><br />
IEvil.<span style="color: #660066;">prototype</span>.<span style="color: #660066;">doEvil</span> <span style="color: #339933;">=</span> <span style="color: #003366; font-weight: bold;">function</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span><span style="color: #009900;">&#125;</span><span style="color: #339933;">;</span><br />
<br />
<span style="color: #006600; font-style: italic;">/**<br />
* @constructor<br />
* @implements {IEvil}<br />
*/</span><br />
com.<span style="color: #660066;">jsmonstersbattle</span>.<span style="color: #660066;">monsters</span>.<span style="color: #660066;">EvilMonster</span> <span style="color: #339933;">=</span> <span style="color: #003366; font-weight: bold;">function</span><span style="color: #009900;">&#40;</span><span style="color: #000066;">name</span><span style="color: #339933;">,</span> age<span style="color: #339933;">,</span> level<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span><br />
&nbsp; &nbsp; <span style="color: #006600; font-style: italic;">// some code here</span><br />
<span style="color: #009900;">&#125;</span><span style="color: #339933;">;</span><br />
<br />
com.<span style="color: #660066;">jsmonstersbattle</span>.<span style="color: #660066;">monsters</span>.<span style="color: #660066;">EvilMonster</span>.<span style="color: #660066;">prototype</span>.<span style="color: #660066;">doEvil</span> <span style="color: #339933;">=</span> <span style="color: #003366; font-weight: bold;">function</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span><br />
&nbsp; &nbsp; <span style="color: #006600; font-style: italic;">// IEvil implementation</span><br />
<span style="color: #009900;">&#125;</span></div></td></tr></tbody></table></div>
<p>Once we created all the necessary classes, we will put our &#8220;com&#8221; folder under &#8220;closure&#8221; folder. Then we will create a main js file which will includes all the code required to initialize and run the application, I called it &#8220;<strong>application.js</strong>&#8221; for convenience. This file will requires all the classes it needs by using <strong>goog.require()</strong>:</p>
<div class="codecolorer-container javascript mac-classic" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:435px;"><table cellspacing="0" cellpadding="0"><tbody><tr><td style="padding:5px;text-align:center;color:#888888;background-color:#EEEEEE;border-right: 1px solid #9F9F9F;font: normal 12px/1.4em Monaco, Lucida Console, monospace;"><div>1<br />2<br /></div></td><td><div class="javascript codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap">goog.<span style="color: #660066;">require</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">&quot;com.jsmonstersbattle.monsters.EvilMonster&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
<span style="color: #006600; font-style: italic;">// ...and so on!</span></div></td></tr></tbody></table></div>
<p>After the import we will be able to use the classes, by referring them with the full qualified class name (&#8220;com.jsmonstersbattle.monsters.EvilMonster&#8221;), however since this means to write long strings, we can create references as the following:</p>
<div class="codecolorer-container javascript mac-classic" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:435px;"><table cellspacing="0" cellpadding="0"><tbody><tr><td style="padding:5px;text-align:center;color:#888888;background-color:#EEEEEE;border-right: 1px solid #9F9F9F;font: normal 12px/1.4em Monaco, Lucida Console, monospace;"><div>1<br />2<br />3<br />4<br />5<br />6<br />7<br /></div></td><td><div class="javascript codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap">goog.<span style="color: #660066;">require</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">&quot;com.jsmonstersbattle.monsters.EvilMonster&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
<br />
<span style="color: #003366; font-weight: bold;">var</span> EvilMonster <span style="color: #339933;">=</span> com.<span style="color: #660066;">jsmonstersbattle</span>.<span style="color: #660066;">monsters</span>.<span style="color: #660066;">EvilMonster</span><span style="color: #339933;">;</span><br />
<br />
<span style="color: #006600; font-style: italic;">// then...</span><br />
<br />
<span style="color: #003366; font-weight: bold;">var</span> monster <span style="color: #339933;">=</span> <span style="color: #003366; font-weight: bold;">new</span> EvilMonster<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span></div></td></tr></tbody></table></div>
<p>or use the JavaScript <strong>with</strong> operator:</p>
<div class="codecolorer-container javascript mac-classic" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:435px;"><table cellspacing="0" cellpadding="0"><tbody><tr><td style="padding:5px;text-align:center;color:#888888;background-color:#EEEEEE;border-right: 1px solid #9F9F9F;font: normal 12px/1.4em Monaco, Lucida Console, monospace;"><div>1<br />2<br />3<br />4<br />5<br />6<br />7<br /></div></td><td><div class="javascript codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap">goog.<span style="color: #660066;">require</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">&quot;com.jsmonstersbattle.monsters.EvilMonster&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
<br />
<span style="color: #000066; font-weight: bold;">with</span> <span style="color: #009900;">&#40;</span>com.<span style="color: #660066;">jsmonstersbattle</span>.<span style="color: #660066;">monsters</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span><br />
<br />
&nbsp; &nbsp; <span style="color: #003366; font-weight: bold;">var</span> monster <span style="color: #339933;">=</span> <span style="color: #003366; font-weight: bold;">new</span> EvilMonster<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
<br />
<span style="color: #009900;">&#125;</span></div></td></tr></tbody></table></div>
<p>&#8230;and this is all today, I hope you enjoyed my post ;^)</p>
<p><strong>UPDATE:<br />
Don&#8217;t use the with operator nor take reference to fully qualified class name, otherwise you will get several errors if you use advanced compilation!</strong></p>
<div class="addthis_toolbox addthis_default_style addthis_32x32_style" addthis:url='http://www.daveoncode.com/2009/12/01/custom-javascript-classes-packages-using-goog-provide-and-goog-require/' addthis:title='Custom JavaScript classes and packages using goog.provide() and goog.require() ' ><a class="addthis_button_preferred_1"></a><a class="addthis_button_preferred_2"></a><a class="addthis_button_preferred_3"></a><a class="addthis_button_preferred_4"></a><a class="addthis_button_compact"></a></div>]]></content:encoded>
			<wfw:commentRss>http://www.daveoncode.com/2009/12/01/custom-javascript-classes-packages-using-goog-provide-and-goog-require/feed/</wfw:commentRss>
		<slash:comments>2</slash:comments>
		</item>
	</channel>
</rss>

