Altcademy - a Forbes magazine logo Best Coding Bootcamp 2023

What is polymorphism in JavaScript

Understanding Polymorphism

Polymorphism is one of the four fundamental principles of Object-Oriented Programming (OOP). The term originates from Greek, where 'poly' means 'many' and 'morph' means 'forms'. So, polymorphism translates to 'many forms'. The principle allows objects to take on many forms depending on the context. In programming, it refers to the ability of different data types to be processed by a single function or method.

In JavaScript, polymorphism is achieved through prototype inheritance and method overloading. Let's break these big terms down.

Prototype Inheritance

Prototypes in JavaScript are the mechanism by which JavaScript objects inherit features from one another. This concept might sound complex, but think of it like a family tree. Your grandparents' features are passed down to your parents, and then to you. Similarly, in JavaScript objects can inherit features (properties and methods) from other objects.

Let's take a look at an example. Suppose we have a Shape object, which we will consider as our parent object.

function Shape(name) {
  this.name = name;
}

Shape.prototype.describe = function() {
  return `I am a ${this.name}`;
}

Now, let's create another object Circle that inherits from Shape.

function Circle(radius) {
  Shape.call(this, "circle");
  this.radius = radius;
}

Circle.prototype = Object.create(Shape.prototype);
Circle.prototype.constructor = Circle;

Here, Circle is inheriting from Shape through Object.create(). Now, the Circle object has access to the describe method of Shape.

let myCircle = new Circle(5);
console.log(myCircle.describe()); // Outputs: I am a circle

So, this is how polymorphism works through prototype inheritance in JavaScript.

Method Overloading

Method overloading in JavaScript is a concept where more than one method can have the same name with different parameters. It's like having a multi-purpose tool - the same tool can perform different tasks depending on what you need it to do.

However, JavaScript doesn't support method overloading directly like other programming languages. But we can simulate the functionality by checking the number of arguments or their types.

Let's see an example with a Rectangle object.

function Rectangle(length, breadth) {
  this.length = length;
  this.breadth = breadth;

  // Check the number of arguments
  if (arguments.length == 1) {
    this.breadth = length; // If only one argument, it's a square
  }
}

Rectangle.prototype.area = function() {
  return this.length * this.breadth;
}

let myRectangle = new Rectangle(10, 5);
console.log(myRectangle.area()); // Outputs: 50

let mySquare = new Rectangle(5);
console.log(mySquare.area()); // Outputs: 25

Here, the Rectangle constructor checks the number of arguments. If there's only one argument, it assumes it's a square and sets both length and breadth to the same value. This is a simple example of how we can replicate method overloading in JavaScript.

Wrapping Up

Polymorphism in JavaScript can be likened to water. Just as water can take the form of a solid, liquid or gas depending on the conditions, so too can objects in JavaScript take on different forms depending on the context. They can inherit properties and methods from parent objects, and even perform different tasks based on the number of arguments or their types.

Understanding polymorphism is essential in writing flexible and reusable code. It allows us to write functions or methods that can handle different data types and perform various tasks, making our code more versatile and efficient.

As you continue your journey in JavaScript and programming in general, you'll discover more about the beauty and flexibility that these principles provide. And remember, just like water, let your understanding and use of these principles adapt to the coding challenges that you face. Happy coding!