JavaScript中匿名函數,函數直接量和閉包

原文出處: http://www.dnew.cn/post/196.htm

先看下下面幾種寫法
1.function f(x){return x*x;};f(x);

2.(function(x){return x*x;})(x);

3.(function(x){return x*x;}(x));

第一種我們應該都很熟悉了,這是我們經常使用的寫法。第二第三種都是匿名函數的寫法。


第二種
可以這樣理解:
  1. var f=function(x) {return x*x;};f()

那我們不通過f這個變量來引用函數就是
  1. function(){}()

然而這樣肯定是錯誤的就像
  1. var f=1+2;
  2. f=f*0;


  1. var f=1+2*0;

結果不同一樣。
要得到正確結果只能:
  1. f=(1+2)*0;

也就是要明確的標識出程序塊,即:
  1. (function(){})()


肯你有疑問:括號“()”到底是不是起到了標識代碼塊的作用?
我們可以用JavaScript的內置函數檢測一下!
舉一個最簡單的例子:
  1. alert(4)

這段代碼會彈出提示內容是“4”
改成這樣
  1. (alert)(4)

可以看到執行的效果和上一段代碼一樣。

這種形式的函數執行也被很多JavaScript框架所採用。


第三種,如果你用過jsvm框架的話就會發現裏面的代碼使用了這種形式。
那如何解釋第三種情況呢?
爲了弄明白瀏覽器是如何理解這樣的寫法的,我們可以利用一下Mozilla Firefox的錯誤控制檯功能。
在代碼中插入一段錯誤代碼,代碼段如下:
  1. (function(s){s+s}(1)).splice();

打開Mozilla Firefox的錯誤控制檯,可以看到有如下的錯誤提示
錯誤: (function (s) {})(1) has no properties
源文件:file:///C:/Documents…….html
行:18

可以認爲,瀏覽器對於
  1. (function(s){s+s}(1))
這樣的代碼按照
  1. (function (s) {s+s})(1)
來解析的。



到此可能你有這樣的認識:
  1. function f(x){return x*x;};f(x);==(function(x){return x*x;})(x);==(function(x){return x*x;}(x));


但是他們還是有區別的,
首先,對於像第二和第三種形式,其它的函數和代碼是不可能調用所定義的函數的,有一種說發把這樣的函數稱爲匿名函數或者函數直接量。
其次,第二和第三種形式執行的函數,中間變量不會污染到全局命名空間,你可以把中間的代碼看作純粹的子過程調用。
當然使用後面兩種形式的函數定義可以很容易的實現閉包。
看一個例子:
  1. /*
  2. http://jibbering.com/faq/faq_notes/closures.html(Dnew.CN注)
  3. A global variable - getImgInPositionedDivHtml - is declared and
  4.    assigned the value of an inner function expression returned from
  5.    a one-time call to an outer function expression.

  6.    That inner function returns a string of HTML that represents an
  7.    absolutely positioned DIV wrapped round an IMG element, such that
  8.    all of the variable attribute values are provided as parameters
  9.    to the function call:-
  10. */
  11. var getImgInPositionedDivHtml = (function(){
  12.      /* The - buffAr - Array is assigned to a local variable of the
  13.          outer function expression. It is only created once and that one
  14.          instance of the array is available to the inner function so that
  15.          it can be used on each execution of that inner function.

  16.          Empty strings are used as placeholders for the date that is to
  17.          be inserted into the Array by the inner function:-
  18.      */
  19.      var buffAr = [
  20.            '<div id="',
  21.            '',    //index 1, DIV ID attribute
  22.            '" style="position:absolute;top:',
  23.            '',    //index 3, DIV top position
  24.            'px;left:',
  25.            '',    //index 5, DIV left position
  26.            'px;width:',
  27.            '',    //index 7, DIV width
  28.            'px;height:',
  29.            '',    //index 9, DIV height
  30.            'px;overflow:hidden;/"><img src=/"',
  31.            '',    //index 11, IMG URL
  32.            '/" width=/"',
  33.            '',    //index 13, IMG width
  34.            '/" height=/"',
  35.            '',    //index 15, IMG height
  36.            '/" alt=/"',
  37.            '',    //index 17, IMG alt text
  38.            '/"><//div>'
  39.      ];
  40.      /* Return the inner function object that is the result of the
  41.          evaluation of a function expression. It is this inner function
  42.          object that will be executed on each call to -
  43.          getImgInPositionedDivHtml( ... ) -:-
  44.      */
  45.      return (function(url, id, width, height, top, left, altText){
  46.            /* Assign the various parameters to the corresponding
  47.                locations in the buffer array:-
  48.            */
  49.            buffAr[1] = id;
  50.            buffAr[3] = top;
  51.            buffAr[5] = left;
  52.            buffAr[13] = (buffAr[7] = width);
  53.            buffAr[15] = (buffAr[9] = height);
  54.            buffAr[11] = url;
  55.            buffAr[17] = altText;
  56.            /* Return the string created by joining each element in the
  57.                array using an empty string (which is the same as just
  58.                joining the elements together):-
  59.            */
  60.            return buffAr.join('');
  61.      }); //:End of inner function expression.
  62. })();
  63. /*^^- :The inline execution of the outer function expression. */
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章