In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-01-21 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/01 Report--
In this article Xiaobian for you to introduce in detail "JavaScript prototype, prototype chain and constructor attribute instance analysis", the content is detailed, the steps are clear, and the details are handled properly. I hope this "JavaScript prototype, prototype chain and constructor attribute instance analysis" article can help you solve your doubts.
Prototype prototype object
Prototype translates as "prototype", which is a model that shows the appearance and behavior of an application or product early in the development life cycle. The prototype in Javascript is called a prototype object and is a property that every function has.
Print Array.prototype in the console to get the following output. It is found that its prototype contains all the functions of our array objects, and it can be inferred that after the object is created using the constructor, the object will "reference" the prototype of the constructor, so that it can access these functions. In fact, when using an object to access a function, if the object does not define the function, it will look up the prototype chain, layer by layer, looking for the object's proto (hereinafter referred to as the following), then find the object's proto's proto, and then find the object's proto's proto's proto until the function is found and executed or not found at the top of the prototype chain (undefined error reported).
At: optional at ()
Concat: optional concat ()
Constructor: optional Array ()
CopyWithin: optional copyWithin ()
Entries: optional entries ()
Every: optional every ()
Fill: optional fill ()
Filter: optional filter ()
Find: optional find ()
/ / … Omit
[[Prototype]]: Object
Function A () {} / / defines a function A
one
In Javascript, a function is also an object, and you can verify that the result of An instanceof Object or Array instanceof Object or String instanceof Object is true. Take a look at it visually, print typeof A.prototype, and get the following results. It can be seen that the prototype of A.prototype is an object.
Prototype chain
Function: there is a prototype attribute, and there is another constructor attribute in the prototype attribute. Constructor points to the function itself.
Object: there is a _ _ proto__ attribute that points to the prototype of its constructor. (a function that new out an object, which is called a constructor)
Combined with an example, to illustrate clearly.
Function A () {
This.text ='I am A.'
}
Const a = new A ()
Console.log (A. protoplast _ = = A.prototype); / / true
Console.log (A. protozoa. Protozoa) _ = = Object.prototype); / / true
Console.log (Object.prototype.__proto__); / / null
Console.log (A. protozoa. Protozoa. Protozoa _); / / null
An is the object constructed by function A, A. prototype prototype _ points to its constructor prototype object A.prototype, and A. prototype. Primitive prototype _ object points to its prototype object Object.prototype,Object.prototype.__proto__ points to null.
The following is an excerpt from the MDN document:
Each instance object has a private property (called _ _ proto__) that points to the prototype object (prototype) of its constructor. The prototype object also has its own prototype object (_ _ proto__), layer upon layer until the prototype object of an object is null. By definition, null does not have a prototype and serves as the last link in the prototype chain.
To see more clearly what the prototype object and prototype chain look like, print A.prototype and Object.prototype on the console, and the output is as follows:
[[Prototype]] is only shown in the console, but it is actually _ _ proto__. It can be seen that A.prototype.protoplast _ points to Object.prototype, that is, a.protoprotozoa _ points to Object.prototype. A prototype object points to a prototype object in the upper layer, and the prototype object in the upper layer points to a chain that points to the upper layer all the way up to null, which is the prototype chain.
Constructor attribute
Constructor is an attribute of prototype, and constructor points to the constructor itself.
A.prototype.constructor = = A / / true
('some text'). Constructor = String / / true
([1]). Constructor = Array / / true
After reading this, the article "JavaScript prototype, prototype chain and constructor attribute instance Analysis" has been introduced. If you want to master the knowledge points of this article, you still need to practice and use it yourself to understand it. If you want to know more about related articles, 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.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.