Object creation:
When a function object is created, the function object generated by the Function constructor runs like this:
Copy code code as follows:
this.prototype = {constructor: this};
Suppose function f
When F is constructed by a 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, it will affect the Construtor property of the creative object
like:
Copy code code as follows:
function f () {};
F.prototype = {constructor: '1111'};
var o = new f (); // o.constructor === '1111' true
Principle of inheritance:
The inheritance in JavaScript is a mechanism that uses the original chain. Each function instance shares the data defined in the constructor prototype property. To inherit another class, the parent function instance is assigned to the propotype property of the sub -function. And every time the NEW instance object, the private attribute of the object __proto__ will be automatically connected to the prototype of the constructor.
Instanceof is to find the private prototype property chain of the instance object to determine whether it is an instance of the specified object
Specific examples:
Copy code code as follows:
// Instanceof implementation
Function MyInstanceof (Obj, Type)
{{
var proto = obj .__loto__;
While (Proto)
{{
if (propo === Type.prototype) Break;
Proto = Proto .__Loto__;
}
Return Proto! = Null;
}
function view () {}
Function treeView () {}
TreeView.prototype = New View (); // Treeview.prototype .__Loto __ = Treeview.prototype automatically completes automatically completion
TreeView.prototype.ConStructor = treeView; // Corphrate constructor
var view = new treeview (); // view .__ propo __ = timeView.prototype automatically completes
Alert (view instanceof view); // True finds View .__ Proto __.__ Proto__
Alert (view instanceof treeView); // True finds View .__Loto___
alert (myInstanceof (view, view)); // true
alert (myInstanceof (view, treeView)); // True