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

Quickly install MySQL5.7 compressed package on Windows

This article shares with you how to install the M...

Simple tips to increase web page loading speed

The loading speed of a web page is an important in...

Solve the problem of combining AND and OR in MySQL

As shown below: SELECT prod_name,prod_price FROM ...

Learn asynchronous programming in nodejs in one article

Table of Contents Introduction Synchronous Asynch...

Summary of examples of common methods of JavaScript arrays

Table of contents Common array methods concat() M...

Analysis of several situations where MySQL index fails

1. Best left prefix principle - If multiple colum...

Detailed process of installing and configuring MySQL and Navicat prenium

Prerequisite: Mac, zsh installed, mysql downloade...

Implementation of webpack code fragmentation

Table of contents background CommonsChunkPlugin s...

How to install MySql in CentOS 8 and allow remote connections

Download and install. First check whether there i...

Example of how to change the domestic source in Ubuntu 18.04

Ubuntu's own source is from China, so the dow...

A brief discussion on the magical slash in nginx reverse proxy

When configuring nginx reverse proxy, the slashes...

How to copy MySQL table

Table of contents 1.mysqldump Execution process: ...

Detailed tutorial on MySql installation and uninstallation

This article shares the tutorial of MySql install...