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

推荐订阅源

P
Palo Alto Networks Blog
Recent Commits to openclaw:main
Recent Commits to openclaw:main
C
CERT Recently Published Vulnerability Notes
C
Cybersecurity and Infrastructure Security Agency CISA
S
Schneier on Security
S
Securelist
酷 壳 – CoolShell
酷 壳 – CoolShell
C
CXSECURITY Database RSS Feed - CXSecurity.com
Cyberwarzone
Cyberwarzone
Apple Machine Learning Research
Apple Machine Learning Research
S
SegmentFault 最新的问题
cs.CL updates on arXiv.org
cs.CL updates on arXiv.org
GbyAI
GbyAI
Security Latest
Security Latest
Last Week in AI
Last Week in AI
Microsoft Security Blog
Microsoft Security Blog
云风的 BLOG
云风的 BLOG
Recorded Future
Recorded Future
Webroot Blog
Webroot Blog
cs.AI updates on arXiv.org
cs.AI updates on arXiv.org
TaoSecurity Blog
TaoSecurity Blog
C
Cisco Blogs
博客园 - 【当耐特】
Blog — PlanetScale
Blog — PlanetScale
Hugging Face - Blog
Hugging Face - Blog
B
Blog
Hacker News - Newest:
Hacker News - Newest: "LLM"
cs.CV updates on arXiv.org
cs.CV updates on arXiv.org
Attack and Defense Labs
Attack and Defense Labs
The Last Watchdog
The Last Watchdog
U
Unit 42
阮一峰的网络日志
阮一峰的网络日志
Project Zero
Project Zero
WordPress大学
WordPress大学
L
LINUX DO - 最新话题
F
Fortinet All Blogs
L
LINUX DO - 热门话题
PCI Perspectives
PCI Perspectives
Simon Willison's Weblog
Simon Willison's Weblog
Threat Intelligence Blog | Flashpoint
Threat Intelligence Blog | Flashpoint
MongoDB | Blog
MongoDB | Blog
Latest news
Latest news
P
Proofpoint News Feed
T
Threat Research - Cisco Blogs
The Hacker News
The Hacker News
爱范儿
爱范儿
O
OpenAI News
J
Java Code Geeks
T
The Exploit Database - CXSecurity.com
H
Hackread – Cybersecurity News, Data Breaches, AI and More

博客园 - 令狐葱★

【备忘】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