Skip to main content
FREE Mock interview & resume review with Sumit. Free right now, no payment needed.Book a slot

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 Alice

Additional Resources

Interview guide discussion

(0)

Join the discussion — login to comment and help other developers.

Login to comment