Object Oriented JavaScript: Polymorphism with examples

Table of contents
Reading Time: 2 minutes

polymorphismAgain this is not the advance topic of JavaScript but it relies under Object Oriented JavaScript & polymorphism is one of the tenets of Object Oriented Programming (OOP), we all know what is Polymorphism from other languages (like C#, Java etc) but we always think when to use it, why to use it and how to this, and most of us are still in confusion that should we really use it? blah !!!.

JavaScript is dynamically typed language [a big issue to discuss], but for understanding purpose:

Statically typed programming languages do type checking (the process of verifying and enforcing the constraints of types) at compile-time as opposed to run-time. (Java, C etc)

Dynamically typed programming languages do type checking at run-time as opposed to Compile-time. (JavaScript etc)

Though in JavaScript it is a bit more difficult to see the effects of polymorphism because the more classical types of polymorphism are more evident in statically-typed systems.

Why: Polymorphism foster many good attributes in software, among other things it fosters modularity & reuse-ability & make the type system more flexible & elastic.

Poly= many, Morphism=form

When: It is used when we want a function’s (let say function HOLA) interface to be flexible enough to accept different types or number of parameters. Also, based on changing parameters types or numbers, we might want the function HOLA to behave differently (morphism).

How: use case- Take the old example of Person & Employee, where 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.

Polymorphism takes advantage of inheritance in order to make this happen.

Lets jump into code samples: take the basic shapes things:

	var shape = function (){};
	shape.prototype.draw = function(){
		return "i am generic shape";
	}
	//circle
	var circle = function(){}
	circle.prototype = Object.create(shape.prototype);
	circle.prototype.draw= function(){
		return "i am a circle";
	}
	//triangle
	var triangle = function (){}
	triangle.prototype = Object.create(shape.prototype);
	triangle.prototype.draw= function(size){
		return "this is triangle";
	}
	//printing shapes
	var shapes = [new shape(), new circle(), new triangle(23)];
	shapes.forEach (function (shapeList){
		console.log(shapeList.draw());
	});

Stay tuned….


KNOLDUS-advt-sticker

Written by 

Rachel Jones is a Solutions Lead at Knoldus Inc. having more than 22 years of experience. Rachel likes to delve deeper into the field of AI(Artificial Intelligence) and deep learning. She loves challenges and motivating people, also loves to read novels by Dan Brown. Rachel has problem solving, management and leadership skills moreover, she is familiar with programming languages such as Java, Scala, C++ & Html.

3 thoughts on “Object Oriented JavaScript: Polymorphism with examples2 min read

Comments are closed.

Discover more from Knoldus Blogs

Subscribe now to keep reading and get access to the full archive.

Continue reading