js 函數調用模式

來源:互聯網
上載者:User

1.方法調用模式
2.函數調用模式
3.構造器調用模式
4.Apply 調用模式

在javascript中一共有四種調用模式:方法調用模式、函數調用模式、構造器調用模式和apply調用模式。這些模式在如何初始化關鍵參數this上存在差異

1.方法調用模式
當一個函數被儲存為對象的一個屬性時,我們稱之它為該對象的一個方法,那麼this被綁定到該對象上。
代碼如下:

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}]

2.函數調用模式
當一個函數並非一個對象的函數時,那麼它被當作一個函數來調用,this被綁定到全域對象上。這是語言設計的一個錯誤。倘若語言設計正確,當內建函式調用時,this應該仍然綁定到外部函數的this變數上。如:
代碼如下: www.2cto.com

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();//內部匿名函數中this指向了全域對象window
})();
}
}
alert(myObject.getInfo());//[object Window]

當幸運的是,有一個很容易的解決方案:定義一個變數並給它賦值為this,那麼內建函式通過該變數訪問到指向該對象的this,如:
 代碼如下:

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對象
})();
}
}
alert(myObject.getInfo());//[Object:myObject {value:0}]

3.構造器調用模式
JavaScript是一門基於原型繼承的語言。這意味著對象可以直接從其他對象繼承屬性。該語言是無類別的。
如果一個函數前面帶上new來調用,那麼將建立一個隱藏串連到該函數的prototype成員的新對象,同時this將會被綁定到建構函式的執行個體上。
 代碼如下:

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();
}
/*
同時建立一個MyObject.prototype對象,myObject繼承了MyObject.prototype的所有的屬性,
this綁定到了MyObject的執行個體上
*/
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

4.Apply 調用模式
JavaScript是一門函數式的物件導向程式設計語言,所以函數可以擁有方法。
函數的apply方法,如同該對象擁有此方法,使該對象擁有此方法。此時this指向該對象。
apply接收兩個參數,第一個是要綁定的對象(this指向的對象),第二個是參數數組.
 代碼如下:

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

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.