A brief discussion on JS prototype and prototype chain

A brief discussion on JS prototype and prototype chain

1. Prototype

All functions in JavaScript have this property, and all objects with a prototype property are functions. The purpose of prototype is to add a method/property to an object.

function persistence(){}
persion.prototype.name = "xiaoming"
console.log(persion.prototype)//{name: "xiaoming", constructor: ƒ}

2. Prototype pointer: __proto__

If the above persion function generates an instance object Persion1, and uses prototype to add an attribute to it, the writing is as follows:

function persistence(){}
persion.prototype.name = "xiaoming"
let Persion1 = new Persion();
console.log(Persion1) //The console results are as follows

The results of printing instance Persion1 are as follows:

From the results printed above, Persion1.__proto__.name = persion.prototype.name, that is, the __proto__ attribute of the instance object is equal to the prototype of its constructor.

After understanding the above, the prototype chain is easy to understand. We can directly find the Object method through Persion1.__proto__.__proto__. This may not be very intuitive, here is the code:

function persistence(){}
persion.prototype.name = "xiaoming"
let Persion1 = new Persion();
console.log(Persion1.__proto__.__proto__.toString) //The toString method of Object found through the prototype chain console.log(Object.prototype.toString) //The toString method on Object

The console prints the following results, which confirms the level-by-level search feature of the prototype chain.

Summarize

Any object can be searched level by level through the prototype chain, that is, the __proto__ attribute. The final focus is Object, and the only way is function. Their relationship is like a chain, and we call this relationship a prototype chain.

The above is a brief discussion of the details of JS prototype and prototype chain. For more information about JS prototype and prototype chain, please pay attention to other related articles on 123WORDPRESS.COM!

You may also be interested in:
  • Detailed explanation of JavaScript prototype chain
  • JavaScript prototype and prototype chain details
  • Thoroughly understand JavaScript prototype and prototype chain
  • Understanding JavaScript prototype chain
  • JS Difficulties Synchronous and Asynchronous and Scope and Closure and Detailed Explanation of Prototype and Prototype Chain
  • Comprehensive analysis of prototypes, prototype objects, and prototype chains in js
  • Detailed explanation of prototypes and prototype chains in JavaScript

<<:  In-depth analysis of MySQL execution plans

>>:  VMware15 installation of CentOS7 detailed process and common problems (picture and text)

Recommend

Ubuntu opens port 22

Scenario You need to use the xshell tool to conne...

Automatically install the Linux system based on cobbler

1. Install components yum install epel-rpm-macros...

Some suggestions for Linux system optimization (kernel optimization)

Disable swap If the server is running a database ...

Detailed explanation of the usage and difference between nohup and & in Linux

Example: We use the Python code loop_hello.py as ...

TypeScript generic parameter default types and new strict compilation option

Table of contents Overview Create a type definiti...

Detailed explanation of how to install MySQL on Alibaba Cloud

As a lightweight open source database, MySQL is w...

Analysis of the Linux input subsystem framework principle

Input subsystem framework The linux input subsyst...

CentOS method to modify the default ssh port number example

The default ssh port number of Linux servers is g...

How to prompt and open hyperlink a

<br />The countless information on the Inter...

Detailed explanation of the process of installing msf on Linux system

Or write down the installation process yourself! ...

How to submit the value of a disabled form field in a form Example code

If a form field in a form is set to disabled, the ...