Interpretation of object creation and inheritance examples in JavaScript

Source: Internet
Author: User

Object creation:

When a Function object is created, the Function object generated by the Function constructor runs code similar to this:
Copy codeThe Code is as follows:
This. prototype = {constructor: this };

Assume that function F
F when constructing an object using the new method, the constructor of the object is set to this F. prototype. constructor
If the function modifies the prototype of the function before creating an object, the construtor attribute of the created object will be affected.

For example:
Copy codeThe Code is as follows:
Function F (){};
F. prototype = {constructor: '000000 '};
Var o = new F (); // o. constructor = '000000' true

Inheritance principle:

Inheritance in JavaScript uses the prototype chain mechanism. Each function instance shares the data defined in the prototype attribute of the constructor. To make one class inherit from the other, assign a value to the parent function instance to the prototype attribute of the sub-function. When a new instance object is added, the object's private property _ proto _ is automatically connected to the prototype of the constructor.

Instanceof is used to find the private prototype attribute chain of the Instance Object to determine whether it is an instance of the specified object.

Specific instance:
Copy codeThe Code is as follows:
// Instanceof implementation
Function Myinstanceof (obj, type)
{
Var proto = obj. _ proto __;
While (proto)
{
If (proto = type. prototype) break;
Proto = proto. _ proto __;
}
Return proto! = Null;
}


Function View (){}
Function TreeView (){}
TreeView. prototype = new View (); // TreeView. prototype. _ proto __= TreeView. prototype is automatically completed.
TreeView. prototype. constructor = TreeView; // corrected constructor
Var view = new TreeView (); // view. _ proto __= TreeView. prototype is automatically completed
Alert (view instanceof View); // It is found when view. _ proto _. _ proto _ is found in true.
Alert (view instanceof TreeView); // true when view. _ proto _ is found
Alert (Myinstanceof (view, View); // true
Alert (Myinstanceof (view, TreeView); // true

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.