對(duì)象創(chuàng)建:
當(dāng)一個(gè)函數(shù)對(duì)象被創(chuàng)建時(shí)候,F(xiàn)unction構(gòu)造器產(chǎn)生的函數(shù)對(duì)象會(huì)運(yùn)行類似這樣的代碼:
復(fù)制代碼 代碼如下:
this.prototype={constructor:this};
復(fù)制代碼 代碼如下:
function F(){};
F.prototype={constructor:'1111'};
var o=new F();//o.constructor===‘1111' true
復(fù)制代碼 代碼如下:
//instanceof實(shí)現(xiàn)
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 自動(dòng)完成
TreeView.prototype.constructor=TreeView;//修正constructor
var view=new TreeView();//view.__proto__=TreeView.prototype 自動(dòng)完成
alert(view instanceof View); //true 查找到view.__proto__.__proto__時(shí)找到
alert(view instanceof TreeView); //true 查找到view.__proto__時(shí)找到
alert(Myinstanceof(view,View)); //true
alert(Myinstanceof(view,TreeView)); //true
新聞熱點(diǎn)
疑難解答
圖片精選