Skip to content

Latest commit

 

History

History
48 lines (36 loc) · 2.35 KB

53-Prototype.md

File metadata and controls

48 lines (36 loc) · 2.35 KB

Prototypes in JavaScript

In JavaScript, every object has a prototype object, which acts as a template for the object. When a property or method is accessed on an object, JavaScript first looks for that property or method on the object itself. If it's not found, JavaScript looks for it on the object's prototype. If it's still not found, JavaScript continues up the prototype chain until it reaches the Object.prototype, which is the root of the prototype chain.

To demonstrate how prototypes work in JavaScript, let's create an object and a constructor function, and add some properties and methods to their prototypes.

// create an object
const person = {
  name: 'Alice',
  age: 30,
  gender: 'female'
};

// add a method to the object's prototype
person.walk = function() {
  console.log(`${this.name} is walking.`);
};

// create a constructor function
function Car(make, model, year) {
  this.make = make;
  this.model = model;
  this.year = year;
}

// add a method to the constructor function's prototype
Car.prototype.start = function() {
  console.log(`Starting the ${this.make} ${this.model}.`);
};

// create an instance of the Car object
const car1 = new Car('Toyota', 'Corolla', 2020);

// access properties and methods on the object and its prototype
console.log(person.name); // logs "Alice"
person.walk(); // logs "Alice is walking."
console.log(car1.make); // logs "Toyota"
car1.start(); // logs "Starting the Toyota Corolla."

In this example, we create an object called "person" with properties for the person's name, age, and gender. We then add a walk() method to the person object's prototype using dot notation.

Next, we create a constructor function called "Car" that takes in arguments for the car's make, model, and year, and sets those properties on the newly created object using this. We then add a start() method to the Car.prototype object using dot notation.

Finally, we create an instance of the "Car" object called "car1". We can access properties and methods on the object and its prototype using dot notation.

Note that adding methods to an object's prototype instead of the object itself can be more memory-efficient, as the method is shared by all instances of the object rather than being duplicated for each instance. It can also make the code more readable and easier to maintain, as all the methods are grouped together in one place.