本文講述了四個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ù)方式。相信學習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 里是一個標準的數(shù)據(jù)類型, 
確切的說是一個對象.你可以傳遞它們或者復制他們. 
就好像整個函數(shù)連帶參數(shù)列表和函數(shù)體都被復制, 
且被分配給了 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ù)直接放在標簽里是一樣的.所以點擊第三個按鈕的結(jié)果是和第二個一樣的。 
所以請大家注意: 
button1.onclick = buttonClicked;  button2.onclick = function(){     buttonClicked();     };   | 
this指向是有區(qū)別的。 
JavaScript函數(shù)調(diào)用規(guī)則三 
當然,如果使用的是jQuery庫,那么你不必考慮這么多,它會幫助重寫this的值以保證它包含了當前事件源元素的引用。 
//使用jQuery  $('#btn1').click( function() {      alert( this.id ); // jQuery ensures 'this' will be the button  });   | 
那么 jQuery是如何重載this的值的呢? 
答案是: call()和apply(); 
當函數(shù)使用的越來越多時,你會發(fā)現(xiàn)你需要的this 并不在相同的上下文里,這樣導致通訊起來異常困難。 
在Javascript中函數(shù)也是對象,函數(shù)對象包含一些預定義的方法,其中有兩個便是apply()和call(),我們可以使用它們來對this進行上下文重置。 
﹤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運算符,將導致你的一些變量被賦值為 undefined。
所以構(gòu)造器函數(shù)以大寫字母開頭是一個好的習慣,這可以作為一個提醒,讓你在調(diào)用的時候不要忘記前面的new運算符. 
這樣 初始化函數(shù)里的代碼和你在其他語言里寫的初始化函數(shù)是相似的.this的值將是你將創(chuàng)建的對象. 
總結(jié)
我希望通過這些來使你們理解各種函數(shù)調(diào)用方式的不同,讓你的JavaScript代碼遠離bugs。知道this的值是你避免bugs的***步。
【編輯推薦】
- Javascript中的replace方法與正則表達式講解
 
- 詳解Javascript trim()函數(shù)實現(xiàn)
 
- 應(yīng)用最廣的十大Javascript框架