Click here to Skip to main content
12,829,660 members (45,796 online)
Click here to Skip to main content
Add your own
alternative version


12 bookmarked
Posted 19 Jun 2008

Exploring OOPS - JavaScript Style: Part 3 - Polymorphism

, 2 Aug 2008 BSD
Rate this:
Please Sign up or sign in to vote.
This article, 3rd Part in the series of 3-articles, discusses Polymorphism in JavaScript


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.


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:

    <title>OOPS in JavaScript - Polymorphism</code>

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

    <title>OOPS in JavaScript - Polymorphism</code>

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?


In this article we learnt about polymorphism in JavaScript.


This article, along with any associated source code and files, is licensed under The BSD License


About the Author

India India

Gaurav lives in Bangalore, the Silicon Valley of India.

His technical strength lies in having deep understanding of not one or two but bunch of enterprise frameworks.

Lately, he is working on an MVC framework implementation for Android put in open domain at

Today, he is an independent guy:

  • He is a programming language independent guy, well almost. He is proficient in C, C++, Java, C#, VB.Net, C++.Net, JavaScript, PHP, Tcl, Python, Ruby

  • He is an OS independent guy, well almost. Has worked and developed at length on HP-UX, Linux (Redhat / Mandrake), Macintosh (9, 10.x), Windows (NT, 2000, 2003, XP, Vista), Solaris (8, 9, 10); and mobile platforms Android, iPhone, Blackberry

  • He is a target independent guy, well almost. Has worked on thick clients (mainly desktops) as well as thin clients (mainly alternative platforms - Symbian, PalmOS, WinCE, WinXP Embedded, Linux Embedded, Android, iPhone, Blackberry)

Today, his thrust areas are Service Oriented Architecture (implementation expert in Java, .Net and PHP; integration with Mainframe and other legacy environments), Mobile Technologies and RFID.

He holds a Bachelor's Degree (B. Tech.) from IIT Kanpur His major was in EE with specialization in DSP (SSP).

His hobby is listening music, reading books (no, he can't read novels), photography and travelling.

Should you wish to talk to him, you can drop him a mail at gaurav[dot]vaish[at]gmail[dot]com. He generally responds within 24-48 hrs unless there is a compelling reason.

And yeah... here's his personal website:

Smile | :)

You may also be interested in...

Comments and Discussions

Generalnice article. Pin
Rajib Ahmed19-Jun-08 18:03
memberRajib Ahmed19-Jun-08 18:03 
GeneralRe: nice article. Pin
mastergaurav20-Jun-08 5:00
membermastergaurav20-Jun-08 5:00 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.

Permalink | Advertise | Privacy | Terms of Use | Mobile
Web02 | 2.8.170326.1 | Last Updated 2 Aug 2008
Article Copyright 2008 by mastergaurav
Everything else Copyright © CodeProject, 1999-2017
Layout: fixed | fluid