JavaScript 函數(shù)調(diào)用規(guī)則
更新時間:2009年09月14日 12:56:10 作者:
JavaScript 函數(shù)調(diào)用規(guī)則
JavaScript函數(shù)調(diào)用規(guī)則一
(1)全局函數(shù)調(diào)用:
function makeArray( arg1, arg2 ){
return [this , arg1 , arg2 ];
}
這是一個最常用的定義函數(shù)方式。相信學(xué)習(xí)JavaScript的人對它的調(diào)用并不陌生。
調(diào)用代碼如下:
makeArray('one', 'two');
// => [ window, 'one', 'two' ]
這種方式可以說是全局的函數(shù)調(diào)用。
為什么說是全局的函數(shù)?
因為它是全局對象window 的一個方法,
我們可以用如下方法驗證:
alert( typeof window.methodThatDoesntExist );
// => undefined
alert( typeof window.makeArray);
// => function
所以我們之前調(diào)用 makeArray的方法是和下面調(diào)用的方法一樣的
window.makeArray('one', 'two');
// => [ window, 'one', 'two' ]
JavaScript函數(shù)調(diào)用規(guī)則二
(1)對象方法調(diào)用:
//creating the object
var arrayMaker = {
someProperty: 'some value here',
make: makeArray
};
arrayMaker.make('one', 'two'); // => [ arrayMaker, 'one', 'two' ]
//或者用下面的方法調(diào)用:
arrayMaker['make']('one', 'two'); // => [ arrayMaker, 'one', 'two' ]
看到這里跟剛才的區(qū)別了吧,this的值變成了對象本身.
你可能會質(zhì)疑:為什么原始的函數(shù)定義并沒有改變,而this卻變化了呢?
非常好,有質(zhì)疑是正確的。這里涉及到 函數(shù)在JavaScript中傳遞的方式,
函數(shù)在JavaScript 里是一個標(biāo)準(zhǔn)的數(shù)據(jù)類型,
確切的說是一個對象.你可以傳遞它們或者復(fù)制他們.
就好像整個函數(shù)連帶參數(shù)列表和函數(shù)體都被復(fù)制,
且被分配給了 arrayMaker 里的屬性 make,那就好像這樣定義一個 arrayMaker :
var arrayMaker = {
someProperty: 'some value here',
make: function (arg1, arg2) {
return [ this, arg1, arg2 ];
}
};
如果不把調(diào)用規(guī)則二 弄明白,那么在事件處理代碼中 經(jīng)常會遇到各種各樣的bug,舉個例子:
<input type="button" value="Button 1" id="btn1" />
<input type="button" value="Button 2" id="btn2" />
<input type="button" value="Button 3" id="btn3" onclick="buttonClicked();"/>
< script type="text/javascript">
function buttonClicked(){
var text = (this === window) ? 'window' : this.id;
alert( text );
}
var button1 = document.getElementById('btn1');
var button2 = document.getElementById('btn2');
button1.onclick = buttonClicked;
button2.onclick = function(){
buttonClicked();
};
< /script>
點擊第一個按鈕將會顯示”btn1”,因為它是一個方法調(diào)用,this為所屬的對象(按鈕元素) 。
點擊第二個按鈕將顯示”window”,因為 buttonClicked 是被直接調(diào)用的( 不像 obj.buttonClicked() ),
這和第三個按鈕,將事件處理函數(shù)直接放在標(biāo)簽里是一樣的.所以點擊第三個按鈕的結(jié)果是和第二個一樣的。
所以請大家注意:
button1.onclick = buttonClicked;
button2.onclick = function(){
buttonClicked();
};
this指向是有區(qū)別的。
JavaScript函數(shù)調(diào)用規(guī)則三
當(dāng)然,如果使用的是jQuery庫,那么你不必考慮這么多,它會幫助重寫this的值以保證它包含了當(dāng)前事件源元素的引用。
//使用jQuery
$('#btn1').click( function() {
alert( this.id ); // jQuery ensures 'this' will be the button
});
那么 jQuery是如何重載this的值的呢?
答案是: call()和apply();
當(dāng)函數(shù)使用的越來越多時,你會發(fā)現(xiàn)你需要的this 并不在相同的上下文里,這樣導(dǎo)致通訊起來異常困難。
在Javascript中函數(shù)也是對象,函數(shù)對象包含一些預(yù)定義的方法,其中有兩個便是apply()和call(),我們可以使用它們來對this進(jìn)行上下文重置。
<input type="button" value="Button 1" id="btn1" />
<input type="button" value="Button 2" id="btn2" />
<input type="button" value="Button 3" id="btn3" onclick="buttonClicked();"/>
< script type="text/javascript">
function buttonClicked(){
var text = (this === window) ? 'window' : this.id;
alert( text );
}
var button1 = document.getElementById('btn1');
var button2 = document.getElementById('btn2');
button1.onclick = buttonClicked;
button2.onclick = function(){
buttonClicked.call(this); // btn2
};
< /script>
JavaScript函數(shù)調(diào)用規(guī)則四
(1)構(gòu)造器
我不想深入研究在Javascript中類型的定義,但是在此刻我們需要知道在Javascript中沒有類,
而且任何一個自定義的類型需要一個初始化函數(shù),使用原型對象(作為初始化函數(shù)的一個屬性)定義你的類型也是一個不錯的想法,
讓我們來創(chuàng)建一個簡單的類型
//聲明一個構(gòu)造器
function ArrayMaker(arg1, arg2) {
this.someProperty = 'whatever';
this.theArray = [ this, arg1, arg2 ];
}
// 聲明實例化方法
ArrayMaker.prototype = {
someMethod: function () {
alert( 'someMethod called');
},
getArray: function () {
return this.theArray;
}
};
var am = new ArrayMaker( 'one', 'two' );
var other = new ArrayMaker( 'first', 'second' );
am.getArray();
// => [ am, 'one' , 'two' ]
other.getArray();
// => [ other, 'first', 'second' ]
一個非常重要并值得注意的是出現(xiàn)在函數(shù)調(diào)用前面的new運算符,沒有那個,你的函數(shù)就像全局函數(shù)一樣,且我們創(chuàng)建的那些屬性都將是創(chuàng)建在全局對象上(window),而你并不想那樣。
另外一點,因為在你的構(gòu)造器里沒有返回值,所以如果你忘記使用new運算符,將導(dǎo)致你的一些變量被賦值為 undefined。
所以構(gòu)造器函數(shù)以大寫字母開頭是一個好的習(xí)慣,這可以作為一個提醒,讓你在調(diào)用的時候不要忘記前面的new運算符.
這樣 初始化函數(shù)里的代碼和你在其他語言里寫的初始化函數(shù)是相似的.this的值將是你將創(chuàng)建的對象.
總結(jié)
我希望通過這些來使你們理解各種函數(shù)調(diào)用方式的不同,
讓你的JavaScript代碼遠(yuǎn)離bugs。
知道this的值是你避免bugs的第一步。
原文:http://devlicio.us/blogs/sergio_pereira/archive/2009/02/09/javascript-5-ways-to-call-a-function.aspx
中文翻譯:http://www.yeeyan.com/articles/view/69184/30270
(1)全局函數(shù)調(diào)用:
function makeArray( arg1, arg2 ){
return [this , arg1 , arg2 ];
}
這是一個最常用的定義函數(shù)方式。相信學(xué)習(xí)JavaScript的人對它的調(diào)用并不陌生。
調(diào)用代碼如下:
makeArray('one', 'two');
// => [ window, 'one', 'two' ]
這種方式可以說是全局的函數(shù)調(diào)用。
為什么說是全局的函數(shù)?
因為它是全局對象window 的一個方法,
我們可以用如下方法驗證:
alert( typeof window.methodThatDoesntExist );
// => undefined
alert( typeof window.makeArray);
// => function
所以我們之前調(diào)用 makeArray的方法是和下面調(diào)用的方法一樣的
window.makeArray('one', 'two');
// => [ window, 'one', 'two' ]
JavaScript函數(shù)調(diào)用規(guī)則二
(1)對象方法調(diào)用:
//creating the object
var arrayMaker = {
someProperty: 'some value here',
make: makeArray
};
arrayMaker.make('one', 'two'); // => [ arrayMaker, 'one', 'two' ]
//或者用下面的方法調(diào)用:
arrayMaker['make']('one', 'two'); // => [ arrayMaker, 'one', 'two' ]
看到這里跟剛才的區(qū)別了吧,this的值變成了對象本身.
你可能會質(zhì)疑:為什么原始的函數(shù)定義并沒有改變,而this卻變化了呢?
非常好,有質(zhì)疑是正確的。這里涉及到 函數(shù)在JavaScript中傳遞的方式,
函數(shù)在JavaScript 里是一個標(biāo)準(zhǔn)的數(shù)據(jù)類型,
確切的說是一個對象.你可以傳遞它們或者復(fù)制他們.
就好像整個函數(shù)連帶參數(shù)列表和函數(shù)體都被復(fù)制,
且被分配給了 arrayMaker 里的屬性 make,那就好像這樣定義一個 arrayMaker :
var arrayMaker = {
someProperty: 'some value here',
make: function (arg1, arg2) {
return [ this, arg1, arg2 ];
}
};
如果不把調(diào)用規(guī)則二 弄明白,那么在事件處理代碼中 經(jīng)常會遇到各種各樣的bug,舉個例子:
<input type="button" value="Button 1" id="btn1" />
<input type="button" value="Button 2" id="btn2" />
<input type="button" value="Button 3" id="btn3" onclick="buttonClicked();"/>
< script type="text/javascript">
function buttonClicked(){
var text = (this === window) ? 'window' : this.id;
alert( text );
}
var button1 = document.getElementById('btn1');
var button2 = document.getElementById('btn2');
button1.onclick = buttonClicked;
button2.onclick = function(){
buttonClicked();
};
< /script>
點擊第一個按鈕將會顯示”btn1”,因為它是一個方法調(diào)用,this為所屬的對象(按鈕元素) 。
點擊第二個按鈕將顯示”window”,因為 buttonClicked 是被直接調(diào)用的( 不像 obj.buttonClicked() ),
這和第三個按鈕,將事件處理函數(shù)直接放在標(biāo)簽里是一樣的.所以點擊第三個按鈕的結(jié)果是和第二個一樣的。
所以請大家注意:
button1.onclick = buttonClicked;
button2.onclick = function(){
buttonClicked();
};
this指向是有區(qū)別的。
JavaScript函數(shù)調(diào)用規(guī)則三
當(dāng)然,如果使用的是jQuery庫,那么你不必考慮這么多,它會幫助重寫this的值以保證它包含了當(dāng)前事件源元素的引用。
//使用jQuery
$('#btn1').click( function() {
alert( this.id ); // jQuery ensures 'this' will be the button
});
那么 jQuery是如何重載this的值的呢?
答案是: call()和apply();
當(dāng)函數(shù)使用的越來越多時,你會發(fā)現(xiàn)你需要的this 并不在相同的上下文里,這樣導(dǎo)致通訊起來異常困難。
在Javascript中函數(shù)也是對象,函數(shù)對象包含一些預(yù)定義的方法,其中有兩個便是apply()和call(),我們可以使用它們來對this進(jìn)行上下文重置。
<input type="button" value="Button 1" id="btn1" />
<input type="button" value="Button 2" id="btn2" />
<input type="button" value="Button 3" id="btn3" onclick="buttonClicked();"/>
< script type="text/javascript">
function buttonClicked(){
var text = (this === window) ? 'window' : this.id;
alert( text );
}
var button1 = document.getElementById('btn1');
var button2 = document.getElementById('btn2');
button1.onclick = buttonClicked;
button2.onclick = function(){
buttonClicked.call(this); // btn2
};
< /script>
JavaScript函數(shù)調(diào)用規(guī)則四
(1)構(gòu)造器
我不想深入研究在Javascript中類型的定義,但是在此刻我們需要知道在Javascript中沒有類,
而且任何一個自定義的類型需要一個初始化函數(shù),使用原型對象(作為初始化函數(shù)的一個屬性)定義你的類型也是一個不錯的想法,
讓我們來創(chuàng)建一個簡單的類型
//聲明一個構(gòu)造器
function ArrayMaker(arg1, arg2) {
this.someProperty = 'whatever';
this.theArray = [ this, arg1, arg2 ];
}
// 聲明實例化方法
ArrayMaker.prototype = {
someMethod: function () {
alert( 'someMethod called');
},
getArray: function () {
return this.theArray;
}
};
var am = new ArrayMaker( 'one', 'two' );
var other = new ArrayMaker( 'first', 'second' );
am.getArray();
// => [ am, 'one' , 'two' ]
other.getArray();
// => [ other, 'first', 'second' ]
一個非常重要并值得注意的是出現(xiàn)在函數(shù)調(diào)用前面的new運算符,沒有那個,你的函數(shù)就像全局函數(shù)一樣,且我們創(chuàng)建的那些屬性都將是創(chuàng)建在全局對象上(window),而你并不想那樣。
另外一點,因為在你的構(gòu)造器里沒有返回值,所以如果你忘記使用new運算符,將導(dǎo)致你的一些變量被賦值為 undefined。
所以構(gòu)造器函數(shù)以大寫字母開頭是一個好的習(xí)慣,這可以作為一個提醒,讓你在調(diào)用的時候不要忘記前面的new運算符.
這樣 初始化函數(shù)里的代碼和你在其他語言里寫的初始化函數(shù)是相似的.this的值將是你將創(chuàng)建的對象.
總結(jié)
我希望通過這些來使你們理解各種函數(shù)調(diào)用方式的不同,
讓你的JavaScript代碼遠(yuǎn)離bugs。
知道this的值是你避免bugs的第一步。
原文:http://devlicio.us/blogs/sergio_pereira/archive/2009/02/09/javascript-5-ways-to-call-a-function.aspx
中文翻譯:http://www.yeeyan.com/articles/view/69184/30270
您可能感興趣的文章:
- JavaScript實現(xiàn)顯示函數(shù)調(diào)用堆棧的方法
- 跟我學(xué)習(xí)javascript的函數(shù)調(diào)用和構(gòu)造函數(shù)調(diào)用
- JS中獲取函數(shù)調(diào)用鏈所有參數(shù)的方法
- js中函數(shù)調(diào)用的兩種常用方法使用介紹
- js函數(shù)調(diào)用的方式
- JS嵌套函數(shù)調(diào)用上下文的問題解決
- js this函數(shù)調(diào)用無需再次抓獲id,name或標(biāo)簽名
- js函數(shù)調(diào)用常用方法詳解
- js 函數(shù)調(diào)用模式小結(jié)
- javascript 函數(shù)調(diào)用的對象和方法
- javascript 函數(shù)調(diào)用規(guī)則
- javascript iframe內(nèi)的函數(shù)調(diào)用實現(xiàn)方法
- Javascript 函數(shù)的四種調(diào)用模式
相關(guān)文章
關(guān)于Javascript回調(diào)函數(shù)的一個妙用
相信在剛開始學(xué)習(xí)JavaScript的時候,很多人感到最困惑的就是回調(diào)函數(shù)了。本文通過一個小小的例子來分析回調(diào)函數(shù)的用法。對大家學(xué)習(xí)Javascript回調(diào)函數(shù)很有幫助,有需要的可以來參考學(xué)習(xí)。2016-08-08關(guān)于前后端json數(shù)據(jù)的發(fā)送與接收詳解
這篇文章主要給大家介紹了關(guān)于前后端json數(shù)據(jù)發(fā)送與接收的相關(guān)資料,文中通過示例代碼詳細(xì)介紹了關(guān)于flask中的json數(shù)據(jù)接收和前端發(fā)送json數(shù)據(jù)等內(nèi)容,需要的朋友可以參考借鑒,下面來一起看看吧。2017-07-07javascript中alert()與console.log()的區(qū)別
我們在做js調(diào)試的時候使用 alert 可以顯示信息,調(diào)試程序,alert 彈出窗口會中斷程序, 如果要在循環(huán)中顯示信息,手點擊關(guān)閉窗口都累死。而且 alert 顯示對象永遠(yuǎn)顯示為[object ]。 自己寫的 log 雖然可以顯示一些 object 信息,但很多功能支持都沒有 console 好2015-08-08JS使用canvas繪制旋轉(zhuǎn)風(fēng)車動畫
這篇文章主要為大家詳細(xì)介紹了JS使用canvas繪制旋轉(zhuǎn)風(fēng)車動畫,有加速減速啟動停止功能,文中示例代碼介紹的非常詳細(xì),具有一定的參考價值,感興趣的小伙伴們可以參考一下2022-02-02