search
HomeCommon ProblemWhat are prototypes and prototype chains

What are prototypes and prototype chains

Nov 09, 2023 pm 05:59 PM
prototype chainprototype

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

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

Video Face Swap

Video Face Swap

Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Article

Hot Tools

SublimeText3 English version

SublimeText3 English version

Recommended: Win version, supports code prompts!

MinGW - Minimalist GNU for Windows

MinGW - Minimalist GNU for Windows

This project is in the process of being migrated to osdn.net/projects/mingw, you can continue to follow us there. MinGW: A native Windows port of the GNU Compiler Collection (GCC), freely distributable import libraries and header files for building native Windows applications; includes extensions to the MSVC runtime to support C99 functionality. All MinGW software can run on 64-bit Windows platforms.

ZendStudio 13.5.1 Mac

ZendStudio 13.5.1 Mac

Powerful PHP integrated development environment

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor