<?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>engfer(s) &#187; Javascript</title>
	<atom:link href="http://www.engfers.com/category/javascript/feed/" rel="self" type="application/rss+xml" />
	<link>http://www.engfers.com</link>
	<description>» scrumptions blog &#038; code » nothing more, nothing less</description>
	<lastBuildDate>Tue, 06 Oct 2009 21:52:32 +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>Google Chrome CSS Selector Speeds Compared</title>
		<link>http://www.engfers.com/2008/09/15/google-chrome-css-selector-speeds-compared/</link>
		<comments>http://www.engfers.com/2008/09/15/google-chrome-css-selector-speeds-compared/#comments</comments>
		<pubDate>Mon, 15 Sep 2008 15:45:48 +0000</pubDate>
		<dc:creator>engfer</dc:creator>
				<category><![CDATA[CSS]]></category>
		<category><![CDATA[Javascript]]></category>
		<category><![CDATA[chrome]]></category>

		<guid isPermaLink="false">http://www.engfers.com/?p=386</guid>
		<description><![CDATA[Google somewhat surprised us all and released it&#8217;s web browser on Sept. 2, 2008; most users of the shiny new browser have been extremely impressed with the initial speed and features. I have to say that even I am impressed by this new browser (as long as I&#8217;m not behind a corporate firewall).  Since the [...]]]></description>
			<content:encoded><![CDATA[<p><div style="float: left; padding: 0px 5px 5px 0px;"><script type="text/javascript"><!--
google_ad_client = "pub-9461422058527053";
google_ad_slot = "3901277195";
google_ad_width = 300;
google_ad_height = 250;
//--></script>
<script type="text/javascript" src="http://pagead2.googlesyndication.com/pagead/show_ads.js"></script>
</div> Google somewhat surprised us all and released it&#8217;s<a href="http://googleblog.blogspot.com/2008/09/google-chrome-now-live.html"> web browser on Sept. 2, 2008</a>; most users of the shiny new browser have been <em>extremely </em>impressed with the initial speed and features. I have to say that even I am impressed by this new browser (as long as I&#8217;m <strong><a href="http://www.engfers.com/2008/09/02/google-chrome-behind-a-corporate-proxy-a-long-way-to-go/">not behind a corporate firewall</a></strong>).  Since the <a href="http://news.cnet.com/8301-1001_3-10030888-92.html">CNET article</a> came out about the extreme speed of Google Chrome, I immediately wondered how it would perform against other browsers in the <a href="http://mootools.net">mootools.net</a> tool <a href="http://mootools.net/slickspeed/"><strong>Slickspeed</strong></a> CSS selectors test, so I decided to do some benchmarking of my own.</p>
<p><span id="more-386"></span></p>
<h3 class="hrule"><span style="color: #3366ff;">The Setup</span></h3>
<p>To do some benchmarking I downloaded the &#8220;mootools-developed&#8221; <a href="http://mootools.net/slickspeed/">Slickspeed</a> PHP framework from it&#8217;s <a href="http://code.google.com/p/slickspeed/"><strong>Google Code hosted location</strong></a>, along with <a href="http://www.apachefriends.org/en/xampp.html"><strong>XAMPP</strong></a> to serve up the PHP pages required for the tests.</p>
<h4><span style="color: #ff0000;">Hardware</span></h4>
<ul>
<li>2007 Compac nc8430</li>
<li>Intel Dual-Core T2500 @ 2.00GHz</li>
<li>1.00 GB RAM</li>
</ul>
<h4><span style="color: #ff0000;">Software</span></h4>
<ul>
<li>MS Windows XP Professional SP 2</li>
<li><a href="http://www.apachefriends.org/en/xampp-windows.html#641">XAMPP 1.6.7</a></li>
<li>Slickspeed (svn trunk version)</li>
</ul>
<h4><span style="color: #ff0000;">Browsers</span></h4>
<ul>
<li>Safari 3.1.1</li>
<li>Opera 9.5.2</li>
<li>Firefox 3.0.1</li>
<li>Internet Explorer 7.0.5730</li>
<li>Chrome 0.2.149.27</li>
</ul>
<h4><span style="color: #ff0000;">JavaScript Libraries</span></h4>
<ul>
<li>Mootools 1.2</li>
<li>jQuery 1.2.6</li>
<li>Prototype 1.6.0.2</li>
</ul>
<p>I only downloaded 3 JS libraries because I thought that was enough to tell the difference between the browsers; this isn&#8217;t meant as an exercise at &#8220;who has the fastest JS library&#8221;.</p>
<p>To generate the results I installed XAMPP, installed slickspeed and set it up with the 3 JS libraries; then I opened each browser one at a time and ran the local copy of the slickspeed test:</p>
<p style="padding-left: 30px;">This test runs <strong>40 different CSS selector tests</strong> with whatever JS libraries you have configured atainst <a href="http://www.w3.org/TR/2001/CR-css3-selectors-20011113/"><strong>this W3C document</strong></a>. Each test is run separately in <strong>separate IFRAME tags</strong> as <em>not to interfere</em> with one another and report false data.</p>
<h4><span style="color: #ff0000;">Data Collection</span></h4>
<p>Data was collected by running the above slickspeed setup <strong>10 times</strong> for <strong>each browser</strong>. After each time the test was ran, I recorded the <strong>final time for that browser</strong>.</p>
<p><span style="color: #339966;"><em>** I used the <strong>final time</strong> in the slickspeed test because I was testing for the <strong><span style="text-decoration: underline;">overall</span> effectiveness &amp; speed</strong> of the browsers. If you want to see how it performs on a per test basis, run it yourself!</em></span></p>
<h3 class="hrule"><span style="color: #3366ff;">The Results</span></h3>
<p><strong><span style="color: #3366ff;">Mootools 1.2 Results</span></strong></p>
<p style="text-align: left;">Mootools used to be my favorite JS library. It has some cool features that make it pretty nice (like a stab at code organization using the Class object).</p>
<p style="text-align: center;"><img class="aligncenter" src="http://chart.apis.google.com/chart?&amp;chs=420x690&amp;chtt=Mootools+1.2+Slickspeed+CSS+Selector+Test|Response+Time+Totals%20(in%20milliseconds)&amp;cht=bhg&amp;chco=5f96ff,ff914d,edfe68,55e851,b069c6&amp;chxt=x,y&amp;chxl=1:|Test+10|Test+9|Test+8|Test+7|Test+6|Test+5|Test+4|Test+3|Test+2|Test+1&amp;chbh=9,1,9&amp;chdl=Chrome+0.2.149.27|Safari+3.1.1|Opera+9.5.2|Firefox+3.0.1|IE+7.0.5730&amp;chdlp=t&amp;chd=t:92.00,91.50,106.50,97.25,102.50,98.25,89.25,105.50,92.50,102.75|47.00,58.50,30.75,43.25,39.00,54.50,42.75,42.75,58.75,63.00|66.75,65.75,93.50,136.50,137.25,165.75,171.50,168.25,175.75,159.25|150.25,137.75,149.50,146.25,147.75,153.75,144.50,146.25,147.50,151.50|476.25,481.25,523.25,504.00,498.50,440.75,414.00,449.50,475.75,444.75&amp;chds=0,600&amp;chxr=0,0,600" alt="" /></p>
<p style="text-align: left;">Safari operated the best with the Mootools library. Opera <strong>started out </strong>as a good candidate; however, the longer you kept open the browser and the more times you ran the test, the worse the response time got. Opera will show the same results for other libraries as well.</p>
<p style="text-align: left;">IE sucks, Firefox is good, Chrome is pretty darn good.</p>
<p><strong><span style="color: #3366ff;">JQuery 1.2.6 Results</span></strong></p>
<p style="text-align: center;"><img class="aligncenter" src="http://chart.apis.google.com/chart?&amp;chs=420x690&amp;chtt=JQuery+1.2.6+Slickspeed+CSS+Selector+Test|Response+Time+Totals%20(in%20milliseconds)&amp;cht=bhg&amp;chco=5f96ff,ff914d,edfe68,55e851,b069c6&amp;chxt=x,y&amp;chxl=1:|Test+10|Test+9|Test+8|Test+7|Test+6|Test+5|Test+4|Test+3|Test+2|Test+1&amp;chbh=9,1,9&amp;chdl=Chrome+0.2.149.27|Safari+3.1.1|Opera+9.5.2|Firefox+3.0.1|IE+7.0.5730&amp;chdlp=t&amp;chd=t:58.50,72.00,62.75,75.00,70.00,66.75,28.00,64.25,72.00,67.50|50.75,35.00,42.75,38.75,46.50,59.00,46.75,54.25,46.50,47.75|58.50,70.25,70.50,113.00,124.25,131.75,148.00,169.25,179.25,140.75|147.25,140.50,150.50,149.00,152.50,151.00,148.75,141.25,145.50,151.00|325.25,320.25,371.75,363.00,324.75,292.75,293.50,285.50,301.00,292.50&amp;chds=0,400&amp;chxr=0,0,400" alt="" /></p>
<p style="text-align: left;">What is up Opera? You leaking stuff? No JS garbage collection? I did &#8220;hard&#8221; refreshes so the caches would be expunged; I just don&#8217;t get it.</p>
<p style="text-align: left;">Chrome and Safari FTW. IE bad, bad, bad.</p>
<p style="text-align: left;"><span style="color: #3366ff;"><strong>Prototype 1.6.0.2 Results</strong></span></p>
<p style="text-align: left;">Prototype made IE 7 look like the biggest piece of junk in the world; I have since then see the same results in YUI.  The IE results are an <strong>order of magnitude</strong> <strong>greater </strong>than the speeds of any of the other browsers <strong>combined</strong>. Keep in mind that less is better.</p>
<p style="text-align: center;"><img src="http://chart.apis.google.com/chart?&amp;chs=420x690&amp;chtt=Prototype+1.6.0.2+Slickspeed+CSS+Selector+Test|Response+Time+Totals%20(in%20milliseconds)&amp;cht=bhg&amp;chco=5f96ff,ff914d,edfe68,55e851,b069c6&amp;chxt=x,y&amp;chxl=1:|Test+10|Test+9|Test+8|Test+7|Test+6|Test+5|Test+4|Test+3|Test+2|Test+1&amp;chbh=9,1,9&amp;chdl=Chrome+0.2.149.27|Safari+3.1.1|Opera+9.5.2|Firefox+3.0.1|IE+7.0.5730&amp;chdlp=t&amp;chd=t:89.25,93.00,85.50,103.25,88.00,90.25,98.75,93.00,95.25,91.50|183,164.25,164.25,168.25,172.25,168.00,176.00,191.25,172.00,168.00|70.00,89.50,101.00,161.00,141.25,159.50,184.00,152.00,156.75,172.25|229.75,234.50,235.75,235.25,237.00,232.00,233.25,234.00,229.25,234.75|1636.75,1644.25,1789.50,1838.75,1801.75,1644.75,1628.50,1622.50,1609.75,1617.25&amp;chds=0,1900&amp;chxr=0,0,1900" alt="" /></p>
<p style="text-align: left;">This was an extremely disappointing test; moreover, Opera still showed signs of memory leaks and slowly increasing final result times.</p>
<p style="text-align: left;">Chrome wins this battle; Safari a close second, and <strong>almost </strong>every other browser is not far behind. Yuck IE!</p>
<h3 class="hrule"><span style="color: #3366ff;">Final Results</span></h3>
<p>So now we decided to take the average of all 10 tests and put it onto one chart so you can see the important stuff all summed up.</p>
<p><img src="http://chart.apis.google.com/chart?&amp;chs=460x600&amp;chtt=Average+Slickspeed+CSS+Selector+Test+Response|Time+Totals+(in%20milliseconds)&amp;cht=bvg&amp;chco=5f96ff,ff914d,edfe68,55e851,b069c6&amp;chxt=y,x&amp;chxl=1:|JQuery+1.2.6|Mootools+1.2Prototype+1.6.0.2&amp;chbh=25,1,16&amp;chdl=Chrome+0.2.149.27|Safari+3.1.1|Opera+9.5.2|Firefox+3.0.1|IE+7.0.5730&amp;chdlp=t&amp;chd=t:63.68,97.80,172.73|46.80,48.03,92.78|120.55,144.03,138.73|147.73,147.50,233.55|317.03,470.80,1683.38&amp;chds=0,1700&amp;chxr=0,0,1700" alt="" /></p>
<p style="text-align: left;"><span style="color: #ff0000;"><strong>Final result conclusions&#8230;</strong></span></p>
<ul>
<li>Chrome did <strong>very well</strong> considering it&#8217;s a partial mesh of Safari and Firefox components.</li>
<li>Safari on a local copy of slickspeed did surprisingly <strong>very well</strong>.</li>
<li>Opera is <strong>okay </strong>if you view a web page and then restart the browser. Otherwise, JS applications become slower and slower.</li>
<li>IE needs <strong>a lot of work</strong> (<em>but who didn&#8217;t know that</em> <em><span style="text-decoration: underline;">already</span></em>)</li>
</ul>
<p><div align="center"><script type="text/javascript"><!--
google_ad_client = "pub-9461422058527053";
google_ad_slot = "5057152483";
google_ad_width = 468;
google_ad_height = 60;
//--></script>
<script type="text/javascript" src="http://pagead2.googlesyndication.com/pagead/show_ads.js"></script>
</div></p>
]]></content:encoded>
			<wfw:commentRss>http://www.engfers.com/2008/09/15/google-chrome-css-selector-speeds-compared/feed/</wfw:commentRss>
		<slash:comments>14</slash:comments>
		</item>
		<item>
		<title>The Main Reason For JavaScript Unreadability</title>
		<link>http://www.engfers.com/2008/09/12/the-main-reason-for-javascript-unreadability/</link>
		<comments>http://www.engfers.com/2008/09/12/the-main-reason-for-javascript-unreadability/#comments</comments>
		<pubDate>Fri, 12 Sep 2008 13:33:07 +0000</pubDate>
		<dc:creator>engfer</dc:creator>
				<category><![CDATA[Javascript]]></category>

		<guid isPermaLink="false">http://www.engfers.com/?p=317</guid>
		<description><![CDATA[One of the things that I have noticed about JavaScript is that there are many ways to solve the same problem (sometimes I even think of it like perl&#8217;s answer to browser-side scripting); one one side, this is definitely a good thing. However, not only are there many ways to solve the same problem, but [...]]]></description>
			<content:encoded><![CDATA[<p>One of the things that I have noticed about JavaScript is that there are <em>many ways to solve the same problem</em> (sometimes I even think of it like perl&#8217;s answer to browser-side scripting); one one side, this is definitely a good thing.</p>
<p>However, not only are there many ways to solve the same problem, but <em>everyone </em>that tries to solve that same problem produces JavaScript code that is so <em><strong>radically different</strong></em> from any previous attempt at the problem that it makes these problems seem <strong>more difficult</strong> than they really are.<br />
<span id="more-317"></span><br />
<span style="color: #3366ff;"><strong>What do you mean?</strong></span></p>
<p>After poking around with raw JavaScript enough and looking at other people&#8217;s raw JS code, I can see that not many people have any clue what the b<strong>est-practices and coding styles</strong> are.  This means that a <span style="text-decoration: underline;"><strong>huge percentage</strong></span> of the JavaScript code out there is just a <em><strong>bunch of hack</strong></em>.</p>
<p><span style="color: #3366ff;"><strong>Hack? Who said anything about hacking?</strong></span></p>
<p>Not &#8220;hacking&#8221;, silly&#8230;</p>
<blockquote><p><em>In modern computer programming, a &#8220;hack&#8221; can refer to a solution or method which functions correctly but which is &#8220;ugly&#8221; in its concept, which works outside the accepted structures and norms of the environment, or which is not easily extendable or maintainable (see <a title="Kludge" href="http://en.wikipedia.org/wiki/Kludge">kludge</a>)</em> &#8212; <a href="http://en.wikipedia.org/wiki/Hack_(technology)">Wikipedia</a></p></blockquote>
<p>If you come from a software development background, you know what hack code looks like and how fun it is to try and read it or learn anything of consequence from it. So?  So what? Read on.</p>
<h3 class="hrule"><span style="color: #3366ff;"><strong>The Root of the Problem</strong></span></h3>
<p>Why doesn&#8217;t this problem exist <em>as much</em> in languages like Java/C++ and other Object-Oriented languages. Well I think that it&#8217;s due to the nature of the JavaScript language; <span style="color: #ff0000;"><em><strong>it&#8217;s inheritance pattern is <a href="http://javascript.crockford.com/prototypal.html"><span style="text-decoration: underline;">prototypal</span></a> and not object based.</strong></em></span></p>
<p>As we said in another article &#8216;<a href="http://www.engfers.com/2008/08/29/javascript-is-not-truly-object-oriented/"><strong>JavaScript Is Not &#8220;Truly&#8221; Object Oriented</strong></a>&#8216;, the difference between prototypal and object-based inheritance is the same difference between a &#8220;HAS-A&#8221; relationship versus a &#8220;IS-A&#8221; relationship.</p>
<p>Anyways, since there are no definitive classes, there is <em>way less</em> enforced structure on coding styles and conventions; there is a reason for this though, keep in mind that JavaScript is a scripting language!</p>
<p>Yeah, we all know that when the JavaScript language was developed they might not have expected it to be used so much; however, in today&#8217;s world, JS is used very heavily.  Many applications are written entirely in JS!</p>
<p>Wouldn&#8217;t it be nice if a newer version of JS supported classes? It would give programmers a way to funnel their JavaScript coding styles! However, crap-in always equals crap-out.</p>
<h3 class="hrule"><span style="color: #3366ff;"><strong>JavaScript Libraries &#8211; A Partial Remedy</strong></span></h3>
<p>Thank God that someone had an idea a while ago to make a sensible collection of JavaScript code into what we now know as today&#8217;s Javascript Libraries and Frameworks!</p>
<p>This helped out somewhat. People started to use these libraries to un-compicate their coding lives by not worrying about browser differences or making ugly calls on the &#8216;<strong>document</strong>&#8216; object to manipulate and query the DOM.</p>
<p>Libraries like <a href="http://code.google.com/webtoolkit/">GWT</a>, <a href="http://developer.yahoo.com/yui/">YUI</a>, <a href="http://mootools.net/">Mootools</a>, <a href="http://jquery.com/">JQuery</a>, <a href="http://www.dojotoolkit.org/">Dojo</a>, <a href="http://www.prototypejs.org/">Prototype</a>, and any of the other <a href="http://en.wikipedia.org/wiki/List_of_JavaScript_libraries#JavaScript"><strong>tons of JS libraries</strong></a> out there do different things to make your coding lives easier and more readable. Google Web Toolkit (GWT) is a java based HTML/JS generator where you write all of your interface code in Java using AWT and Swing-like coding convention; mootools has a simplified &#8220;Class&#8221; object that you can &#8220;extend&#8221;.</p>
<h3 class="hrule"><span style="color: #3366ff;">Quit Complaining if You Don&#8217;t Have a Solution</span></h3>
<p>Ok so my proposed solution is for everyone that&#8217;s not using a JS framework or the framework doesn&#8217;t support some type of code organization to use the Douglas Crockford&#8217;s <a href="http://www.engfers.com/code/javascript-module-pattern/"><strong>JavaScript Module Pattern</strong></a>. It gives you a way to organize your code into something that may make a little more sense.</p>
]]></content:encoded>
			<wfw:commentRss>http://www.engfers.com/2008/09/12/the-main-reason-for-javascript-unreadability/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>JavaScript Is Not &#8220;Truly&#8221; Object Oriented</title>
		<link>http://www.engfers.com/2008/08/29/javascript-is-not-truly-object-oriented/</link>
		<comments>http://www.engfers.com/2008/08/29/javascript-is-not-truly-object-oriented/#comments</comments>
		<pubDate>Fri, 29 Aug 2008 16:34:26 +0000</pubDate>
		<dc:creator>engfer</dc:creator>
				<category><![CDATA[Javascript]]></category>

		<guid isPermaLink="false">http://www.engfers.com/?p=276</guid>
		<description><![CDATA[What do you mean? You are wrong, JavaScript is an Object Oriented Programming language! NO&#8230; IT&#8217;S&#8230; NOT! If anyone else tries to tell you that, you need to slap them! It has object-oriented like features but it is not object-oriented in nature! Prove it! Classical definitions for object-oriented programming languages usually talk about two main [...]]]></description>
			<content:encoded><![CDATA[<p><span style="color: #3366ff;"><strong>What do you mean? You are wrong, JavaScript <span style="text-decoration: underline;">is</span> an Object Oriented Programming language!</strong></span></p>
<h3 style="text-align: center;"><span style="color: #ff0000;"><strong>NO&#8230; IT&#8217;S&#8230; NOT!</strong></span></h3>
<p>If anyone else tries to tell you that, you need to slap them! It has object-oriented like <strong><em>features </em></strong>but it is <span style="text-decoration: underline;"><strong>not</strong></span> object-oriented in nature!</p>
<p><span style="color: #3366ff;"><strong>Prove it!</strong></span><br />
<span id="more-276"></span><br />
<a href="http://en.wikipedia.org/wiki/Object-oriented_programming"><strong>Classical definitions</strong></a> for object-oriented programming languages usually talk about two <em>main </em>features that make a language object-oriented:</p>
<h4><span style="color: #ff0000;"><strong>1) Encapsulation</strong></span></h4>
<blockquote><p><em>In <a title="Computer science" href="http://en.wikipedia.org/wiki/Computer_science">computer science</a>, the principle of <strong>information hiding</strong> </em>[aka encapsulation] <em>is the hiding of </em><em>design decisions in a computer program that are most likely to change, thus protecting other parts of the program from change if the design decision is changed. The protection involves providing a stable <a title="Interface (computer science)" href="http://en.wikipedia.org/wiki/Interface_%28computer_science%29">interface</a> which shields the remainder of the program from the implementation (the details that are most likely to change).</em> &#8212; <a href="http://en.wikipedia.org/wiki/Encapsulation_(classes_-_computers)">WIkipedia</a></p></blockquote>
<p>JavaScript does <strong>not </strong>support information hiding.  There are <strong>neither &#8220;true&#8221; private nor protected members/methods. Everything is public!</strong></p>
<p>If I create an &#8220;Object&#8221; in JavaScript, any properties I put in that object can be accessed and modified by anyone who has a handle on that object!</p>
<h4><span style="color: #ff0000;"><strong>2) Polymorphic Inheritance </strong></span></h4>
<blockquote><p><em>More precisely, polymorphism (<a title="Object-oriented programming" href="http://en.wikipedia.org/wiki/Object-oriented_programming">object-oriented programming</a> theory) is the ability of <a title="Object (computer science)" href="http://en.wikipedia.org/wiki/Object_%28computer_science%29">objects</a><a title="Data type" href="http://en.wikipedia.org/wiki/Data_type">types</a> to respond to <a title="Method (computer science)" href="http://en.wikipedia.org/wiki/Method_%28computer_science%29">method</a> calls of the same name, each one according to an appropriate type-specific behavior. The programmer (and the program) does not have to know the exact type of the object in advance, so this behavior can be implemented at run time (this is called </em> belonging to different <em>late binding or </em><em><a title="Dynamic binding" href="http://en.wikipedia.org/wiki/Dynamic_binding">dynamic binding</a>)</em>. &#8212; <a href="http://en.wikipedia.org/wiki/Polymorphism_in_object-oriented_programming">Wikipedia</a></p></blockquote>
<p>Polymorphic inheritance is the main driving reason that gives Java and C++/C# its user base.  It allows for an easy way for <strong>code reuse</strong> and <strong>object generalization</strong>. From the sub-class, you can access members and methods of the super-class; <span style="text-decoration: underline;">JavaScript can&#8217;t do this</span>.</p>
<p>The reason for this is explained by the differences between <strong>HAS-A</strong> and <strong>IS-A object relationships</strong></p>
<p><strong>IS-A relationships</strong> contain a hierarchy of objects where any sub-class of an object <strong>IS</strong> also a type of the super-class!</p>
<p style="padding-left: 30px;">When ObjectB extends another ObjectA, the type of the ObjectB is both ObjectB and ObjectA! That way I can make other objects that pass around ObjectA&#8217;s which may or may not be my ObjectB!</p>
<p>Think of the classical Shape example. There are a bunch of shapes out there and they all share some properties, but they do have many different properties that they don&#8217;t share. Well why not put the shared properties into a super-class and have shapes sub-class them.</p>
<pre>// Shape base Object
public class Shape {
    protected String name = "Shape";
    protected int sides = 0;

    public String toString() {
        return this.name + " (" + this.sides + " sides)";
    }
}

public class Square extends Shape {
    public Square() {
        this.name = "Square";
        this.sides = 4;
    }

    public String toString() {
        return "It's hip to be a " + super.toString();
    }
}

public class Driver {
    public static void main( String [] args ) {
        Shape myShape = new Square();
        System.out.println( myShape );
    }
}</pre>
<p>In the above example we can create a Shape object (myShape) and set it to a new Square. <em>Why does this work?</em> <strong>IS-A relationships</strong> dictate that <strong>since Square extends Shape, Square must also be a Shape</strong>. Therefore, we can store sub-classes of Shape into a Shape object reference and it will work fine.</p>
<p>Moreover, when the toString() method is called on myShape, <strong>dynamic binding</strong> helps to correctly call the Square.toString() method instead of Shape.toString()!</p>
<p><span style="color: #3366ff;"><strong>HAS-A</strong></span> <strong>relationships</strong> exists via a form of <a href="http://en.wikipedia.org/wiki/Object_composition">object composition</a> (where 1 object contains another object and uses it&#8217;s methods).</p>
<p style="padding-left: 30px;"><span style="color: #ff0000;"><strong>In JavaScript, &#8220;extending an object&#8221; (doesn&#8217;t really exist) means just <em><span style="text-decoration: underline;">appending</span> </em></strong><strong>the super-objects <em>members </em>and <em>methods </em>to the sub-object.</strong></span> It&#8217;s really not any more difficult than that; you can&#8217;t ask or test a JavaScript Object if it is both the super-object and sub-object types.</p>
<p style="padding-left: 30px;">This is called <strong><a href="http://javascript.crockford.com/prototypal.html"><em>prototypal inheritance</em></a></strong>.</p>
<pre>// Shape base Object
var Shape = function() {
    this.name = "Shape";
    this.sides = 0;

    this.toString = function() {
        return this.name + " (" + this.sides + " sides)";
    };
};

var Square = function() {
    this.name = "Square";
    this.sides = 4;
};

// Starts the HAS-A relationship
Square.prototype = new Shape();

// Test new Square
alert( new Square() );</pre>
<p>If you needed to access anything from the super-object, there is <em>no way </em>of doing that using JavaScript&#8217;s object model unless you followed object composition where Square carried around an <em>instance </em>of Shape.</p>
<p>Moreover there is no way create a Shape reference and have it stored as a Square as in the previous Java example because <strong>HAS-A relationships dictate that members and methods are only appended to sub-classes</strong>.<br />
<div align="center"><script type="text/javascript"><!--
google_ad_client = "pub-9461422058527053";
google_ad_slot = "5057152483";
google_ad_width = 468;
google_ad_height = 60;
//--></script>
<script type="text/javascript" src="http://pagead2.googlesyndication.com/pagead/show_ads.js"></script>
</div></p>
<h3><span style="color: #3366ff;"><strong>Why Did You Even Write This Article?</strong></span></h3>
<p>I am tired of people writing stuff in/about JavaScript and calling it Object Oriented without explaining the difference between true object-oriented languages and scripting languages like JavaScript.</p>
<p>OOP Developers trying to learn JavaScript need to understand the fundamental difference between Java and JavaScript (is-a vs. has-a); just because JavaScript has &#8220;Java&#8221; in the name doesn&#8217;t mean it&#8217;s anyway related to Java.</p>
]]></content:encoded>
			<wfw:commentRss>http://www.engfers.com/2008/08/29/javascript-is-not-truly-object-oriented/feed/</wfw:commentRss>
		<slash:comments>4</slash:comments>
		</item>
	</channel>
</rss>
