惯性聚合 高效追踪和阅读你感兴趣的博客、新闻、科技资讯
阅读原文 在惯性聚合中打开

推荐订阅源

Martin Fowler
Martin Fowler
CTFtime.org: upcoming CTF events
CTFtime.org: upcoming CTF events
A
About on SuperTechFans
Apple Machine Learning Research
Apple Machine Learning Research
The Register - Security
The Register - Security
Vercel News
Vercel News
H
Hackread – Cybersecurity News, Data Breaches, AI and More
人人都是产品经理
人人都是产品经理
MyScale Blog
MyScale Blog
云风的 BLOG
云风的 BLOG
博客园_首页
U
Unit 42
T
Tailwind CSS Blog
G
GRAHAM CLULEY
F
Full Disclosure
V
Vulnerabilities – Threatpost
T
Tenable Blog
月光博客
月光博客
P
Privacy & Cybersecurity Law Blog
P
Privacy International News Feed
K
Kaspersky official blog
Scott Helme
Scott Helme
cs.AI updates on arXiv.org
cs.AI updates on arXiv.org
N
News and Events Feed by Topic
T
The Exploit Database - CXSecurity.com
N
News and Events Feed by Topic
有赞技术团队
有赞技术团队
Recent Commits to openclaw:main
Recent Commits to openclaw:main
L
LINUX DO - 最新话题
Recorded Future
Recorded Future
Application and Cybersecurity Blog
Application and Cybersecurity Blog
Help Net Security
Help Net Security
The GitHub Blog
The GitHub Blog
Cisco Talos Blog
Cisco Talos Blog
SecWiki News
SecWiki News
P
Proofpoint News Feed
Security Latest
Security Latest
cs.CL updates on arXiv.org
cs.CL updates on arXiv.org
罗磊的独立博客
S
Security Affairs
M
MIT News - Artificial intelligence
L
LINUX DO - 热门话题
美团技术团队
Simon Willison's Weblog
Simon Willison's Weblog
T
Threat Research - Cisco Blogs
Stack Overflow Blog
Stack Overflow Blog
Forbes - Security
Forbes - Security
Hugging Face - Blog
Hugging Face - Blog
博客园 - Franky
V
Visual Studio Blog

博客园 - 令狐葱★

【备忘】12306购票必杀技 Github上最受关注的前端大牛,快来膜拜吧! 那些开源程序中让人叹为观止的代码 - 3 保持元素纵横比 那些开源程序中让人叹为观止的代码 - 2 单例模式 那些开源程序中让人叹为观止的代码 - 1 浏览器特性判断 浏览器中F5和CTRL F5的行为区别 CR, LF, CR/LF区别与关系 利用 jQuery 克隆 Object Chrome下Failed to load resource问题居然是由于AdBlock WordPress在线安装插件 jQuery中的.height()、.innerHeight()和.outerHeight() jQuery中的attr()和prop() jQuery中的事件绑定函数.bind()、.live()、on()和.delegate() 实现ECMAScript 5中的Object.create()函数和Object.getPrototypeOf() 函数 Javascript全局变量和delete jQuery中$.proxy()的原理和使用 Javascript基类对象原型中有数组的情况 必须关注的25位知名JavaScript开发者 大坑贴:CSS浏览器兼容性问题及bug汇总
Javascript知识点:IIFE - 立即调用函数
令狐葱★ · 2014-05-29 · via 博客园 - 令狐葱★

Immediately-invoked Function Expression(IIFE,立即调用函数),简单的理解就是定义完成函数之后立即执行。因此有时候也会被称为“自执行的匿名函数”(self-executing anonymous function)。

IIFE的叫法最早见于Ben Alman的文章。文章中Ben Alman 已经解释得很清楚了,希望定义自执行函数式常见的语法错误有两种:

1) function (){ }()

期望是立即调用一个匿名函数表达式,结果是进行了函数声明,函数声明必须要有标识符做为函数名称。

2) function g(){ }()

期望是立即调用一个具名函数表达式,结果是声明了函数 g。末尾的括号作为分组运算符,必须要提供表达式做为参数。
所以那些匿名函数附近使用括号或一些一元运算符的惯用法,就是来引导解析器,指明运算符附近是一个表达式。

按照这个理解,可以举出五类,超过十几种的让匿名函数表达式立即调用的写法:

1)使用括号

( function() {}() );
( function() {} )();
[ function() {}() ];

2)使用一元操作符

~ function() {}();
! function() {}();
+ function() {}();
- function() {}();

3)使用void等操作符

delete function() {}();
typeof function() {}();
void function() {}();

4)使用表达式

var i = function(){ return 10; }();
14.true && function(){ /* code */ }();
15.0, function(){ /* code */ }();
1 ^ function() {}();
1 > function() {}();

5)使用new关键字

new function(){  }

31.new function(){  }() 

但是总体来说,比较常见的是如下三种写法:


(function() {
  console.log('Welcome to the Internet. Please follow me.');
}()); 

(function() {
  console.log('Welcome to the Internet. Please follow me.'); 

})(); 

!function() {

  console.log('Welcome to the Internet. Please follow me.'); 

}(); 

其实讨论IIFE的多少种写法多少和研究茴香豆的“茴”字有几种写法一样无聊,但其实不无用处,至少在阅读别人的代码时见到这样的写法不至于不知所云,抑或可以拿出去和小伙伴们装装,顿时觉得逼格提升不少。

参考资料:

http://benalman.com/news/2010/11/immediately-invoked-function-expression/(中文译文:http://www.cnblogs.com/TomXu/archive/2011/12/31/2289423.html

http://www.elijahmanor.com/angry-birds-of-javascript-red-bird-iife/(中文译文:http://nuysoft.com/2013/04/15/angry-birds-of-javascript-red-bird-iife/Immediately-invoked%20Function%20Expression

http://www.zhihu.com/question/20249179