Javascript prototype details

Source: Internet
Author: User

Simple DescriptionJSPrototype

PrototypeProvides a mechanism for sharing attributes and methods of similar objects.,The object inherits all attributes and methods of the prototype (Functions). To put it bluntlyJSClass inheritance is implemented using the prototype.

Full understandingPrototype/_ PROTO __

It is difficult for beginners to clearly distinguish between these two attributes. Therefore, a front-end friend said that these two attributes are completely understood.JSThe prototype is almost thorough. Then I will try to elaborate on these two attributes (the content is summarized from others' crystallization andJSDocumentation ).

prototype : prototype is a function attribute, when a function is used as a constructor, if you want a subclass to have a certain type of feature, you can directly operate the parent class (always feel that the parent function is appropriate) prototype . . Therefore, in JS inheritance prototype is an attribute of the parent class constructor. This attribute points to the object created by the constructor (Be sure to note prototype is a function attribute, not an object)

_ PROTO __: During object Creation_ PROTO __Property will point to the function that creates this objectPrototypeAttribute.Eg:VaR A = new function (); A. _ PROTO _ = function. Prototype._ PROTO __Is an attribute owned by all objects, but this attribute is discarded and not recommended for use.

Instance 1:

VaR A = function () {This. name = "A";} var B = {} console. log (. prototype); console. log (B. prototype); // prototype is the attribute of the function, so print undefined here. Console. Log (B. constructor. Prototype); console. Log (A. _ PROTO _); console. Log (B. _ PROTO __);

The result is as follows:

Prototype chain

InJSIf the current activity object does not have this attribute during executionJSThe execution engine will_ PROTO __The attribute is always searched by the upper-levelObject. _ PROTO __If not found, the system returnsUndefined. The previous figure shows how to find it.

JavascriptCreate an object

JavascriptTo create an object, follow these steps:(1), Create a new object;(2), Pointing the object prototype to the constructorPrototypeThe prototype object to which the object is directed;(3), The objectThisParameters call the constructor. As follows:

 
Function OBJ () {}var o ={}; obj. Call (o)

 

 

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.