Exploring Polymorphism in Javascript
Polymorphism, derived from Greek words meaning "many forms," is a fundamental concept in object-oriented programming languages like JavaScript. It allows objects of different types to be treated as objects of a common superclass, enabling flexibility and extensibility in our code.
Understanding Polymorphism :
Polymorphism in JavaScript refers to the ability of different objects to respond to the same method or property in different ways. This means that while objects may share a common interface, they can exhibit different behaviors based on their specific implementations.
Types of Polymorphism in JavaScript :
Ad-hoc polymorphism :
Also known as function overloading, ad-hoc polymorphism allows functions to behave differently based on the number or type of arguments passed to them. In JavaScript, this is commonly achieved through function overloading or using the arguments object.
Example :
Consider a function calculateArea() that calculates the area of different shapes based on the number of arguments provided. If two arguments are passed, it calculates the area of a rectangle. If only one argument is passed, it calculates the area of a circle.
function calculateArea(shape, arg1, arg2) {
if (shape === "rectangle") {
return arg1 * arg2;
} else if (shape === "circle") {
return Math.PI * Math.pow(arg1, 2);
} else {
throw new Error("Unsupported shape");
}
}
console.log(calculateArea("rectangle", 5, 3)); // Outputs: 15 (Area of rectangle)
console.log(calculateArea("circle", 4));
Parametric polymorphism :
This type of polymorphism allows functions or data types to operate uniformly on objects of different types. In JavaScript, parametric polymorphism can be implemented using generics or by leveraging the dynamic typing nature of the language.
Recommended by LinkedIn
Example :
// Generic function to print the type of any value
function printType(value) {
console.log(typeof value);
}
// Usage
printType(5); // Output: number
printType('Hello'); // Output: string
printType(true); // Output: boolean
printType({}); // Output: object
In this example, the printType function takes a single argument value, which can be of any type. It then prints the type of the value using JavaScript's typeof operator. This function operates uniformly on different types of data without needing separate implementations, showcasing parametric polymorphism.
Subtype polymorphism :
Subtype polymorphism, also known as inheritance, allows objects of a derived class to be treated as objects of their base class. In JavaScript, this is achieved through prototypal inheritance, where objects inherit properties and methods from their prototype chain.
// Base class
class Animal {
constructor(name) {
this.name = name;
}
// Method to make the animal sound
makeSound() {
console.log('Animal sound');
}
}
// Derived class Dog
class Dog extends Animal {
// Override makeSound method
makeSound() {
console.log('Woof! Woof!');
}
}
// Derived class Cat
class Cat extends Animal {
// Override makeSound method
makeSound() {
console.log('Meow! Meow!');
}
}
// Usage
const dog = new Dog('Buddy');
const cat = new Cat('Whiskers');
dog.makeSound(); // Output: Woof! Woof!
cat.makeSound(); // Output: Meow! Meow!
In this example :
Polymorphism is a powerful concept in JavaScript that allows for flexibility and extensibility in our code. By understanding and leveraging polymorphism, we can write more modular, reusable, and maintainable software.
Stay tuned for more insights and discussions on JavaScript and other programming topics in our future newsletters.
Happy Coding!