prototype
Topic 4JavaScript Part 4 (OOP)
prototype
Introduction
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 and Definitions
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 Explanation
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 AliceAdditional Resources
Interview guide discussion
(0)Join the discussion — login to comment and help other developers.
Login to comment