对象创建:
当一个函数对象被创建时候,Function构造器产生的函数对象会运行类似这样的代码:
复制代码 代码如下:
this.prototype={constructor:this};
复制代码 代码如下:
function F(){};
F.prototype={constructor:'1111'};
var o=new F();//o.constructor===‘1111' true
复制代码 代码如下:
//instanceof实现
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 自动完成
TreeView.prototype.constructor=TreeView;//修正constructor
var view=new TreeView();//view.__proto__=TreeView.prototype 自动完成
alert(view instanceof View); //true 查找到view.__proto__.__proto__时找到
alert(view instanceof TreeView); //true 查找到view.__proto__时找到
alert(Myinstanceof(view,View)); //true
alert(Myinstanceof(view,TreeView)); //true
新闻热点
疑难解答
图片精选