Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What is polymorphism in Javascript?

I have read some possible article I could found on the internet on polymorphism. But I think I could not quite grasp the meaning of it and its importance. Most of the articles don't say why it is important and how I can achieve polymorphic behavior in OOP (of course in JavaScript).

I can not provide any code example because I haven't got the idea how to implement it, so my questions are below:

  1. What is it?
  2. Why we need it ?
  3. How it works?
  4. How can I achieve this polymorphic behavior in javascript?

I have got this example. But it is easily understandable what will be outcome of this code. It doesn't give any clear idea about polymorphism itself.

function Person(age, weight) {     this.age = age;     this.weight = weight;     this.getInfo = function() {         return "I am " + this.age + " years old " +         "and weighs " + this.weight +" kilo.";     } } function Employee(age, weight, salary) {     this.salary = salary;     this.age = age;     this.weight = weight;     this.getInfo = function() {         return "I am " + this.age + " years old " +         "and weighs " + this.weight +" kilo " +         "and earns " + this.salary + " dollar.";     } }  Employee.prototype = new Person(); Employee.prototype.constructor = Employee;   // The argument, 'obj', can be of any kind   // which method, getInfo(), to be executed depend on the object   // that 'obj' refer to.  function showInfo(obj) {     document.write(obj.getInfo() + "<br>"); }  var person = new Person(50,90); var employee = new Employee(43,80,50000); showInfo(person); showInfo(employee); 
like image 481
AL-zami Avatar asked Dec 24 '14 21:12

AL-zami


People also ask

What is polymorphism in JavaScript with example?

Polymorphism takes advantage of inheritance in order to make this happen. In the following example child objects such as 'cricket' and 'tennis' have overridden the 'select' method called from parent object 'game' and returned a new string respectively as shown in the output.

Do we have polymorphism in JavaScript?

The way programming languages implement polymorphism differs. As an example, Java and JavaScript are both object-oriented programming languages and they don't handle polymorphism in the same manner.

What is polymorphism with example?

In simple words, we can define polymorphism as the ability of a message to be displayed in more than one form. A real-life example of polymorphism is a person who at the same time can have different characteristics. Like a man at the same time is a father, a husband and an employee.

What do you mean by polymorphism?

Definition of polymorphism : the quality or state of existing in or assuming different forms: such as. a(1) : existence of a species in several forms independent of the variations of sex. (2) : existence of a gene in several allelic forms also : a variation in a specific DNA sequence.


1 Answers

Polymorphism is one of the tenets of Object Oriented Programming (OOP). It is the practice of designing objects to share behaviors and to be able to override shared behaviors with specific ones. Polymorphism takes advantage of inheritance in order to make this happen.

In OOP everything is considered to be modeled as an object. This abstraction can be taken all the way down to nuts and bolts for a car, or as broad as simply a car type with a year, make, and model.

To have a polymorphic car scenario there would be the base car type, and then there would subclasses which would inherit from car and provide their own behaviors on top of the basic behaviors a car would have. For example, a subclass could be TowTruck which would still have a year make and model, but might also have some extra behaviors and properties which could be as basic as a flag for IsTowing to as complicated as the specifics of the lift.

Getting back to the example of people and employees, all employees are people, but all people are not employees. Which is to say that people will be the super class, and employee the sub class. People may have ages and weights, but they do not have salaries. Employees are people so they will inherently have an age and weight, but also because they are employees they will have a salary.

So in order to facilitate this, we will first write out the super class (Person)

function Person(age,weight){  this.age = age;  this.weight = weight; } 

And we will give Person the ability to share their information

Person.prototype.getInfo = function(){  return "I am " + this.age + " years old " +     "and weighs " + this.weight +" kilo."; }; 

Next we wish to have a subclass of Person, Employee

function Employee(age,weight,salary){  this.age = age;  this.weight = weight;  this.salary = salary; } Employee.prototype = new Person(); 

And we will override the behavior of getInfo by defining one which is more fitting to an Employee

Employee.prototype.getInfo = function(){  return "I am " + this.age + " years old " +     "and weighs " + this.weight +" kilo " +     "and earns " + this.salary + " dollar.";   }; 

These can be used similar to your original code use

var person = new Person(50,90); var employee = new Employee(43,80,50000);  console.log(person.getInfo()); console.log(employee.getInfo()); 

However, there isn't much gained using inheritance here as Employee's constructor is so similar to person's, and the only function in the prototype is being overridden. The power in polymorphic design is to share behaviors.

like image 89
Travis J Avatar answered Sep 23 '22 21:09

Travis J