<?xml version="1.0" encoding="utf-8" ?>
<rss version="2.0"
   xmlns:rdf="http://www.w3.org/1999/02/22-rdf-syntax-ns#"
   xmlns:admin="http://webns.net/mvcb/"
   xmlns:dc="http://purl.org/dc/elements/1.1/"
   xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
   xmlns:wfw="http://wellformedweb.org/CommentAPI/"
   xmlns:content="http://purl.org/rss/1.0/modules/content/"
   >
<channel>
    <title>Eduzine© - OOAD</title>
    <link>http://eduzine.edujini-labs.com/</link>
    <description>Articles from Edujini Team</description>
    <dc:language>en</dc:language>
    <admin:errorReportsTo rdf:resource="mailto:" />
    <generator>Serendipity 0.8.2 - http://www.s9y.org/</generator>
    <pubDate>Wed, 18 Jun 2008 07:43:08 GMT</pubDate>

    <image>
        <url>http://www.edujini-labs.com/images/newmasthead.gif</url>
        <title>RSS: Eduzine© - OOAD - Articles from Edujini Team</title>
        <link>http://eduzine.edujini-labs.com/</link>
        <width></width>
        <height></height>
    </image>
<item>
    <title>Exploring OOPS - JavaScript Style: Part 3 - Polymorphism</title>
    <link>http://eduzine.edujini-labs.com/archives/36-Exploring-OOPS-JavaScript-Style-Part-3-Polymorphism.html</link>
<category>JavaScript</category><category>Web 2.0</category><category>RIA</category><category>OOAD</category>    <comments>http://eduzine.edujini-labs.com/archives/36-Exploring-OOPS-JavaScript-Style-Part-3-Polymorphism.html#comments</comments>
    <wfw:comment>http://eduzine.edujini-labs.com/wfwcomment.php?cid=36</wfw:comment>
    <slash:comments>2</slash:comments>
    <wfw:commentRss>http://eduzine.edujini-labs.com/rss.php?version=2.0&amp;type=comments&amp;cid=36</wfw:commentRss>
    <author>eduzine@edujinionline.com (Eduzine)</author>
    <content:encoded>

&lt;h2&gt;Introduction&lt;/h2&gt;

&lt;p&gt;
In the third and last article of the series of &amp;quot;Exploring OOPS - JavaScript Style&amp;quot;, we look at polymorphism in JavaScript.
&lt;/p&gt;

&lt;p&gt;
Polymorphism literally means &amp;quot;multiple forms&amp;quot;. In OOAD, it means that an object reference can take different behave differently depending upon the scenario.
&lt;/p&gt;

&lt;p&gt;
Technically, there are two types of polymorphism - compile-time polymorphism and runtime polymorphism. One of the ways to implement compile-time polymorphism is method overloading while runtime polymorphism can be implemented by method overriding.
&lt;/p&gt;

&lt;h2&gt;Downloads&lt;/h2&gt;

&lt;p&gt;
The downloads are available in the &lt;a href=&quot;http://downloads.edujini-labs.com&quot;&gt;Downloads&lt;/a&gt; area.
&lt;/p&gt;

 
&lt;h2&gt;Compile-time Polymorphism&lt;/h2&gt;

&lt;p&gt;
All functions in JavaScript are overloaded by default. Surprised, isn't it? Let's take a simple code below for analysis:
&lt;/p&gt;

