JavaScript中匿名函數

來源:互聯網
上載者:User

在JavaScript中定義函數方式有
    1.  function f( x ){
             return x*x;
         };
         f(x);

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

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

 其中2和3是定義匿名函數的方式。方式可以理解成
        var f = function( x ) { return x*x; };
        f( x );
這三中方式的關係如下
  ( function( x ){ return x*x; } ) ( x ); == ( function( x ) { return x*x; } ( x ) ); == function f( x ) { return x*x; }; f(x);
但是他們還是有區別的,
首先,對於像第二和第三種形式,其它的函數和代碼是不可能調用所定義的函數的,所以把這樣的函數稱為匿名函數或者函數直接量。
其次,第二和第三種形式執行的函數,中間變數不會汙染到全域命名空間,你可以把中間的代碼看作純粹的子程序呼叫。
當然使用後面兩種形式的函數定義可以很容易的實現閉包。

例子
<script type="text/javascript">
/*
  http://jibbering.com/faq/faq_notes/closures.html(Dnew.CN注)
  A global variable - getImgInPositionedDivHtml - is declared and
  assigned the value of an inner function expression returned from
  a one-time call to an outer function expression.

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

      Empty strings are used as placeholders for the date that is to
      be inserted into the Array by the inner function:-
   */
   var buffAr = [
       '<div id="',
       '',   //index 1, DIV ID attribute
       '" style="position:absolute;top:',
       '',   //index 3, DIV top position
       'px;left:',
       '',   //index 5, DIV left position
       'px;width:',
       '',   //index 7, DIV width
       'px;height:',
       '',   //index 9, DIV height
       'px;overflow:hidden;/"><img src=/"',
       '',   //index 11, IMG URL
       '/" width=/"',
       '',   //index 13, IMG width
       '/" height=/"',
       '',   //index 15, IMG height
       '/" alt=/"',
       '',   //index 17, IMG alt text
       '/"><//div>'
   ];
   /* Return the inner function object that is the result of the
      evaluation of a function expression. It is this inner function
      object that will be executed on each call to -
      getImgInPositionedDivHtml( ... ) -:-
   */
   return (function(url, id, width, height, top, left, altText){
               /* Assign the various parameters to the corresponding
                  locations in the buffer array:-
               */
               buffAr[1] = id;
               buffAr[3] = top;
               buffAr[5] = left;
               buffAr[13] = (buffAr[7] = width);
               buffAr[15] = (buffAr[9] = height);
               buffAr[11] = url;
               buffAr[17] = altText;
               /* Return the string created by joining each element in the
                  array using an empty string (which is the same as just
                  joining the elements together):-
               */
               return buffAr.join('');
           }); //:End of inner function expression.
})();
/*^^- :The inline execution of the outer function expression. */
</script>

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.