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 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 automatic completion
treeview.prototype.constructor=treeview;//Correction Constructor
var view=new TreeView ();//view.__proto__=treeview.prototype Auto Complete
Alert (view instanceof view); True to find when view.__proto__.__proto__
Alert (view instanceof TreeView); True to find when view.__proto__
Alert (myinstanceof (View,view)); True
Alert (myinstanceof (View,treeview)); True
The custom myinstanceof above is the function of a instanceof function that is implemented by oneself, because the IE Kernel Instance store prototype is not __proto__, so myinstanceof can not pass, other browsers should have no problem