constructor.prototype.key = value
Adding methods or properties to a constructor function's prototype makes them accessible to all instances created by that constructor.
Key concepts
constructor.prototype
Adding methods to the constructor's prototype makes them available to all instances. Methods added to the prototype are shared among all instances of the object.
Step by step
Key Insight:
- Methods added to the prototype are shared among all instances of the object.
Examples
Adding Methods to Prototype:
function Person(name) {
this.name = name;
}
Person.prototype.greet = function () {
console.log(`Hello, my name is ${this.name}`);
};
const person1 = new Person('Alice');
person1.greet(); // Hello, my name is AliceStuck on this one? Talk to Sumit.
Keys: F focus mode · ← → previous / next topic · Esc leave focus