Welcome Guest! (App Version 3.1)

websitnero logo name

Polymorphism

Polymorphism Implemented in JavaScript

Polymorphism is the practice of designing objects such that they share behaviors and are able to override shared behaviors with specific ones or which give best results. In JavaScript, Polymorphism takes advantage of inheritance in order to make this happen.

Polymorphism is another important tenet of Object Oriented Language. Polymorphism basically represents that in a case where there are more than one function with same name and we call it (function name) on the object, then automatically that function is called that best suits the object. In various languages this is represented and fulfilled by following 2 ways -- Method Overloading and Method Overriding.

We need to remember that Polymorphism is best represented by these 2 pathways and in JavaScript also we will consider these ways and compare how can these bring in Polymorphism in JavaScript.

Method Overloading basically takes help of types of arguments to match the correct function, while Method Overriding takes closest one to the object rather than inherited one to call the function. As, in JavaScript, types of arguments are not bound to be same as called in function or JS will stop processing further on ... if types are not matched. So this one does not match nature of JavaScript. Lets look onto 2nd option -- Method Overriding.

Due to inheritance (Prototypal, of course), it might happen that via Prototype Chain, there might be more than one Methods with the same name (say alpha). So, due to laws of Prototypal Chain, the one closes to the object in the Prototype Chain will be picked up shadowing others. Thus, though there may be multiple functions (of same name) present in the Prototype Chain, but closest to the object will be picked up representing Polymorphism in JavaScript.

Example

<script>
/* 
//When there are more than 1 function with same name, the object 
will run the 'one' which is most meaningful 
// Polymorphism can be achieved via 'method overriding' or 'method 
overloading'
// Automatic selection of function in case multiple functions have 
same name is done in OOPs language based on:
// 1. No of arguments
// 2. Argument types
// As JavaScript doesn't force type of arguments, and in case count 
doesn't match, puts value as undefined, 
// only way left is via 'Prototypal Inheritance' and picking closest 
function via 'single chain Inheritance'
*/

var person = function(name, age, country){
  this.name = name; 
  this.age = age; 
  this.country = country;
}

person.prototype.printName = function(){return this.name;}
person.prototype.printAge = function(){return this.age;}
person.prototype.printSalary = function(){return "Do not have salary";}

var employee = function(name, age, country, salary){
  this.name = name;
  this.age = age;
  this.country = country;
  this.salary = salary;
}

employee.prototype = Object.create(person.prototype);
employee.prototype.printSalary = function(){return this.salary;}

var peter = new employee("Peter", 29, "USA", "$50000");
alert(peter.printName());
alert(peter.printAge());
alert(peter.printSalary()); //Depicting Polymorphism in JavaScript by selection best suited function when multiple functions have same name

</script>
WebsiteNero is highly optimized for learning various website developing technlogies. We try our best to add maximum modules and examples to help learn the concepts clearly and vividly. We try to present all content and examples as simple as we can removing any complexity to hurdle easy understanding. We try best provide you with worthful content, but we cannot guarantee full correctness of all the content on this site. While using this website, you agree to have read and accepted all our terms of use and conditions, cookie, and privacy policy. Copyright, 2013-2018 by websitenero.com. All Rights Reserved.