A detailed summary of prototype and _proto_ in JS Zhongyuan chain (code example + HD uncensored Big picture!) --self-prepared melon Seeds Peanuts)

Source: Internet
Author: User
Tags hasownproperty

Always think that the prototype chain is too complex, especially to see a picture after being around the halo of a whole son, today to clean up the hard disk space (slag computer), and occasionally see this figure, hook up a bit of memory, so simply review the prototype chain related content, lack of logical confusion of expression ability (in order to prevent the new __ (here) __ The picture is put at the End. )

Here are three points to keep in mind

1. Each object has a property named __proto__;

2. Each constructor (the constructor standard starts with uppercase, such as function (), Object (), and so on, the constructor that comes with js, and what you Create) has a method called prototype (note: since it is a method, So is an object (js function is also the object), so prototype also with the __proto__ attribute);

3. The __proto__ property of each object points to the prototype of its own constructor;

The idea expands as follows

1             functionfun () {2             }3 //I created a function, fn.4 //This function is generated by functions (function as a constructor)5             varfn=Newfun ()6 //I created a function, fn7 //This function is generated by FN (FN as a constructor)8             9             TenConsole.log (fn.__proto__===fun.prototype)//true one //the __proto__ of FN points to its constructor fun prototype aConsole.log (fun.__proto__===function.prototype)//true - //fun __proto__ points to the prototype of its constructor function -Console.log (function.__proto__===function.prototype)//true the //The __proto__ of a function points to the prototype of its constructor function - //the constructor itself is a function, and he is constructed by Itself. -Console.log (function.prototype.__proto__===object.prototype)//true - //the __proto__ of Function.prototype points to the prototype of its constructor object + //Function.prototype is an object, which is also a method, and the method is a function, so it must have its own constructor, which is Object -Console.log (fun.prototype.__proto__===object.prototype)//true + //Same as previous bar a //one thing to know here is that all constructors ' prototype methods point to __object.prototype (except for ...). Object.prototype Itself) atConsole.log (object.__proto__===function.prototype)//true - //object as a constructor (is a function object!!) function object!), so his __proto__ points to Function.prototype -Console.log (object.prototype.__proto__===NULL)//true - //Object.prototype as the source of all, his __proto__ is null -  - //Here's A new, extra example in  -             varobj={} to //created an obj +Console.log (obj.__proto__===object.prototype)//true - //obj is an object created directly as a literal, so obj__proto__ points directly to the object.prototype without having to go through the function!!  the  * //The following is what extends from the prototype chain $ //There is also a constructor that is not mentioned above, constructor in the prototype chain, as an object PROTOTYPR the existence of an attribute, it points to the constructor (because the main prototype chain, this is not concerned about,);Panax Notoginseng  -Console.log (obj.__proto__.__proto__===NULL)//true theConsole.log (obj.__proto__.constructor===object)//true +Console.log (obj.__proto__.constructor.__proto__===function.prototype)//true aConsole.log (obj.__proto__.constructor.__proto__.__proto__===object.prototype)//true theConsole.log (obj.__proto__.constructor.__proto__.__proto__.__proto__===NULL)//true +Console.log (obj.__proto__.constructor.__proto__.__proto__.constructor.__proto__===function.prototype)//true -              $              $ //above, interested can be verified F12.
         

In order to facilitate the memory can be drawn as a conclusion (if there is a mistake welcome treatise ... )

Prototype is a property unique to a constructor;

The __prototype__ property of an object usually corresponds to the prototype property of its constructor;

The prototype method of all constructors points to __object.prototype (except for ...). Object.prototype itself);

The point to note is

The __proto__ of function points to the prototype of its constructor function;

Object as a constructor (is a function object!!) function object!), so his __proto__ points to function.prototype;

The __proto__ of Function.prototype points to the prototype of its constructor object;

The __prototype__ of object.prototype points to null (end);

In the article structure by the way with two methods related to the prototype chain .... Welcome to use

1.

hasOwnProperty a property or object that determines whether an object has a name, This method cannot check whether the object has that property in its prototype chain, and the property must be a member of the object itself. Returns true if the property or method is defined in the Object's own definition rather than in the prototype chain; The format is as Follows: Object.hasownproperty (proname);
Quotation marks must be enclosed in parentheses and written directly to the property name 2. isPrototypeOf is used to determine whether the specified object Object1 exists in the prototype chain of another object object2, or returns true, otherwise False. The format is as Follows: object1.isprototypeof (object2); Object1 is an instance of an object; Object2 is another object that will examine its prototype Chain. A prototype chain can be used to share functionality between different instances of the same object Type. If Object2 's prototype chain contains object1, then the isPrototypeOf method returns True. If Object2 is not an object or Object1 does not appear in the prototype chain in object2, the isPrototypeOf method returns False.

A figure here ps: this article is summed up, plus personal understanding ..... The picture is a long time ago in the computer .... Forget where you saw it ...

A detailed summary of prototype and _proto_ in JS Zhongyuan chain (code example + HD uncensored Big picture!) --self-prepared melon Seeds Peanuts)

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.