prototype
The `prototype` is an object associated with every function and object in JavaScript. It is used to share methods and properties among instances of an object, reducing memory usage.
Key concepts
prototype
An object associated with every function and object in JavaScript. Used to share methods and properties among instances of an object, reducing memory usage.
Step by step
Why Use prototype?
- Methods defined on the prototype are shared across all instances, reducing memory overhead.
Examples
Using prototype:
function Person(name, age) {
this.name = name;
this.age = age;
}
Person.prototype.greet = function () {
console.log(`Hello, my name is ${this.name}`);
};
const person1 = new Person('Alice', 25);
person1.greet(); // Hello, my name is AliceRead more
Stuck on this one? Talk to Sumit.
Keys: F focus mode · ← → previous / next topic · Esc leave focus