Clarify the relationship between prototype, __proto__, Object, and function in JavaScript to better understand prototype inheritance

Source: Internet
Author: User

This article , referring to Http://www.blogjava.net/heavensay/archive/2013/10/20/405440.html's article, makes a brief summary of its contents, and forms a few simple conclusions. It makes it easier for readers to remember the relationship between prototype, __proto__, Object, and function.

Conclusion 1:Object.prototype is just an ordinary object, it is the top of the JS prototype chain.
(typeof object.prototype) = = = object;//trueobject.prototype.__proto__=== Null;//trueobject.prototype.prototype = = = Undefied;//true
Object.prototype is just a normal object (the normal object has no prototype property, so the value is undefined), Object.prototype is the top of the JS prototype chain, its __proto__ is null (there are __ The Proto__ property, but the value is null, because this is the topmost of the prototype chain.
Conclusion 2: in JS If the A object is constructed by the B function, then a.__proto__ = = = B.prototype.
function person () {}var obj = {};alert (obj.__proto__ = = = Object.prototype);//truealert (person.__proto__ = = = Function.prototype);//true
The objects in JavaScript are created by object, and functions are created by function.

Conclusion 3: The built-in object is actually a function object, which is created by functions.
object.__proto__ = = = Function.prototype;

Conclusion 4:each object or function in JS has a __proto__ property, but only the function object has the prototype property.
function object Functions Person () {}//Ordinary object var obj = {};obj.__proto__ = = = = = = = = = = = undefined;// trueperson.__proto__ = = = Function.prototype;//trueperson.prototype!== undefined;//true
we know that JavaScript is inherited through prototype implementations. If Obja, OBJB are created by cfunction, then the conclusion 2,obja.__proto__ = = = objb.__proto__ Cfunction.prototype, which means that both Obja and OBJB objects inherit the prototype of Cfunction. The prototype chain is based on __proto__, and the inheritance is implemented by prototype .
conclusion 5:function.prototype is a special case, it is a function object, but there is no prototype property. All other functions have the prototype property.
(typeof function.prototype) = = = Function;//truefunction.prototype.prototype = = = Undefined;//true

Conclusion 6: The built-in function is also a functional object, which is created by itself.
(typeof function.__proto__) = = = function;//truefunction.__proto__=== Function.prototype;//true
The function object, which is constructed by itself through function functions.

Conclusion 7: The function is also an object because function.__proto__ points to object.prototype.
(typeof function.prototype.__proto__) = = = object;//truefunction.prototype.__proto__=== Object.prototype;//true

finally , the prototype chain is based on __proto__, and the inheritance is implemented by prototype .
Each object has a __proto__ property, and the object on the prototype chain is linked together by this __proto__ property! For an object on the prototype chain, obj, the procedure for accessing the Obj.xxx property (the method is also a property) is to access it if it has a xxx property, or, if not, to find the first-level prototype object of its prototype chain through the __proto__ property, to see if it has a XXX attribute, so recursive lookup, Until you find the XXX attribute or the Object.prototype object at the top of the prototype chain.
The function has the prototype property, which is an object type. When function A creates object B, the __proto__ of the B object points to A.prototype, which is the inheritance of JavaScript. 

Clarify the relationship between prototype, __proto__, Object, and function in JavaScript to better understand prototype inheritance

Related Article

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.