When a Function object is created, the Function object generated by the Function constructor runs code similar to this
Object creation:
When a Function object is created, the Function object generated by the Function constructor runs code similar to this:
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:
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:
// 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
The above custom Myinstanceof is a self-implemented instanceof function. Because the prototype stored in the IE kernel instance is not _ proto __, Myinstanceof will fail, and there should be no problem in other browsers.