Home >Common Problem >What are prototypes and prototype chains

What are prototypes and prototype chains

百草
百草Original
2023-11-09 17:59:482221browse

Prototype, an object in js, used to define the properties and methods of other objects. Each constructor has a prototype attribute. This attribute is a pointer pointing to a prototype object when creating a new object. , this new object will inherit properties and methods from the prototype attribute of its constructor. Prototype chain, when trying to access the properties of an object, js will first check whether the object has this property. If not, then js will turn to the prototype of the object. If the prototype object does not have this property, it will continue to look for the prototype of the prototype.

What are prototypes and prototype chains

The operating system for this tutorial: Windows 10 system, DELL G3 computer.

Prototype and prototype chain are an important concept in object-oriented programming, especially in languages ​​like JavaScript that support dynamic prototypes. Understanding prototypes and prototype chains is critical to understanding how objects are created and used, and how to call their methods.

Prototype:

A prototype is an object in JavaScript that is used to define the properties and methods of other objects. Each constructor has a prototype attribute, which is a pointer to a prototype object. When a new object is created, the new object inherits properties and methods from the prototype attribute of its constructor (that is, the prototype object).

For example:

function Person(name) {  
  this.name = name;  
}  
  
Person.prototype.sayHello = function() {  
  return 'Hello, my name is ' + this.name;  
};  
  
let person1 = new Person('Alice');  
let person2 = new Person('Bob');  
  
console.log(person1.sayHello()); // 输出: 'Hello, my name is Alice'  
console.log(person2.sayHello()); // 输出: 'Hello, my name is Bob'

In the above example, we defined a Person constructor and defined a sayHello method through Person.prototype. Then we created two Person objects, both of which inherit the sayHello method. This is because they all inherit from the prototype object Person.prototype.

Prototype chain:

When trying to access a property of an object, JavaScript will first check whether the object has this property. If not, then JavaScript will turn to the prototype of the object (that is, the prototype attribute of the constructor). If the prototype object does not have this property, then JavaScript will continue to look for the prototype's prototype, and so on, until it finds an object with this property, or reaches the top of the prototype chain (that is, null). This is called the prototype chain.

For example:

function Person(name) {  
  this.name = name;  
}  
  
Person.prototype.sayHello = function() {  
  return 'Hello, my name is ' + this.name;  
};  
  
let person1 = new Person('Alice');  
delete person1.sayHello; // 删除person1的sayHello属性  
  
console.log(person1.sayHello()); // 输出: 'Hello, my name is Alice'

In the above example, we deleted the sayHello attribute of person1. When we try to access person1.sayHello(), JavaScript first checks whether person1 has a sayHello attribute. Since person1 does not have this attribute, JavaScript will turn to person1's prototype (i.e. Person.prototype), which has the sayHello method, so it is called. This is what the prototype chain does: it provides a way to share an object's properties and methods, even if those properties and methods are not defined on the object itself.

The above is the detailed content of What are prototypes and prototype chains. For more information, please follow other related articles on the PHP Chinese website!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn