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

What is the prototype and prototype chain of javascript

2025-01-17 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

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

Editor to share with you what the prototype and prototype chain of javascript is, I believe most people do not know much about it, so share this article for your reference, I hope you can learn a lot after reading this article, let's learn about it!

One, function object

All reference types (functions, arrays, objects) have the _ _ proto__ property (implicit prototype)

All functions have prototype attributes (explicit prototypes) (functions only)

Prototype object: an object with a prototype property that is created when a function is defined

Second, constructor

Review the constructor first.

/ / create constructor function Word (words) {this.words = words;} Word.prototype = {alert () {alert (this.words);}} / / create instance var w = new Word ("hello world") W.print = function () {console.log (this.words); console.log (this); / / Person object} w.print (); / / hello world w.alert (); / / hello world

The print () method is a method that the w instance itself has, so w.print () prints the method that hello world;alert () does not belong to the w instance, it belongs to the method of the constructor, and w.alert () also prints hello world, because the instance inherits the method of the constructor.

The implicit prototype of instance w points to the explicit prototype of its constructor, meaning that it is always equal to

W. pure protoplast _ = Word.prototype

When calling a method or looking for an attribute, it will first call and find it in itself. If it does not have the property or method, it will call lookup in its _ _ proto__ attribute, that is, in the prototype of its constructor. So it's good to understand the methods and properties of the instance inheritance constructor:

W itself does not have an alert () method, so it calls alert () in the explicit prototype of Word (), that is, the method of the instance inheriting the constructor.

Third, prototype and prototype chain Function.prototype.a = "a"; Object.prototype.b = "b"; function Person () {} console.log (Person); / / function Person () let p = new Person (); console.log (p); / / Person {} object console.log (p.a) / / undefined console.log (p.b); / / b

Think about p.a printing result as undefined,p.b and result as b

Parsing:

P is an instance of Person (), a Person object that has an attribute value of _ _ proto__, and _ _ proto__ is an object that contains two attribute values constructor and _ _ proto__

Console.log (p.__proto__.constructor); / / function Person () {} console.log (p.protozoa. Protozoa _); / / object {}, which has many attribute values

We will find that the result returned by p.__proto__.constructor is the constructor itself, and there are a lot of parameters.

We call the constructor property, and p.___proto__.__proto__.constructor gets the Object () function with multiple arguments, and the constructor of the implicit prototype of Person.prototype points to Object (), that is, Person.prototype.__proto__.constructor = = Object ().

The result returned from p.__proto__.constructor is that the constructor itself gets Person.prototype.constructor = = Person (), so p.

So P.B print result is bjournal p does not have b attribute, will always look up through _ _ proto__, finally find when Object.prototype is found, finally print out b, in the process of upward search, get Object.prototype, not Function.prototype, can not find an attribute, so the result is undefined, this is the prototype chain, search up through _ _ proto__, and finally to the end of null

Console.log (P. protozoa. Protozoa. Protozoa _); / / null console.log (Object.prototype.__proto__); / / null

We all understand the process just now, and I believe the following should also be understood.

/ / Function function Function () {} console.log (Function); / / Function () console.log (Function.prototype.constructor); / / Function () console.log (Function.prototype.__proto__); / / Object.prototype console.log (Function.prototype.__proto__.__proto__); / / NULL console.log (Function.prototype.__proto__.constructor) / / Object () console.log (Function.prototype.__proto__ = = Object.prototype); / / true

Summary:

1. Look for a property. If it does not have one, it will look for it in _ _ proto__, that is, in the explicit prototype of the constructor. If it does not have this property in the constructor, because the constructor is also an object and has _ _ proto__, then it will look in its explicit prototype all the way to null, and if it does not, return undefined.

2.p.__proto__.constructor = = function Person () {}

3.p.The protozoa of the eggs. The protozoa of the animals = Object.prototype

4.p.The protozoa of the oysters. The protozoa of the oysters of the oysters = Object.prototype.__proto__ = = null

5. Form a prototype chain instead of protrotype through _ _ proto__

Finally, a picture is attached. After you have read it, it should be easy to understand.

The above is all the content of the article "what is the prototype and prototype chain of javascript". Thank you for reading! I believe we all have a certain understanding, hope to share the content to help you, if you want to learn more knowledge, welcome to follow the industry information channel!

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

Development

Wechat

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

12
Report