自拍偷在线精品自拍偷,亚洲欧美中文日韩v在线观看不卡

JavaScript函數(shù)調(diào)用的四個規(guī)則

開發(fā) 前端
本文講述了四個JavaScript函數(shù)調(diào)用規(guī)則,包括全局函數(shù)調(diào)用、對象方法調(diào)用、構(gòu)造器等內(nèi)容。

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﹥  

點(diǎn)擊***個按鈕將會顯示”btn1”,因為它是一個方法調(diào)用,this為所屬的對象(按鈕元素) 。
點(diǎn)擊第二個按鈕將顯示”window”,因為 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),因為在你的構(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. Javascript中的replace方法與正則表達(dá)式講解
  2. 詳解Javascript trim()函數(shù)實(shí)現(xiàn)
  3. 應(yīng)用最廣的十大Javascript框架
責(zé)任編輯:book05 來源: okajax
相關(guān)推薦

2022-06-27 23:31:01

JavaScript框架開發(fā)

2022-01-12 15:50:24

JavaScript開發(fā)循環(huán)

2021-08-23 10:37:14

Javascript 機(jī)器學(xué)習(xí)阿里云

2022-08-02 10:33:11

JavaScript代碼

2023-10-17 08:57:21

2023-10-26 07:47:35

JavaScript代碼變量

2023-05-04 09:02:56

2020-10-29 08:35:06

Pandas函數(shù)Python

2025-03-31 08:45:00

作用域Python編程

2023-09-06 16:55:33

JavaScript閉包

2013-03-18 13:31:28

2024-06-25 12:45:05

2021-09-27 08:56:36

Python代碼函數(shù)

2022-02-23 15:09:18

數(shù)字化轉(zhuǎn)型國有企業(yè)數(shù)據(jù)

2023-09-26 12:34:29

Python迭代過濾函數(shù)

2022-06-30 08:31:54

排序函數(shù)SQL

2020-06-04 08:15:53

Kubernetes容器PaaS

2025-04-21 06:25:00

2020-08-13 10:29:55

項目管理項目經(jīng)理CIO

2022-07-30 07:50:40

數(shù)據(jù)庫字段存儲
點(diǎn)贊
收藏

51CTO技術(shù)棧公眾號