Javascript---Immediately-Invoked Function Expression (IIFE)立即执行的函数表达式

1.一下是几种形式的函数调用:

各种调用的效率:在这编文章中有谈到:

http://suqing.iteye.com/blog/1981591

// Either of the following two patterns can be used to immediately invoke
// a function expression, utilizing the function's execution context to
// create "privacy."(function(){ /* code */ }()); // Crockford recommends this one
(function(){ /* code */ })(); // But this one works just as well// Because the point of the parens or coercing operators is to disambiguate
// between function expressions and function declarations, they can be
// omitted when the parser already expects an expression (but please see the
// "important note" below).var i = function(){ return 10; }();
true && function(){ /* code */ }();
0, function(){ /* code */ }();// If you don't care about the return value, or the possibility of making
// your code slightly harder to read, you can save a byte by just prefixing
// the function with a unary operator.!function(){ /* code */ }();
~function(){ /* code */ }();
-function(){ /* code */ }();
+function(){ /* code */ }();// Here's another variation, from @kuvos - I'm not sure of the performance
// implications, if any, of using the `new` keyword, but it works.
// http://twitter.com/kuvos/status/18209252090847232new function(){ /* code */ }
new function(){ /* code */ }() // Only need parens if passing arguments

 

2.What’s wrong with “Self-executing anonymous function?”

这种即使调用的函数表达式(IIFE),并不一定是匿名的,所以 JavaScript community members这个别称“Self-executing anonymous function”有令人误解的地方。

// This is a self-executing function. It's a function that executes (or
// invokes) itself, recursively:function foo() { foo(); }// This is a self-executing anonymous function. Because it has no
// identifier, it must use the  the `arguments.callee` property (which
// specifies the currently executing function) to execute itself.var foo = function() { arguments.callee(); };// This *might* be a self-executing anonymous function, but only while the
// `foo` identifier actually references it. If you were to change `foo` to
// something else, you'd have a "used-to-self-execute" anonymous function.var foo = function() { foo(); };// Some people call this a "self-executing anonymous function" even though
// it's not self-executing, because it doesn't invoke itself. It is
// immediately invoked, however.(function(){ /* code */ }());// Adding an identifier to a function expression (thus creating a named
// function expression) can be extremely helpful when debugging. Once named,
// however, the function is no longer anonymous.(function foo(){ /* code */ }());// IIFEs can also be self-executing, although this is, perhaps, not the most
// useful pattern.(function(){ arguments.callee(); }());
(function foo(){ foo(); }());// One last thing to note: this will cause an error in BlackBerry 5, because
// inside a named function expression, that name is undefined. Awesome, huh?(function foo(){ foo(); }());

3.A final aside: The Module Pattern

javascript的模块实现模式,是返回一个Object而不是函数。模块化有相关的方法和属性放在一个命名空间里,组织好整个模块的代码,降低了全局性变量的污染。

下面是一个例子:

 

// Create an anonymous function expression that gets invoked immediately,
// and assign its *return value* to a variable. This approach "cuts out the
// middleman" of the named `makeWhatever` function reference.
// 
// As explained in the above "important note," even though parens are not
// required around this function expression, they should still be used as a
// matter of convention to help clarify that the variable is being set to
// the function's *result* and not the function itself.var counter = (function(){var i = 0;return {get: function(){return i;},set: function( val ){i = val;},increment: function() {return ++i;}};
}());// `counter` is an object with properties, which in this case happen to be
// methods.counter.get(); // 0
counter.set( 3 );
counter.increment(); // 4
counter.increment(); // 5counter.i; // undefined (`i` is not a property of the returned object)
i; // ReferenceError: i is not defined (it only exists inside the closure)

 

原文参考:http://benalman.com/news/2010/11/immediately-invoked-function-expression/

转载于:https://www.cnblogs.com/IanI/p/4021956.html

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.mzph.cn/news/398946.shtml

如若内容造成侵权/违法违规/事实不符,请联系多彩编程网进行投诉反馈email:809451989@qq.com,一经查实,立即删除!

相关文章

extern 使用方法具体解释

