Click here to Skip to main content
Email Password   helpLost your password?

Introduction

In the third and last article of the series of "Exploring OOPS - JavaScript Style", we look at polymorphism in JavaScript.

Note: This article is an adaption (may be a mirror) of the article originally posted at Edujini™ Eduzine™ here.

All the three articles of the series:

Polymorphism literally means "multiple forms". In OOAD, it means that an object reference can take different behave differently depending upon the scenario.

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.

Downloads

The downloads are available in the Downloads area.

Compile-time Polymorphism

All functions in JavaScript are overloaded by default. Surprised, isn't it? Let's take a simple code below for analysis:

<html>
  <head>
    <title>OOPS in JavaScript - Polymorphism</code>
    <script language="'javascript'" type='text/javascript'>
      /**
       * (C) 2008, Gaurav Vaish, Edujini Labs Pvt Ltd
       * http://www.mastergaurav.com
       * http://eduzine.edujini-labs.com
       */

      function addMethod(x, y)
      {
        document.writeln('x: ' + x + ', y: ' + y);
      }
    </script>
  </head>
  <body>
  <pre><script language="'javascript'" type='text/javascript'>
      addMethod();
      addMethod(1);
      addMethod(1, 2);
      addMethod(1, 2, 3);
      addMethod(1, 2, 3, 4);
    </script>
  </pre>
  </body>
</html>

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?

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 arguments. 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).

<html>
  <head>
    <title>OOPS in JavaScript - Polymorphism</code>
    <script language="'javascript'" type='text/javascript'>
      /**
       * (C) 2008, Gaurav Vaish, Edujini Labs Pvt Ltd
       * http://www.mastergaurav.com
       * http://eduzine.edujini-labs.com
       */

      function addMethod(x, y)
      {
        var rv = 0;
        var length = arguments.length;
        var n;

        for(var i = 0; i < length; i++)
        {
          n = Number(arguments[i]);
          if(!isNaN(n))
          {
            rv += n;
          }
        }
        return rv;
      }
    </script>
  </head>
  <body>
  <pre><script language="'javascript'" type='text/javascript'>
      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));
    </script>
  </pre>
  </body>
</html>

arguments if of the type Arguments. Apart from other properties, one critical thing to which it provides access to is all the parameters passed to the function while invoking it.

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 varargs. And the same mechanism is available for instance methods (for custom types) as well.

Runtime Polymorphism

Method overriding? Haven't we already discussed this in the previous article while discussing about inheritance?

Summary

In this article we learnt about polymorphism in JavaScript.

You must Sign In to use this message board.
 
 
Per page   
 FirstPrevNext
Generalnice article.
Rajib Ahmed
18:03 19 Jun '08  
nice article.


GeneralRe: nice article.
mastergaurav
5:00 20 Jun '08  
Hi Rajib,

Great thanks for your response! I hope it helped...

--

Happy Hacking,
Gaurav Vaish
http://blogs.mastergaurav.com
http://eduzine.edujini-labs.com
---------------------------


Last Updated 2 Aug 2008 | Advertise | Privacy | Terms of Use | Copyright © CodeProject, 1999-2010