call 和apply 功能神似,參數不同
function
.apply([thisObj[,argArray]])
function
.call([thisObj[,arg1[, arg2[, [,.argN]]]]])
function A(){
this.flag = 'A';
this.flaf='C';
this.tip = function(){
alert("flag"+this.flag);
};
console.log(this);
alert("flaw"+this.flaw);
alert("flaf"+this.flaf);
}
function B(){
this.flag = 'B';
this.flaf='D';
}
A.prototype.flaw="E";
B.prototype.flaw="F";
A();//執行函數A this指向window,this.flaw 未定義
var a = new A(); // 執行函數A this指向函數A,a=等於函數本身,a不是函數不能執行, this.flaw="E",其中this.flaw是原生屬性,在this._proto_裡面,a.flag='A' a.flaf='C'
var b = new B();
a.flaX="G";
b.flaX="H";
A.apply(b);// 執行函數 A ,這是this.flag,this.flaf被重新賦值,this.flaX='H' this.flaw="F",其中this.flaw是原生屬性,在this._proto_裡面
a.tip.apply(b);// 執行函數tip,this.flag未重新賦值,this.flag='B',this.flaw="F"