The Prototype Design Pattern in JavaScript is a creational pattern that allows you to create new objects by cloning an existing object (the "prototype") instead of creating them from scratch, which serves as a prototype. This pattern is particularly well-suited to JavaScript, as JavaScript itself is a prototype-based language and is useful when object creation is costly or when you want to create an object that shares certain properties with a base prototype.
ES6 classes make it easier to understand the structure of objects. Using extends simplifies inheritance but before ES6, prototype was used to inherit and implement this. Here is blog to understand this concept.
Ps : In the Prototype Design Pattern in JavaScript, there is no built-in clone() method like there is in some other languages (e.g., Java). However, you can manually implement a clone() method in your prototype or constructor function to provide similar functionality.
The idea behind the Prototype pattern is to:
Prototype: A template object from which new objects are created.
Cloning: New objects are created by copying the prototype.
Prototype Chain: Objects can delegate behaviour to their prototype
class Shape { constructor(type = 'Generic Shape', color = 'White') { this.type = type; this.color = color; } getDetails() { return `Type: ${this.type}, Color: ${this.color}`; } // Clone method to create a new object with the same prototype clone() { return Object.create(this); // Creates a new object that inherits from this prototype } }
Explanation:
Purpose: The Shape class acts as a base class or "prototype" from which specific shapes like circles or rectangles can inherit.
clone() Method: This is a key part of the Prototype Pattern. Instead of creating a new instance of a class from scratch, we create a clone of the existing instance (or "prototype"). In this case, Object.create(this) creates a new object that shares the same prototype as the original Shape.
class Circle extends Shape { constructor(radius = 0, color = 'White') { super('Circle', color); // Calls the parent (Shape) constructor this.radius = radius; } getArea() { return Math.PI * this.radius * this.radius; } }
Explanation:
Purpose: The Circle class extends the Shape class and represents a specific type of shape.
Attributes:
getArea() Method: This calculates the area of the circle using the formula π * radius^2.
class Rectangle extends Shape { constructor(width = 0, height = 0, color = 'White') { super('Rectangle', color); // Calls the parent (Shape) constructor this.width = width; this.height = height; } getArea() { return this.width * this.height; } }
Explanation:
Purpose: The Rectangle class extends the Shape class and represents another specific type of shape (a rectangle).
const circlePrototype = new Circle(); // Create prototype const myCircle = circlePrototype.clone(); // Clone the prototype myCircle.radius = 5; myCircle.color = 'Red'; const rectanglePrototype = new Rectangle(); // Create prototype const myRectangle = rectanglePrototype.clone(); // Clone the prototype myRectangle.width = 10; myRectangle.height = 5; myRectangle.color = 'Blue';
Explanation:
Here, instead of creating myCircle and myRectangle objects from scratch using the new keyword, we are cloning prototypes.
Cloning Process:
Modifying Cloned Instances:
console.log(myCircle.getDetails()); // Output: Type: Circle, Color: Red console.log(`Circle Area: ${myCircle.getArea()}`); // Output: Circle Area: 78.54 console.log(myRectangle.getDetails()); // Output: Type: Rectangle, Color: Blue console.log(`Rectangle Area: ${myRectangle.getArea()}`); // Output: Rectangle Area: 50
Explanation:
The Prototype pattern is useful when:
If you've made it this far ?✨, drop a comment below with any questions or thoughts. I'd love to hear from you and engage in some great discussions!✨
Disclaimer: All resources provided are partly from the Internet. If there is any infringement of your copyright or other rights and interests, please explain the detailed reasons and provide proof of copyright or rights and interests and then send it to the email: [email protected] We will handle it for you as soon as possible.
Copyright© 2022 湘ICP备2022001581号-3