在C语言中,修饰符extern用在变量或者函数的声明前,用来说明“此变量/函数是在别处定义的。要在此处引用”。(extern能够置于变量或者函数前,以标示变量或者函数的定义在别的文件里,提示编译器遇到此变量和函数时在其它…

.NET常用工具类集锦

不错的地址: http://www.cnblogs.com/flashbar/archive/2013/01/23/helper.html https://github.com/chrisyanghua/MyHelper/tree/master/MyHelper http://www.cnblogs.com/conan87810/archive/2009/03/15/1412529.html http://www.cnblogs.com/ltp/archive/2008/03…

现代控制理论-李雅普诺夫

现代控制理论-李雅普诺夫 单输入单输出系统(BIBO)的系统函数如下: 则,该系统的能控标准型(能空性)为: 能观性: 李雅普诺夫下的稳定性: 李雅普诺夫下的渐进稳定性&a…

Linux服务器的gou,开源跨平台移动项目Langou【简介】

Langou简介Langou是一个跨平台(Android/iOS)前端开发框架,核心代码使用C编写,底层基于OpenGL绘图,上层实现了一个精简的排版引擎以及一个JS/JSX运行环境。目标是想实现在此基础上开发GUI应用程序可兼顾开发速度与运行效率。暂时只支持iOS与An…

caffe 错误

一些caffe错误 训练时很快梯度爆炸,loss猛增至nan 如果找不到数据上的原因的话,可以怀疑caffe框架有问题,换用其它版本试试。比如我遇到的问题是在训练时使用了Accuracy层,而该层的实现代码在某次更新中GPU代码存在bug&#xff0c…

发手气红包算法

lowest0.01元,最小金额 操作是整数,最小人民币单位是分,所以有2位小数,最少是0.01元 发金额totalBill 发n人,就取1到100的随机数n个 为了提高精度,把金额放大100倍 totalAmounttotalBill*100 n个人各取的随…

cs106a编程方法学作业解答(3)

此次作业要求我们做一个简单的打砖块游戏。 1 * File: Breakout.java2 * -------------------3 * Name:4 * Section Leader:5 * 6 * This file will eventually implement the game of Breakout.7 */8 9 import acm.graphics.*;10 import acm.program.*;11 import acm.ut…

vue从入门到进阶:简介(一)

前言 用了这么久的vue了,但是一直没有时间写个系列文章,现在抽一定时间总结下vue的知识点。 首先,Vue 不支持 IE8 及以下版本,因为 Vue 使用了 IE8 无法模拟的 ECMAScript 5 特性。但它支持所有兼容 ECMAScript 5 的浏览器。下面总…

linux堡垒机开源软件,Jumpserver开源堡垒机

Jumpserver开源跳板机系统部署1.简介Jumpserver使用Python / Django进行开发,遵循Web 2.0规范,配备了业界领先的Web Terminal解决方案,交互界面美观、用户体验好。Jumpserver采纳分布式架构,支持多机房跨区域部署,中心…

node截图服务可用性报告

2019独角兽企业重金招聘Python工程师标准>>> 前言 服务器端截图可以做什么? 个人观点:省去跟报表有关的EDM开发,直接从系统上截图,然后发图片给用户就搞定。剩下的自己脑补。 既然这么好,为毛不赶紧弄。…

C++的extern关键字

extern是一个声明,不是一个定义,A模块想应用B模块的一个函数或者变量,A模块包含B模块的头文件,并且在变量或者头文件前,加 extern,虽然编译的时候,找不到模块的定义,但是在连接的时候…

Linux网站访问的电脑占CPU,详解Linux如何查看当前占用CPU或内存最多的几个进程...

命令ps -aux | sort -k4nr | head -N命令详解:1、head:-N可以指定显示的行数,默认显示10行。2、ps:参数a指代all——所有的进程,u指代userid——执行该进程的用户id,x指代显示所有程序,不以终端…

Hi,博客园

Hi,博客园! 这是我在博客园的第一篇博文,主要是为了测试发布。 在以后的日子里我会陆续介绍ArcGIS soft的使用及Flex和JS的开发,以及开源GIS的开发,敬请关注。转载于:https://www.cnblogs.com/unitgis/p/4028171.html

java反射快速入门(二)

上一遍博文 , 简单介绍java 反射的常用接口,本遍博文, 我会结合项目开发的实际例子讲解下 java反射的使用 现在有个需求, 要将一个对象转换成xml格式, 或者将一串xml转换一个对象, 这时我们循序渐进, 先从最简单的入手 一: 方案① 场景 : NBA球员信息描述…

Myecplise Tomcat 启动很慢

今天突然遇到一个问题,tomcat在Myecplse启动非常慢,直接用tomcat自带的start.bat启动很快,如果通过Myeclipse启动会发现项目一直在实例化,最后发现是因为加了断点调试,断点忘了去掉了,这个恰恰影响了spring…

C#QQ邮箱验证

注意: QQ邮箱的简单邮件传输协议(SMTP)使用了SSL加密,必须启用SSL加密、指定端口。 QQ邮箱POP3/SMTP服务默认是关闭的,需要开启服务(设置>账户>开启服务)。 QQ邮箱若有独立密码&#xff0…

linux 文件 重命名 缓存,linux – rename()原子性和NFS?

参考:Is rename() atomic?我问的是类似的东西,但不完全相同,因为我想知道的是在使用NFS时依赖于rename()的原子性是否安全?这是我正在处理的一个场景 – 我有一个必须始终存在的’索引’文件.所以:>客户端创建一个新文件>客户端通过“…

Win2008上.NET4.0部署出错HTTP 错误 500.21 - Internal Server Error的解决方法

原因:在安装Framework v4.0之后,再启用IIS,导致Framework没有完全安装 解决:开始->所有程序->附件->鼠标右键点击“命令提示符”->以管理员身份运行,输入以下命令: %windir%\\Microsoft.NET\\F…

ORACLE DATAGURARD配置手记

经过多次实践,参阅网上N多文章……最后还是配不成,可能本人悟性太低,无法体会高手的笔记。最终还是在前辈的帮助下完成。特用最平实的手法记录下来,以便如吾辈菜鸟能 看得懂。 运行Data Guard的条件 1、 在主库和从库的所有机器上…

浅谈ASP.NET框架

本篇文章更适合具有一定开发经验,一定功底,且对底层代码有所研究的朋友!!! 本篇文章稍微偏原理且底层,有一定难度和且比较晦涩,文章粒度稍微粗些,更细粒度的,会在后续的文…