Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

About the prototype and prototype chain in JS object-oriented and the relationship between them and the detailed explanation of this

2025-02-27 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Network Security >

Share

Shulou(Shulou.com)06/01 Report--

One: prototypes and prototype objects:

1. Function prototype prototype: only the function prototype,prototype is an object that points to the reference address of the current constructor.

two。 The prototype object of the function, _ _ proto__:, all objects have a _ _ proto__ property, and when an object is instantiated (new) with the constructor, the _ _ proto__ property of the new object is pointed to the prototype of the constructor.

1zhangsan.__proto__==Person.prototype

Note: in the above code, Person is the constructor, and zhangsan is an instantiated object of the constructor.

A diagram is used to explain the relationship between the prototype object and the prototype of the function:

From the above picture, we can clearly see the relationship between the function prototype and the prototype object:

Zhangsan is an instantiated object of the constructor, and its _ _ proto__ points to its constructor prototype, namely Person.prototype.

The _ _ proto__ of the constructor Person () points to the prototype of the general function class Function, while Function () itself points to the prototype of Function.

Both Person.prototype and Function.prototype hi point to the prototype of the Object general class, that is, the _ _ proto__ of Object.prototype,Object () points to Function.prototype

The _ _ proto__ of Object.prototype points to null.

To sum up:

The _ _ proto__ of all ① functions is the prototype that points to Function.

The object _ _ proto__ from the ② constructor new points to the prototype of the constructor.

The _ _ proto__ of the object or the prototype of the object instantiated by the ③ non-constructor points to the prototype of Object.

The prototype of ④ Object points to null.

Second: this detailed explanation:

1. Who finally calls the function, this points to.

What ① this points to can only be an object!

Who ② this points to never depends on where the this is written, but on where the function is called

The object that ③ this points to, called the function's context context, also known as the function's caller

The rule that 2.this points to (closely related to the way the function is called):

What this points to, depending on how the function is called

① is called directly through the function name (): this points to window

② passes through the object. Called by function name (): this points to this object

The ③ function is called through an element of the array, through the array subscript, and this points to the array.

When the ④ function is called as a callback function of the window built-in function, this points to window, such as setTimeout setInterval, etc.

When the ⑤ function is called with the new keyword as a constructor, this points to the object out of the new new.

Example:

123function func () {}

① is called directly through the function name (): this points to window.

1func ()

② passes through the object. Called by the function (): this points to this object.

Narrow object:

12345var obj= {name: "obj", func1:func} obj.func1 ()

Generalized objects:

123document.getElementById ("div") .onclick=function () {this.style.backgroundColor= "red";}

The ③ function is called through an element of the array, through the array subscript, and this points to the array.

12var arr= [func,1,2,3]; arr [0] ()

When the ④ function is called as a callback function of the window built-in function, this points to window.

12setTimeout (func,1000); setInterval (func,1000)

When the ⑤ function is called with the new keyword as a constructor, this points to the object out of the new new.

1var obj = new func ()

The above is the detailed explanation of the prototype chain and this.

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Network Security

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report