JavaScript this調(diào)用規(guī)則說明
更新時間:2010年03月08日 18:03:17 作者:
我希望通過這些來使你們理解各種函數(shù)調(diào)用方式的不同,讓你的JavaScript代碼遠(yuǎn)離bugs。
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ù)?
因?yàn)樗侨謱ο體indow 的一個方法,
我們可以用如下方法驗(yàn)證:
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>
點(diǎn)擊第一個按鈕將會顯示”btn1”,因?yàn)樗且粋€方法調(diào)用,this為所屬的對象(按鈕元素) 。
點(diǎn)擊第二個按鈕將顯示”window”,因?yàn)?buttonClicked 是被直接調(diào)用的( 不像 obj.buttonClicked() ),
這和第三個按鈕,將事件處理函數(shù)直接放在標(biāo)簽里是一樣的.所以點(diǎn)擊第三個按鈕的結(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 ];
}
// 聲明實(shí)例化方法
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運(yùn)算符,沒有那個,你的函數(shù)就像全局函數(shù)一樣,且我們創(chuàng)建的那些屬性都將是創(chuàng)建在全局對象上(window),而你并不想那樣。
另外一點(diǎn),因?yàn)樵谀愕臉?gòu)造器里沒有返回值,所以如果你忘記使用new運(yùn)算符,將導(dǎo)致你的一些變量被賦值為 undefined。
所以構(gòu)造器函數(shù)以大寫字母開頭是一個好的習(xí)慣,這可以作為一個提醒,讓你在調(diào)用的時候不要忘記前面的new運(yùn)算符.
這樣 初始化函數(shù)里的代碼和你在其他語言里寫的初始化函數(shù)是相似的.this的值將是你將創(chuàng)建的對象.
總結(jié)
我希望通過這些來使你們理解各種函數(shù)調(diào)用方式的不同,
讓你的JavaScript代碼遠(yuǎn)離bugs。
知道this的值是你避免bugs的第一步。
(1)全局函數(shù)調(diào)用:
復(fù)制代碼 代碼如下:
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ù)?
因?yàn)樗侨謱ο體indow 的一個方法,
我們可以用如下方法驗(yàn)證:
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)用:
復(fù)制代碼 代碼如下:
//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 :
復(fù)制代碼 代碼如下:
var arrayMaker = {
someProperty: 'some value here',
make: function (arg1, arg2) {
return [ this, arg1, arg2 ];
}
};
如果不把調(diào)用規(guī)則二 弄明白,那么在事件處理代碼中 經(jīng)常會遇到各種各樣的bug,舉個例子:
復(fù)制代碼 代碼如下:
<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>
點(diǎn)擊第一個按鈕將會顯示”btn1”,因?yàn)樗且粋€方法調(diào)用,this為所屬的對象(按鈕元素) 。
點(diǎn)擊第二個按鈕將顯示”window”,因?yàn)?buttonClicked 是被直接調(diào)用的( 不像 obj.buttonClicked() ),
這和第三個按鈕,將事件處理函數(shù)直接放在標(biāo)簽里是一樣的.所以點(diǎn)擊第三個按鈕的結(jié)果是和第二個一樣的。
所以請大家注意:
復(fù)制代碼 代碼如下:
button1.onclick = buttonClicked;
button2.onclick = function(){
buttonClicked();
};
this指向是有區(qū)別的。
JavaScript函數(shù)調(diào)用規(guī)則三
當(dāng)然,如果使用的是jQuery庫,那么你不必考慮這么多,它會幫助重寫this的值以保證它包含了當(dāng)前事件源元素的引用。
復(fù)制代碼 代碼如下:
//使用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)行上下文重置。
復(fù)制代碼 代碼如下:
<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)建一個簡單的類型
復(fù)制代碼 代碼如下:
//聲明一個構(gòu)造器
function ArrayMaker(arg1, arg2) {
this.someProperty = 'whatever';
this.theArray = [ this, arg1, arg2 ];
}
// 聲明實(shí)例化方法
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運(yùn)算符,沒有那個,你的函數(shù)就像全局函數(shù)一樣,且我們創(chuàng)建的那些屬性都將是創(chuàng)建在全局對象上(window),而你并不想那樣。
另外一點(diǎn),因?yàn)樵谀愕臉?gòu)造器里沒有返回值,所以如果你忘記使用new運(yùn)算符,將導(dǎo)致你的一些變量被賦值為 undefined。
所以構(gòu)造器函數(shù)以大寫字母開頭是一個好的習(xí)慣,這可以作為一個提醒,讓你在調(diào)用的時候不要忘記前面的new運(yùn)算符.
這樣 初始化函數(shù)里的代碼和你在其他語言里寫的初始化函數(shù)是相似的.this的值將是你將創(chuàng)建的對象.
總結(jié)
我希望通過這些來使你們理解各種函數(shù)調(diào)用方式的不同,
讓你的JavaScript代碼遠(yuǎn)離bugs。
知道this的值是你避免bugs的第一步。
相關(guān)文章
xmlplus組件設(shè)計(jì)系列之下拉刷新(PullRefresh)(6)
xmlplus 是一個JavaScript框架,用于快速開發(fā)前后端項(xiàng)目。這篇文章主要介紹了xmlplus組件設(shè)計(jì)系列之下拉刷新,具有一定的參考價值,感興趣的小伙伴們可以參考一下2017-05-05
js+canvas實(shí)現(xiàn)飛機(jī)大戰(zhàn)
這篇文章主要為大家詳細(xì)介紹了js?canvas實(shí)現(xiàn)飛機(jī)大戰(zhàn),文中示例代碼介紹的非常詳細(xì),具有一定的參考價值,感興趣的小伙伴們可以參考一下2022-05-05
Bootstrap基本組件學(xué)習(xí)筆記之分頁(12)
這篇文章主要為大家詳細(xì)介紹了Bootstrap基本組件學(xué)習(xí)筆記之分頁,具有一定的參考價值,感興趣的小伙伴們可以參考一下2016-12-12
javascript+canvas實(shí)現(xiàn)刮刮卡抽獎效果
這篇文章主要介紹了javascript+canvas實(shí)現(xiàn)刮刮卡抽獎效果的相關(guān)資料,需要的朋友可以參考下2015-07-07
一文掌握J(rèn)avaScript數(shù)組常用工具函數(shù)總結(jié)
這篇文章主要介紹了一文掌握J(rèn)avaScript數(shù)組常用工具函數(shù)總結(jié),文章圍繞主題展開詳細(xì)的內(nèi)容介紹,具有一定的參考價值2022-06-06

