Object creation:
When a function object is created, the function object generated by the Function constructor will run code similar to this:
this.prototype={constructor:this};
Assume function F When F constructs an object using 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 the object, it will affect the constructor attribute of the created object
For example :
function F(){};
F .prototype={constructor:'1111'};
var o=new F();//o.constructor==='1111' 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 another, you need to assign the parent function instance to the child function. prototype property. And every time a new instance object is created, the object's private property __proto__ will be automatically connected to the prototype of the constructor.
instanceof is to look up the private prototype attribute chain of the instance object to determine whether it is an instance of the specified object
Specific instance:
//instanceof implements
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 automatic completion
alert(view instanceof View); //true finds view.__proto__.
alert(view instanceof TreeView) is found when __proto__ is found; //true is found when view.__proto__ is found
alert(Myinstanceof(view,View)); //true
alert(Myinstanceof(view, TreeView)); //true
The customized Myinstanceof above is a function of instanceof function implemented by myself. Since the IE kernel instance storage prototype is not __proto__, Myinstanceof will not pass, and other browsers There should be no problem
Statement:The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn