欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

js 函數(shù)調(diào)用模式小結(jié)

 更新時(shí)間:2011年12月26日 23:11:26   作者:  
在javascript中一共有四種調(diào)用模式:方法調(diào)用模式、函數(shù)調(diào)用模式、構(gòu)造器調(diào)用模式和apply調(diào)用模式。這些模式在如何初始化關(guān)鍵參數(shù)this上存在差異
方法調(diào)用模式
當(dāng)一個(gè)函數(shù)被保存為對(duì)象的一個(gè)屬性時(shí),我們稱之它為該對(duì)象的一個(gè)方法,那么this被綁定到該對(duì)象上。
復(fù)制代碼 代碼如下:

var myObject={
name : "myObject" ,
value : 0 ,
increment : function(num){
this.value += typeof(num) === 'number' ? num : 0;
} ,
toString : function(){
return '[Object:'+this.name+' {value:'+this.value+'}]';
}
}
alert(myObject);//[Object:myObject {value:0}]

函數(shù)調(diào)用模式
當(dāng)一個(gè)函數(shù)并非一個(gè)對(duì)象的函數(shù)時(shí),那么它被當(dāng)作一個(gè)函數(shù)來調(diào)用,this被綁定到全局對(duì)象上。這是語言設(shè)計(jì)的一個(gè)錯(cuò)誤。倘若語言設(shè)計(jì)正確,當(dāng)內(nèi)部函數(shù)調(diào)用時(shí),this應(yīng)該仍然綁定到外部函數(shù)的this變量上。如:
復(fù)制代碼 代碼如下:

var myObject={
name : "myObject" ,
value : 0 ,
increment : function(num){
this.value += typeof(num) === 'number' ? num : 0;
} ,
toString : function(){
return '[Object:'+this.name+' {value:'+this.value+'}]';
},
getInfo:function(){
return (function(){
return this.toString();//內(nèi)部匿名函數(shù)中this指向了全局對(duì)象window
})();
}
}
alert(myObject.getInfo());//[object Window]

當(dāng)幸運(yùn)的是,有一個(gè)很容易的解決方案:定義一個(gè)變量并給它賦值為this,那么內(nèi)部函數(shù)通過該變量訪問到指向該對(duì)象的this,如:
復(fù)制代碼 代碼如下:

var myObject={
name : "myObject" ,
value : 0 ,
increment : function(num){
this.value += typeof(num) === 'number' ? num : 0;
} ,
toString : function(){
return '[Object:'+this.name+' {value:'+this.value+'}]';
},
getInfo:function(){
var self=this;
return (function(){
return self.toString();//通過變量self指向myObject對(duì)象
})();
}
}
alert(myObject.getInfo());//[Object:myObject {value:0}]

構(gòu)造器調(diào)用模式
JavaScript是一門基于原型繼承的語言。這意味著對(duì)象可以直接從其他對(duì)象繼承屬性。該語言是無類別的。
如果一個(gè)函數(shù)前面帶上new來調(diào)用,那么將創(chuàng)建一個(gè)隱藏連接到該函數(shù)的prototype成員的新對(duì)象,同時(shí)this將會(huì)被綁定到構(gòu)造函數(shù)的實(shí)例上。
復(fù)制代碼 代碼如下:

function MyObject(name){
this.name=name || 'MyObject';
this.value=0;
this.increment=function(num){
this.value += typeof(num) === 'number' ? num : 0;
};
this.toString=function(){
return '[Object:'+this.name+' {value:'+this.value+'}]';
}
this.target=this;
}
MyObject.prototype.getInfo=function(){
return this.toString();
}
/*
同時(shí)創(chuàng)建一個(gè)MyObject.prototype對(duì)象,myObject繼承了MyObject.prototype的所有的屬性,
this綁定到了MyObject的實(shí)例上
*/
var myObject=new MyObject();
var otherObject=new MyObject();
//alert(myObject.target===myObject);//ture
//alert(myObject.target.getInfo());//[Object:MyObject {value:0}]
myObject.increment(10);
otherObject.increment(20);
alert(myObject.value);//10
alert(otherObject.value);//20

Apply 調(diào)用模式
JavaScript是一門函數(shù)式的面向?qū)ο缶幊陶Z言,所以函數(shù)可以擁有方法。
函數(shù)的apply方法,如同該對(duì)象擁有此方法,使該對(duì)象擁有此方法。此時(shí)this指向該對(duì)象。
apply接收兩個(gè)參數(shù),第一個(gè)是要綁定的對(duì)象(this指向的對(duì)象),第二個(gè)是參數(shù)數(shù)組.
復(fù)制代碼 代碼如下:

function MyObject(name){
this.name=name || 'MyObject';
this.value=0;
this.increment=function(num){
this.value += typeof(num) === 'number' ? num : 0;
};
this.toString=function(){
return '[Object:'+this.name+' {value:'+this.value+'}]';
}
this.target=this;
}
function getInfo(){
return this.toString();
}
var myObj=new MyObject();
alert(getInfo.apply(myObj));//[Object:MyObject {value:0}],this指向myObj
alert(getInfo.apply(window));//[object Window],this指向window

相關(guān)文章

最新評(píng)論