[Reprint]js in __proto__ and prototype differences and relations

Source: Internet
Author: User

Tag: It's LAN CTI prototype share GES technical pointer foo


First, make a few points clear:
1. In JS, everything is the object. A method (Function) is an object, and the prototype (Function.prototype) of a method is an object. Therefore, they all have characteristics that are common to objects.
That is, an object has an attribute __proto__, which can be called an implicit prototype, and an implicit prototype of an object points to the prototype of the constructor that constructs the object, which ensures that the instance has access to the properties and methods defined in the constructor prototype.

2. Method (Function)
Method This special object, in addition to the same _proto_ as other objects, has its own unique properties-the prototype attribute (prototype), the property is a pointer to an object, The purpose of this object is to include properties and methods shared by all instances (we call this object a prototype object). The prototype object also has a property called constructor, which contains a pointer to the original constructor.

Well, knowing the two basic points, let's take a look at the picture above.
1. Constructor Foo ()
The prototype property of the constructor Foo.prototype points to the prototype object, there are common methods in the prototype object, and all instances of the constructor declaration (this is F1,F2) can share this method.

2. Prototype Object Foo.prototype
Foo.prototype holds the method of instance sharing, with a pointer constructor the constructor.

3. Example
F1 and F2 are two instances of Foo, both of which have property __proto__, a prototype object that points to the constructor, which can be like all the methods of accessing the prototype object as mentioned in 1 above.

Other than that:
Constructor Foo () Besides being a method and an object, it also has the __proto__ attribute, which point to whom?
The prototype object that points to its constructor, chant. The constructor of a function is not just functions, so the __proto__ here point to Function.prototype.
In fact, in addition to Foo (), Function (), Object () is the same reason.

The prototype object is also the object Ah, its __proto__ property, and to whom?
Similarly, the prototype object that points to its constructor is Bai. This is object.prototype.

Finally, the __proto__ property of Object.prototype points to null.


Summarize:
1. The object has an attribute __proto__, a prototype object that points to the constructor of the object.
2. In addition to the attribute __proto__, there is a property prototype,prototype the prototype object that points to the method.

On the sauce, welcome all kinds of criticism to improve the discussion, common progress ~ original link: https://www.zhihu.com/question/34183746/answer/58155878

[Reprint]js in __proto__ and prototype differences and relations

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.