JavaScript中匿名函数

在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>

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值