(转载)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 相关文章推荐
jQuery 遍历- 关于closest() 的方法介绍以及与parents()的方法区别分析
Apr 26 Javascript
jQuery中:nth-child选择器用法实例
Dec 31 Javascript
vue之父子组件间通信实例讲解(props、$ref、$emit)
May 22 Javascript
在 Angular6 中使用 HTTP 请求服务端数据的步骤详解
Aug 06 Javascript
vue下history模式刷新后404错误解决方法
Aug 18 Javascript
vue.js项目 el-input 组件 监听回车键实现搜索功能示例
Aug 25 Javascript
微信小程序开发之左右分栏效果的实例代码
May 20 Javascript
微信小程序自定义弹窗实现详解(可通用)
Jul 04 Javascript
JavaScript基础之this和箭头函数详析
Sep 05 Javascript
浅谈Webpack4 Tree Shaking 终极优化指南
Nov 18 Javascript
vue 全局封装loading加载教程(全局监听)
Nov 05 Javascript
基于VUE实现简单的学生信息管理系统
Jan 13 Vue.js
阻止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
PHP数据库开发知多少
2006/10/09 PHP
PHP中的命名空间相关概念浅析
2015/01/22 PHP
简单介绍PHP非阻塞模式
2016/03/03 PHP
风吟的小型JavaScirpt库 (FY.JS).
2010/03/09 Javascript
xml转json的js代码
2012/08/28 Javascript
Jquery 模板数据绑定插件的使用方法详解
2013/07/08 Javascript
JavaScript中的document.referrer在各种浏览器测试结果
2014/07/18 Javascript
jQuery判断checkbox选中状态
2016/05/12 Javascript
15位和18位身份证JS校验的简单实例
2016/07/18 Javascript
引用jquery框架后出错的解决方法
2016/08/09 Javascript
ionic实现可滑动的tab选项卡切换效果
2020/04/15 Javascript
AngulaJS路由 ui-router 传参实例
2017/04/28 Javascript
利用node.js实现反向代理的方法详解
2017/07/24 Javascript
vue中本地静态图片路径写法
2018/03/06 Javascript
微信小程序签到功能
2018/10/31 Javascript
Vue 动态组件与 v-once 指令的实现
2019/02/12 Javascript
Vue实现滑动拼图验证码功能
2019/09/15 Javascript
js实现随机抽奖
2020/03/19 Javascript
[38:54]完美世界DOTA2联赛PWL S2 Rebirth vs LBZS 第一场 11.28
2020/12/01 DOTA
python dataframe常见操作方法:实现取行、列、切片、统计特征值
2018/06/09 Python
浅谈关于Python3中venv虚拟环境
2018/08/01 Python
Python使用gRPC传输协议教程
2018/10/16 Python
python自制包并用pip免提交到pypi仅安装到本机【推荐】
2019/06/03 Python
python中的split、rsplit、splitlines用法说明
2020/10/23 Python
python中lower函数实现方法及用法讲解
2020/12/23 Python
浅谈html5之sse服务器发送事件EventSource介绍
2017/08/28 HTML / CSS
Html5适配iphoneX刘海屏的简单实现
2019/04/09 HTML / CSS
Perfume’s Club意大利官网:欧洲美妆电商
2019/05/03 全球购物
介绍一下HDLC(High-Level Data Link Control)高层数据链路协议
2012/01/21 面试题
会计专业大学生职业生涯规划范文
2014/01/11 职场文书
个人四风问题对照检查材料思想汇报
2014/10/06 职场文书
政风行风评议心得体会
2014/10/21 职场文书
golang中的空slice案例
2021/04/27 Golang
MySQL大小写敏感的注意事项
2021/05/24 MySQL
Python 发送SMTP邮件的简单教程
2021/06/24 Python
Python可视化神器pyecharts之绘制箱形图
2022/07/07 Python