Object creation:
When a function object is created, the function object produced by the functions constructor runs code similar to this:
Copy Code code as follows:
This.prototype={constructor:this};
Suppose function f
f when constructing an object in new form, the object's constructor is set to this F.prototype.constructor
If the function modifies the prototype of the function before the object is created, it affects the Construtor property of the Created object
Such as:
Copy Code code as follows:
function F () {};
F.prototype={constructor: ' 1111 '};
var o=new F ();//o.constructor=== ' 1111 ' true
Inheritance principle:
Inheritance in JavaScript is the mechanism of using a prototype chain, where each instance of the function shares the data defined in the constructor prototype property, and for one class to inherit another, the parent function instance is assigned to the prototype property of the child function. And each time the new instance object is __proto__, the private property of the object is automatically connected to the prototype of the constructor.
Instanceof is to find the private prototype property chain of an instance object to determine whether it is an instance of the specified object
Concrete Examples:
Copy Code code as follows:
//instanceof implement
Function myinstanceof (obj,type)
{
var proto=o bj.__proto__;
while (proto)
{
if (proto ===type.prototype) break;
proto=proto.__proto__;
}
Return proto!=null;
}
Function View () {}
Function TreeView () {}
Treeview.prototype=new view ();//treeview.proto Type.__proto__=treeview.prototype automatically completes the
treeview.prototype.constructor=treeview;//correction Constructor
Var view= New TreeView ();//view.__proto__=treeview.prototype automatically completes
alert (view instanceof view);//true finds view.__proto__.__
Alert (view instanceof TreeView) is found when proto__,//true found when view.__proto__ find
Alert (myinstanceof (View,view));//tru E
Alert (myinstanceof (View,treeview));//true