My view of JS prototype chain

Source: Internet
Author: User

The first two days from the Internet to see a topic, the general content is as follows:

var f=function() {}; Function.prototype.a=function() {Console.log ("a");};o bject.prototype.b=function() {Console.log ("B");}; var f=New  f (); f.b (); F.A ();

At first look at this problem, feel very simple, should be output "B" and "a" bar, but actually run, not so, the actual result is: The second function error, did not find a definition. What's going on?

In fact, this topic mainly examines the understanding of the prototype chain. We know that when an object cannot find a property, it will look up in its prototype chain to see if there is a property in the other object, until it is found, if it is not found, then it will be an error like the above topic.

How does the prototype chain build up? In the JS world, everything is object, each object has a private __proto__ attribute, which is used to identify the prototype object above it, so that the entire prototype chain through this property to build up.

And what is the prototype chain in the title? Build the prototype chain from the top of the prototype chain, which is the current object F. We know that the __proto__ property value is actually the prototype property value of the current object constructor, so there is the following equation:

The F.__proto__===f.prototype;//f constructor is the F

F.prototype.__proto__===object.prototype; The F.prototype constructor is an object

Similarly, the summary prototype chain is as follows:

F---->f.prototype---->object.prototype---->nullf---->function.prototype----> Object.prototype---->null

Therefore, it is obvious that F.A does not exist.

My view of JS prototype chain

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.