(转载)JavaScript中匿名函数,函数直接量和闭包


Posted in Javascript onMay 08, 2007

原文出处: 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));

第一种我们应该都很熟悉了,这是我们经常使用的写法。第二第三种都是匿名函数的写法。

--------------------------------------------------------------------------------

第二种
可以这样理解:

var f=function(x) {return x*x;};f()

那我们不通过f这个变量来引用函数就是

function(){}()

然而这样肯定是错误的就像

var f=1+2;
f=f*0;

var f=1+2*0;

结果不同一样。
要得到正确结果只能:

f=(1+2)*0;

也就是要明确的标识出程序块,即:

(function(){})()

肯你有疑问:括号“()”到底是不是起到了标识代码块的作用?
我们可以用JavaScript的内置函数检测一下!
举一个最简单的例子:

alert(4)

这段代码会弹出提示内容是“4”
改成这样
(alert)(4)

可以看到执行的效果和上一段代码一样。

这种形式的函数执行也被很多JavaScript框架所采用。

--------------------------------------------------------------------------------

第三种,如果你用过jsvm框架的话就会发现里面的代码使用了这种形式。
那如何解释第三种情况呢?
为了弄明白浏览器是如何理解这样的写法的,我们可以利用一下Mozilla Firefox的错误控制台功能。
在代码中插入一段错误代码,代码段如下:

(function(s){s+s}(1)).splice();

打开Mozilla Firefox的错误控制台,可以看到有如下的错误提示

错误: (function (s) {})(1) has no properties
源文件:file:///C:/Documents…….html
行:18

可以认为,浏览器对于
(function(s){s+s}(1))
这样的代码按照

(function (s) {s+s})(1)
来解析的。

--------------------------------------------------------------------------------

到此可能你有这样的认识:

function f(x){return x*x;};f(x);==(function(x){return x*x;})(x);==(function(x){return x*x;}(x));

但是他们还是有区别的,
首先,对于像第二和第三种形式,其它的函数和代码是不可能调用所定义的函数的,有一种说发把这样的函数称为匿名函数或者函数直接量。
其次,第二和第三种形式执行的函数,中间变量不会污染到全局命名空间,你可以把中间的代码看作纯粹的子过程调用。
当然使用后面两种形式的函数定义可以很容易的实现闭包。
看一个例子:

/*
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. */

Javascript 相关文章推荐
Prototype使用指南之array.js
Jan 10 Javascript
基于JQuery的Pager分页器实现代码
Jul 17 Javascript
超酷的网页音乐播放器DewPlayer使用方法
Dec 18 Javascript
jQuery EasyUI API 中文文档 - Menu菜单
Oct 03 Javascript
js复制网页内容并兼容各主流浏览器的代码
Dec 17 Javascript
JavaScript中循环遍历Array与Map的方法小结
Mar 12 Javascript
Google 地图API资料整理及详细介绍
Aug 06 Javascript
常用Javascript函数与原型功能收藏(必看篇)
Oct 09 Javascript
vue elementUI tree树形控件获取父节点ID的实例
Sep 12 Javascript
小程序异步问题之多个网络请求依次执行并依次收集请求结果
May 05 Javascript
vue router总结 $router和$route及router与 router与route区别
Jul 05 Javascript
Javascript摸拟自由落体与上抛运动原理与实现方法详解
Apr 08 Javascript
阻止JavaScript事件冒泡传递(cancelBubble 、stopPropagation)
May 08 #Javascript
延时重复执行函数 lLoopRun.js
May 08 #Javascript
用js判断浏览器是否是IE的比较好的办法
May 08 #Javascript
[全兼容哦]--实用、简洁、炫酷的页面转入效果loing
May 07 #Javascript
javascript之水平横向滚动歌词同步的应用
May 07 #Javascript
javascript之ESC(第二类混淆)
May 06 #Javascript
通过Unicode转义序列来加密,按你说的可以算是混淆吧
May 06 #Javascript
You might like
造就帕卡马拉的帕卡斯是怎么被发现的
2021/03/03 咖啡文化
高性能PHP框架Symfony2经典入门教程
2014/07/08 PHP
几道坑人的PHP面试题 试试看看你会不会也中招
2014/08/19 PHP
PHP的运行机制与原理(底层)
2015/11/16 PHP
PHP环形链表实现方法示例
2017/09/15 PHP
Laravel 修改默认日志文件名称和位置的例子
2019/10/17 PHP
php设计模式之原型模式分析【星际争霸游戏案例】
2020/03/23 PHP
关于Jqzoom的使用心得 jquery放大镜效果插件
2010/04/12 Javascript
理解Javascript_10_对象模型
2010/10/16 Javascript
js实现全屏漂浮广告移入光标停止移动
2013/12/02 Javascript
jquery获取复选框被选中的值
2014/04/10 Javascript
jQuery焦点控制图层展示延迟隐藏的方法
2015/03/09 Javascript
js实现Select列表内容自动滚动效果代码
2015/08/20 Javascript
理解JS事件循环
2016/01/07 Javascript
JavaScript中boolean类型之三种情景实例代码
2016/11/21 Javascript
webpack4简单入门实例
2018/09/06 Javascript
js实现浏览器打印功能的示例代码
2020/07/15 Javascript
[49:41]NB vs NAVI Supermajor小组赛A组 BO3 第一场 6.2
2018/06/03 DOTA
[20:30]职业巡回赛回顾
2018/08/09 DOTA
python3.5+tesseract+adb实现西瓜视频或头脑王者辅助答题
2018/01/17 Python
python3 requests中使用ip代理池随机生成ip的实例
2018/05/07 Python
使用Python画股票的K线图的方法步骤
2019/06/28 Python
python由已知数组快速生成新数组的方法
2020/04/08 Python
python3检查字典传入函数键是否齐全的实例
2020/06/05 Python
美国单身专业人士在线约会网站:EliteSingles
2019/03/19 全球购物
Charles & Keith欧盟:新加坡时尚品牌
2019/08/01 全球购物
英国排名第一的宠物店:PetPlanet
2020/02/02 全球购物
普通院校学生的自荐信
2013/11/27 职场文书
2014年开学第一课活动方案
2014/03/06 职场文书
竞选班长演讲稿500字
2014/08/22 职场文书
中学生2014国庆节演讲稿:不屈的民族
2014/09/21 职场文书
实习单位证明范例
2014/11/17 职场文书
万里长城导游词
2015/01/30 职场文书
会计简历自我评价
2015/03/10 职场文书
婚礼上证婚人致辞
2015/07/28 职场文书
七年级话题作文之执着
2019/11/19 职场文书