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 Tools

SecLists

SecLists

SecLists is the ultimate security tester's companion. It is a collection of various types of lists that are frequently used during security assessments, all in one place. SecLists helps make security testing more efficient and productive by conveniently providing all the lists a security tester might need. List types include usernames, passwords, URLs, fuzzing payloads, sensitive data patterns, web shells, and more. The tester can simply pull this repository onto a new test machine and he will have access to every type of list he needs.

WebStorm Mac version

WebStorm Mac version

Useful JavaScript development tools

Atom editor mac version download

Atom editor mac version download

The most popular open source editor

EditPlus Chinese cracked version

EditPlus Chinese cracked version

Small size, syntax highlighting, does not support code prompt function

DVWA

DVWA

Damn Vulnerable Web App (DVWA) is a PHP/MySQL web application that is very vulnerable. Its main goals are to be an aid for security professionals to test their skills and tools in a legal environment, to help web developers better understand the process of securing web applications, and to help teachers/students teach/learn in a classroom environment Web application security. The goal of DVWA is to practice some of the most common web vulnerabilities through a simple and straightforward interface, with varying degrees of difficulty. Please note that this software