&lt;pre&gt;&amp;lt;html&amp;gt;
  &amp;lt;head&amp;gt;
    &amp;lt;title&amp;gt;OOPS in JavaScript - Polymorphism&amp;lt;/code&amp;gt;
    &amp;lt;script language='javascript' type='text/javascript'&amp;gt;
      /**
       &lt;strong&gt; Part of the Eduzine (http://eduzine.edujini-labs.com) Articles.
       &lt;/strong&gt;
       &lt;strong&gt; Downloads available at http://downloads.edujini-labs.com
       &lt;/strong&gt;
       &lt;strong&gt; (C) 2008, Edujini Labs Pvt Ltd
       &lt;/strong&gt; http://www.edujini-labs.com
       &lt;strong&gt;/

      function addMethod(x, y)
      {
        document.writeln('x: ' + x + ', y: ' + y);
      }
    &amp;lt;/script&amp;gt;
  &amp;lt;/head&amp;gt;
  &amp;lt;body&amp;gt;
  &amp;lt;pre&amp;gt;&amp;lt;script language='javascript' type='text/javascript'&amp;gt;
      addMethod();
      addMethod(1);
      addMethod(1, 2);
      addMethod(1, 2, 3);
      addMethod(1, 2, 3, 4);
    &amp;lt;/script&amp;gt;
  &amp;lt;/pre&amp;gt;
  &amp;lt;/body&amp;gt;
&amp;lt;/html&amp;gt;
&lt;/pre&gt;

&lt;p&gt;
For the time being, do not worry about the actual results. What is more important is that invocation of these methods do not result in any error whatsoever! And that's because all methods in JavaScript are, by default, overloaded. And did somebody say - that's compile-time polymorphism?
&lt;/p&gt;

&lt;p&gt;
What we provide in the parameter-list to a JavaScript function is just names to those parameters so that it is convenient to access them. All parameters passed to a function are available through an automatically-created local-variable &lt;code&gt;arguments&lt;/code&gt;. Let's redefine the same method to access all parameters passed to it, convert each into number and return the their sum (if they are not NaN).
&lt;/p&gt;

&lt;pre&gt;&amp;lt;html&amp;gt;
  &amp;lt;head&amp;gt;
    &amp;lt;title&amp;gt;OOPS in JavaScript - Polymorphism&amp;lt;/code&amp;gt;
    &amp;lt;script language='javascript' type='text/javascript'&amp;gt;
      /**
       &lt;/strong&gt; Part of the Eduzine (http://eduzine.edujini-labs.com) Articles.
       &lt;strong&gt;
       &lt;/strong&gt; Downloads available at http://downloads.edujini-labs.com
       &lt;strong&gt;
       &lt;/strong&gt; (C) 2008, Edujini Labs Pvt Ltd
       &lt;strong&gt; http://www.edujini-labs.com
       &lt;/strong&gt;/

      function addMethod(x, y)
      {
        &lt;b&gt;var rv = 0;
        var length = arguments.length;
        var n;

        for(var i = 0; i &amp;lt; length; i++)
        {
          n = Number(arguments[i]);
          if(!isNaN(n))
          {
            rv += n;
          }
        }
        return rv;&lt;/b&gt;
      }
    &amp;lt;/script&amp;gt;
  &amp;lt;/head&amp;gt;
  &amp;lt;body&amp;gt;
  &amp;lt;pre&amp;gt;&amp;lt;script language='javascript' type='text/javascript'&amp;gt;
      &lt;b&gt;document.writeln(addMethod());
      document.writeln(addMethod(1));
      document.writeln(addMethod(1, 2));
      document.writeln(addMethod(1, 2, 3));
      document.writeln(addMethod(1, 2, 3, 4));&lt;/b&gt;
    &amp;lt;/script&amp;gt;
  &amp;lt;/pre&amp;gt;
  &amp;lt;/body&amp;gt;
&amp;lt;/html&amp;gt;
&lt;/pre&gt;

&lt;p&gt;
&lt;code&gt;arguments&lt;/code&gt; if of the type &lt;a href=&quot;http://developer.mozilla.org/en/docs/Core_JavaScript_1.5_Reference:Functions:arguments&quot;&gt;&lt;code&gt;Arguments&lt;/code&gt;&lt;/a&gt;. Apart from other properties, one critical thing to which it provides access to is all the parameters passed to the function while invoking it.
&lt;/p&gt;

&lt;p&gt;
Though it may sound unusual for those who are used to working with languages like C, C++, Java, C# etc but probably they can relate this to &lt;code&gt;varargs&lt;/code&gt;. And the same mechanism is available for instance methods (for custom types) as well.
&lt;/p&gt;

&lt;h2&gt;Runtime Polymorphism&lt;/h2&gt;

&lt;p&gt;
Method overriding? Haven't we already discussed this in the previous article while &lt;a href=&quot;http://eduzine.edujini-labs.com/archives/35-Exploring-OOPS-JavaScript-Style-Part-2-Inheritance.html&quot;&gt;discussing about inheritance&lt;/a&gt;?
&lt;/p&gt;

&lt;h2&gt;Summary&lt;/h2&gt;

&lt;p&gt;
In this article we learnt about polymorphism in JavaScript.
&lt;/p&gt;

    </content:encoded>
    <pubDate>Wed, 18 Jun 2008 00:34:10 -0700</pubDate>
    <guid isPermaLink="false">http://eduzine.edujini-labs.com/archives/36-guid.html</guid>
    </item>
<item>
    <title>Exploring OOPS - JavaScript Style: Part 1 - Encapsulation</title>
    <link>http://eduzine.edujini-labs.com/archives/34-Exploring-OOPS-JavaScript-Style-Part-1-Encapsulation.html</link>
<category>JavaScript</category><category>Web 2.0</category><category>RIA</category><category>OOAD</category>    <comments>http://eduzine.edujini-labs.com/archives/34-Exploring-OOPS-JavaScript-Style-Part-1-Encapsulation.html#comments</comments>
    <wfw:comment>http://eduzine.edujini-labs.com/wfwcomment.php?cid=34</wfw:comment>
    <slash:comments>3</slash:comments>
    <wfw:commentRss>http://eduzine.edujini-labs.com/rss.php?version=2.0&amp;type=comments&amp;cid=34</wfw:commentRss>
    <author>eduzine@edujinionline.com (Eduzine)</author>
    <content:encoded>

&lt;h2&gt;Introduction&lt;/h2&gt;

&lt;p&gt;
As promised in one of the earlier articles, we discuss about how the various pillars of OOPS are implemented in JavaScript.
&lt;/p&gt;

&lt;p&gt;
Obect Oriented Programming - as defined by Grady Booch - stands on three pillars:
&lt;/p&gt;
&lt;ul&gt;
  &lt;li&gt;Encapsulation&lt;/li&gt;
  &lt;li&gt;Inheritance&lt;/li&gt;
  &lt;li&gt;Polymorphism&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;
Here, we explore how all three - Yes! All Three Pillars of OOPS - are implemented in JavaScript.
&lt;/p&gt;

&lt;h2&gt;Downloads&lt;/h2&gt;

&lt;p&gt;
The downloads are available in the &lt;a href=&quot;http://downloads.edujini-labs.com&quot;&gt;Downloads&lt;/a&gt; area.
&lt;/p&gt;

 
&lt;h2&gt;Getting Started&lt;/h2&gt;

&lt;p&gt;
To get started, it is assumed that you are already familiar with basic JavaScript. If not you can look on the web for some tutorials on JavaScript 101 tutorials or the like. To be specific, the following fundamentals are assume:
&lt;/p&gt;

&lt;ul&gt;
  &lt;li&gt;Declaring variables - &lt;code&gt;var&lt;/code&gt; keyword.&lt;/li&gt;
  &lt;li&gt;Basic data types supported - &lt;code&gt;Object&lt;/code&gt;, &lt;code&gt;Array&lt;/code&gt;, &lt;code&gt;Boolean&lt;/code&gt;, &lt;code&gt;Number&lt;/code&gt;, &lt;code&gt;String&lt;/code&gt; and &lt;code&gt;Function&lt;/code&gt;.&lt;/li&gt;
  &lt;li&gt;Writing functions - well, you got to be really very strong in functions!&lt;/li&gt;
&lt;/ul&gt;

&lt;h2&gt;4-Sutras while working with JavaScript&lt;/h2&gt;

&lt;p&gt;
While working with JavaScript, always keep in mind the following, as &lt;a href=&quot;http://www.mastergaurav.com/&quot;&gt;Gaurav Vaish&lt;/a&gt; calls them, &lt;a href=&quot;http://blogs.mastergaurav.com/2008/06/16/4-sutras-to-work-with-javascript/&quot;&gt;&amp;quot;4-Sutras&amp;quot;&lt;/a&gt;:
&lt;/p&gt;

&lt;ol&gt;
  &lt;li&gt;All data-types are infinitely flexible.&lt;/li&gt;
  &lt;li&gt;All objects (references; instances) are infinitely extensible.&lt;/li&gt;
  &lt;li&gt;Objects are associative arrays.&lt;/li&gt;
  &lt;li&gt;It's all about functions.&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;
&amp;quot;In JavaScript, whenever you run into any problem related to implementation or structuring of the code, these four sutras will help you out&amp;quot;, says &lt;a href=&quot;http://blogs.mastergaurav.com/2008/06/16/4-sutras-to-work-with-javascript/&quot;&gt;Gaurav&lt;/a&gt;.
&lt;/p&gt;

&lt;h2&gt;Encapsulation&lt;/h2&gt;

&lt;p&gt;
Encapsulation - in simplest terms - can be defined as putting the related items together. Structures in C is a way to implement encapsulation. Classes in C++, Java, C# etc is another way to implement encapsulation.
&lt;/p&gt;

&lt;p&gt;
Let us see how encapsulation is implemented in JavaScript.
&lt;/p&gt;

&lt;h2&gt;Welcome, Functions!&lt;/h2&gt;

&lt;p&gt;
Gaurav's 4&lt;sup&gt;th&lt;/sup&gt; Sutra comes into action here - &amp;quot;It's all about functions&amp;quot;.
&lt;/p&gt;

&lt;p&gt;
Let us define a data-type &lt;code&gt;UserProfile&lt;/code&gt; that encapsulates the following three items:
&lt;/p&gt;

&lt;ol&gt;
  &lt;li&gt;&lt;code&gt;username&lt;/code&gt;: Property to hold the username of the user.&lt;/li&gt;
  &lt;li&gt;&lt;code&gt;password&lt;/code&gt;: Property to hold the password of the user.&lt;/li&gt;
  &lt;li&gt;&lt;code&gt;authenticate&lt;/code&gt;: Method to authenticate the user. Returns &lt;code&gt;true&lt;/code&gt; if successful, &lt;code&gt;false&lt;/code&gt; otherwise.&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;
Let us implement the &lt;code&gt;UserProfile&lt;/code&gt; with a constructor that takes the two properties as parameters and initializes itself. Create a file &lt;code&gt;UserProfile.js&lt;/code&gt; (strictly speaking, the filename doesn't really matter) with the following code.
&lt;/p&gt;

&lt;pre&gt;/**&lt;br /&gt; &lt;strong&gt; Part of the Eduzine (http://eduzine.edujini-labs.com) Articles.&lt;br /&gt; &lt;/strong&gt;&lt;br /&gt; &lt;strong&gt; Downloads available at http://downloads.edujini-labs.com&lt;br /&gt; &lt;/strong&gt;&lt;br /&gt; &lt;strong&gt; (C) 2008, Edujini Labs Pvt Ltd&lt;br /&gt; &lt;/strong&gt; http://www.edujini-labs.com&lt;br /&gt; &lt;strong&gt;/&lt;br /&gt;&lt;br /&gt;function UserProfile(username, password)&lt;br /&gt;{&lt;br /&gt;  this.username = username;&lt;br /&gt;  this.password = password;&lt;br /&gt;&lt;br /&gt;  this.authenticate = function()&lt;br /&gt;  {&lt;br /&gt;    if(this.username == 'gvaish' &amp;amp;&amp;amp; this.password == 'edujini')&lt;br /&gt;    {&lt;br /&gt;      return true;&lt;br /&gt;    }&lt;br /&gt;    return false;&lt;br /&gt;  }&lt;br /&gt;}&lt;/pre&gt;

&lt;p&gt;
Notice the use of &lt;code&gt;this&lt;/code&gt; keyword. Does it remind you of someting? Yes! You got it right... &lt;code&gt;function&lt;/code&gt; is the way to implement the concept of &lt;code&gt;class&lt;/code&gt;, or to speak - encapsulation.
&lt;/p&gt;

&lt;h2&gt;Test Case&lt;/h2&gt;

&lt;p&gt;
Ok... so, how do we use this new data-type that we just defined? Let's create a HTML file that will serve as the playground for our case-studies around the &lt;code&gt;UserProfile&lt;/code&gt;. Create a file &lt;code&gt;UserProfile.html&lt;/code&gt; (again, filename is not important at all) with the following content:
&lt;/p&gt;

&lt;pre&gt; 1 &amp;lt;html&amp;gt;&lt;br /&gt; 2   &amp;lt;head&amp;gt;&lt;br /&gt; 3     &amp;lt;title&amp;gt;OOPS in JavaScript- Encapsulation&amp;lt;/code&amp;gt;&lt;br /&gt; 4     &amp;lt;script language='javascript' type='text/javascript' src='UserProfile.js'&amp;gt;&amp;lt;/script&amp;gt;&lt;br /&gt; 5     &amp;lt;script language='javascript' type='text/javascript'&amp;gt;&lt;br /&gt; 6       /**&lt;br /&gt; 7        &lt;/strong&gt; Part of the Eduzine (http://eduzine.edujini-labs.com) Articles.&lt;br /&gt; 8        &lt;strong&gt;&lt;br /&gt; 9        &lt;/strong&gt; Downloads available at http://downloads.edujini-labs.com&lt;br /&gt;10        &lt;strong&gt;&lt;br /&gt;11        &lt;/strong&gt; (C) 2008, Edujini Labs Pvt Ltd&lt;br /&gt;12        &lt;strong&gt; http://www.edujini-labs.com&lt;br /&gt;13        &lt;/strong&gt;/&lt;br /&gt;14       function validateUser()&lt;br /&gt;15       {&lt;br /&gt;16         var uname = document.getElementById('u').value;&lt;br /&gt;17         var pwd = document.getElementById('p').value;&lt;br /&gt;18&lt;br /&gt;19         var e = document.getElementById('result');&lt;br /&gt;20&lt;br /&gt;21         var u = new UserProfile(uname, pwd);&lt;br /&gt;22         var result = u.authenticate();&lt;br /&gt;23         e.innerHTML = 'Result: ' + result;&lt;br /&gt;24       }&lt;br /&gt;25     &amp;lt;/script&amp;gt;&lt;br /&gt;26   &amp;lt;/head&amp;gt;&lt;br /&gt;27   &amp;lt;body&amp;gt;&lt;br /&gt;28&lt;br /&gt;29   Username: &amp;lt;input type='text' name='u' id='u' /&amp;gt;&lt;br /&gt;30   &amp;lt;br/&amp;gt;&lt;br /&gt;31   Password: &amp;lt;input type='password' name='p' id='p' /&amp;gt;&lt;br /&gt;32   &amp;lt;br/&amp;gt;&lt;br /&gt;33&lt;br /&gt;34   &amp;lt;button onclick='validateUser(); return false;'&amp;gt;Login&amp;lt;/button&amp;gt;&lt;br /&gt;35&lt;br /&gt;36   &amp;lt;div id='result'&amp;gt;&amp;lt;/div&amp;gt;&lt;br /&gt;37&lt;br /&gt;38   &amp;lt;/body&amp;gt;&lt;br /&gt;30 &amp;lt;/html&amp;gt;&lt;/pre&gt;

&lt;h2&gt;Analysis&lt;/h2&gt;

&lt;p&gt;
Let's just analyze the code that we just wrote.
&lt;/p&gt;

&lt;p&gt;
We have designed a simple form comprising of inputs for username and password, and a button to trigger validation.
&lt;/p&gt;

&lt;p&gt;
The function &lt;code&gt;validateUser&lt;/code&gt; defined at line 14-24 triggers the validation by extracting the values of username and password (lines 16-17).
&lt;/p&gt;

&lt;p&gt;
At line 21, &lt;code&gt;UserProfile&lt;/code&gt; is instantiated. Notice a very important thing - the function definition has been used as the constructor. Yes! That's where functions are so important in JavaScript. The functions in JavaScript are higher order functions (similar to that in Smalltalk), that can be used to retain the state.
&lt;/p&gt;

&lt;p&gt;
At line 22, we invoke the method &lt;code&gt;authenticate&lt;/code&gt; that returns a value &lt;code&gt;true&lt;/code&gt; or &lt;code&gt;false&lt;/code&gt; depending upon what we enter in the form.
&lt;/p&gt;

&lt;p&gt;
So, go ahead and try out with different combinations of username and password, and checkout the results!
&lt;/p&gt;

&lt;h2&gt;Summary&lt;/h2&gt;

&lt;p&gt;
In this article we learnt how encapsulation is implemented in JavaScript. To summarize, we explored the following:
&lt;/p&gt;

&lt;ul&gt;
	&lt;li&gt;Encapsulation in JavaScript.&lt;/li&gt;
	&lt;li&gt;Use of &lt;code&gt;this&lt;/code&gt; keyword within the type definition.&lt;/li&gt;
	&lt;li&gt;Defining properties and methods in JavaScript.&lt;/li&gt;
&lt;/ul&gt;

    </content:encoded>
    <pubDate>Mon, 16 Jun 2008 21:53:49 -0700</pubDate>
    <guid isPermaLink="false">http://eduzine.edujini-labs.com/archives/34-guid.html</guid>
    </item>
</channel>
</rss>
