胰腺炎吃什么水果| 舒服是什么意思| 吃孕酮片有什么副作用| 什么的脸庞| 行尸走肉是什么动物| 尚公主是什么意思| 盛世的意思是什么| 心火吃什么药| minute是什么意思| 副市长什么级别| 有什么功效| 枸杞与菊花一起泡水喝有什么功效| zxj是什么意思| 自言自语说话是什么病| 试营业是什么意思| 双规是什么| 卵巢囊性结构是什么| 忠于自己是什么意思| 辩证什么意思| 丙氨酸氨基转移酶是查什么的| 氢键是什么| 鸟来家里预示什么| 头晕挂什么科室| 出汗少的人是什么原因| 女生真空是什么意思| 03年属什么生肖| 低血压是什么原因引起的| 腋下疼痛挂什么科| 譬如是什么意思| 为什么叫关东军| 功能性子宫出血是什么原因造成的| 女人熬夜吃什么抗衰老| 蜕变是什么意思| 男性吃什么增强性功能| 唯爱是什么意思| 不割包皮有什么影响| 检查肺部应该挂什么科| 乳酸杆菌是什么| 心神不宁是什么意思| 悸动什么意思| 什么是零售| 大云是什么中药| 胃发胀是什么原因| 狐臭用什么药| 十月二十五是什么星座| 喝什么去火| 哺乳期吃避孕药对孩子有什么影响| fgr医学上是什么意思| 孔雀蓝配什么颜色好看| 遍布是什么意思| 家里养什么鱼好| 肚子里有积水是什么病| 肠道胀气吃什么药| 屁股出血是什么原因| generic是什么意思| 乙肝五项15阳性是什么意思| 为什么老是打喷嚏| 桦树茸泡水喝有什么功效| 5.4是什么星座| 艾玛是什么意思啊| 肠胃不好吃什么水果好| 与生俱来是什么意思| 小米是什么米| 瀹是什么意思| 我国计划生育什么时候开始| 什么地唱歌| 虾仁炒什么| 棒棒糖是什么意思| 什么是企业年金| 不着相是什么意思| 布鲁斯是什么| 肾萎缩吃什么药好| 金兰之交是什么意思| 尿酸高的人不能吃什么| 什么寒什么冻| 区武装部部长是什么级别| 无名指麻木是什么原因| 吃什么补内膜最快| 阳虚湿热吃什么中成药| 麻雀吃什么| 大便粗大是什么原因| 7月26日是什么日子| 睡醒咳嗽是什么原因| 衣原体是什么| 白藜芦醇是什么东西| 丹参长什么样子图片| 肺部纹理增粗是什么意思| 里急后重什么意思| 洋葱与什么食物相克| 佛跳墙是什么意思| 肝浸润是什么意思| 庭字五行属什么| 陈皮有什么作用| 梅尼埃综合症是什么病| 做酸菜鱼用什么鱼| 血常规能查出什么| 宝宝乳糖不耐受喝什么奶粉比较好| 米黄是什么颜色| 树欲静而风不止什么意思| 混合痔是什么意思| 偶发性房性早搏是什么意思| 2049年是什么年| 什么人不用电| 74年属什么的生肖| 人心叵测什么意思| 薛字五行属什么| 粉刺是什么| 脾肾两虚吃什么中成药| 百折不挠的意思是什么| 衿字五行属什么| 什么时候放开二胎| 万条垂下绿丝绦是什么季节| 沁什么意思| 结婚长明灯有什么讲究| wb是什么意思| 甲鱼喜欢吃什么食物| 囊变是什么意思| 心脏是由什么组织构成的| cpc什么意思| pms是什么意思| 豆腐干炒什么菜好吃| 化验肝功挂什么科| 夏天喝什么汤| 肌酸激酶高吃什么药| 脚心痒是什么预兆| 不能吃辣是什么原因| 无利不起早是什么意思| 为什么我| 火险痣是什么意思| 蛋白质用什么试剂鉴定| 什么鱼最好养不容易死| 动土是什么意思| 梦见下大雪是什么意思| 沉沦什么意思| 潮热盗汗是什么意思| 复出是什么意思| 眉毛上长痘是什么原因| 首饰是什么意思| 头皮长痘痘是什么原因| 心慌气短吃什么药| 什么是贵妇脸| 喉咙痒痒的吃什么药| 何首乌长什么样子图片| 面红耳赤是什么意思| 月青念什么| 绿豆汤不能和什么一起吃| 被动是什么意思| 兔子的眼睛是什么颜色| 芋头是什么| 经常偏头疼是什么原因| 缠腰蛇是什么症状图片| 看头部挂什么科| 什么治咳嗽最快最有效| 米酒不甜是什么原因| 四面受敌是什么动物| 为什么印度叫阿三| 早上起床口臭是什么原因| 老娘们是什么意思| 红细胞分布宽度偏高是什么意思| 今天适合穿什么衣服| 梦见吃核桃是什么意思| 麻雀长什么样| 行政工作主要负责什么| 四维彩超是检查什么| 女性黄体期是什么意思| 碱性食物对身体有什么好处| 误区是什么意思| 刘五行属性是什么| 口腔溃疡缺乏什么维生素| 豆腐干炒什么菜好吃| 嘴巴右下角有痣代表什么| 幽门螺旋杆菌有什么症状| 子宫内膜异位症是什么意思| 吃维生素b12有什么好处和副作用| 什么叫继发性高血压| 执子之手与子偕老什么意思| 甲醛对人体有什么危害| 高血脂挂什么科| 1996五行属什么| 放疗和化疗有什么区别| 减肥早上吃什么比较好| 肠炎能吃什么水果| 诺欣妥是什么药| 便秘喝什么茶| 第一次见女方家长带什么礼物好| 脚肿腿肿是什么原因引起的| 勿忘我是什么意思| 胎心不稳定是什么原因| 什么是伤官见官| 胎儿腹围偏大说明什么| 什么叫二氧化碳| 溢于言表什么意思| 泡打粉是什么东西| 活跃是什么意思| 墨鱼和鱿鱼有什么区别| 宝宝发烧是什么原因引起的| 梦见迁祖坟有什么预兆| 家里为什么会有蟑螂| 鼠的本命佛是什么佛| 射进去有什么感觉| 肌腱属于什么组织| 自愿离婚要带什么证件| oct是什么意思| 表现手法有什么| 眼睛不舒服是什么原因引起的| 肝炎是什么| 羊癫疯有什么症状表现| 为什么来月经会拉肚子| 手指指尖发麻是什么原因| 不结婚的叫什么族| 710是什么意思| 生抽和酱油有什么区别| 梦见买车是什么意思| 骑单车锻炼什么好处| 什么光| 做胃镜挂什么科| 什么竹子| 有出息是什么意思| cartier什么牌子| 十恶不赦是什么意思| 拍胸片挂什么科| 吃什么可以瘦肚子| nfl是什么意思| 胃酸胃胀反酸水吃什么药| 寻常疣是什么原因造成的| 大便有血是什么原因男性| 除湿气用什么药| 上海是什么中心| 什么是血尿| 唯我独尊是什么意思| 草字头占读什么| 养肺补肺吃什么药最好| 橘色五行属什么| 乌克兰和俄罗斯为什么打仗| 频繁放屁是什么原因| 7月24号是什么星座| 脚趾长痣代表什么意思| 经常自言自语是什么原因| 尿毒症是什么原因导致的| 吃菠萝有什么好处| 为什么说金克木生财| 破是什么生肖| 什么眉什么眼| 高甘油三酯血症是什么意思| 冠带是什么意思| 大同古代叫什么| 45是什么生肖| 2000年是属什么生肖| 乌鸡白凤丸适合什么人吃| 石足念什么| 长脸型适合什么样的发型女| 为什么做着做着就软了| 家是什么| 什么叫肾功能不全| 排暖期出血是什么原因| 蚂蚁上树什么意思| 哈字五行属什么| 艾叶泡脚有什么好处| 扁桃体结石是什么原因引起的| 满江红属于什么植物| 30年属什么生肖| 迪奥口红属于什么档次| 冷面是什么面做的| 乡镇党委书记是什么级别| 百度Jump to content

就问你,这样的“深夜食堂”你去吗?

From Wikipedia, the free encyclopedia
百度 从大年初四就开始进入吃草状态的袁姗姗,简直给了节后胖三斤的大家一记暴击。

An immediately invoked function expression (or IIFE, pronounced "iffy", IPA /??f.i/) is a programming language idiom which produces a lexical scope using function scoping. It was popular in JavaScript[1] as a method of supporting modular programming before the introduction of more standardized solutions such as CommonJS and ES modules.[2]

Immediately invoked function expressions can be used to avoid variable hoisting from within blocks, protecting against polluting the global environment and simultaneously allowing public access to methods while retaining privacy for variables defined within the function. In other words, it wraps functions and variables, keeping them out of the global scope and giving them a local scope.

Usage

[edit]

Immediately invoked function expressions may be written in a number of different ways.[3] A common convention is to enclose the function expression – and optionally its invocation operator – with the grouping operator,[4] in parentheses, to tell the parser explicitly to expect an expression. Otherwise, in most situations, when the parser encounters the function keyword, it treats it as a function declaration (statement), and not as a function expression.[5][6]

(function () { /* ... */ })();
(function () { /* ... */ }());
(() => { /* ... */ })(); // With ES6 arrow functions (though parentheses only allowed on outside)

In contexts where an expression is expected, wrapping in parentheses is not necessary:

let f = function () { /* ... */ }();
true && function () { /* ... */ }();
0, function () { /* ... */ }();

Passing variables into the scope is done as follows:

(function(a, b) { /* ... */ })("hello", "world");
(function(a="hello", b="world") { /* ... */ })(); //also works

An initial parenthesis is one case where the automatic semicolon insertion (ASI) in JavaScript can cause problems; the expression is instead interpreted as a call to the last term on the preceding line. In some styles that omit optional semicolons, the semicolon is placed in front of the parenthesis, and is known as a defensive semicolon.[7][8] For example:

a = b + c
;(function () {
  // code
})();

...to avoid being parsed as c().

Examples

[edit]

The key to understanding design patterns such as IIFE is to realize that prior to ES6, JavaScript only featured function scope (thus lacking block scope), passing values by reference inside closures.[9] This is no longer the case, as the ES6 version of JavaScript implements block scoping using the new let and const keywords.[10]

// Before ES6: Creating a scope using an IIFE
var foo = 1;
var bar = 2;
(function(){
    var foo = 3;  // shadows the outer `foo`
    bar = 4;      // overwrites the outer `bar`
})();
console.log(foo, bar);  // 1 4

// Since ES6: Creating a scope using curly brackets in combination with let and const
const foo = 1;
let bar = 2;
{
    const foo = 3;  // shadows the outer `foo`
    bar = 4;        // overwrites the outer `bar`
}
console.log(foo, bar);  // 1 4

Evaluation context

[edit]

A lack of block scope means that variables defined inside (for example) a for loop will have their definition "hoisted" to the top of the enclosing function. Evaluating a function that depends on variables modified by the outer function (including by iteration) can be difficult. We can see this without a loop if we update a value between defining and invoking the function.[11]

let v, getValue;
v = 1;
getValue = function () { return v; };
v = 2;

getValue(); // 2

While the result may seem obvious when updating v manually, it can produce unintended results when getValue() is defined inside a loop.

Hereafter the function passes v as an argument and is invoked immediately, preserving the inner function's execution context.[12]

let v, getValue;
v = 1;
getValue = (function (x) {
    return function () { return x; };
})(v);
v = 2;

getValue();  // 1

This is equivalent to the following code:

let v, getValue;
v = 1;
function f(x) {
    return function () { return x; };
};
getValue = f(v);
v = 2;

getValue();  // 1

Establishing private variables and accessors

[edit]

IIFEs are also useful for establishing private methods for accessible functions while still exposing some properties for later use.[13] The following example comes from Alman's post on IIFEs.[1]

// "counter" is a function that returns an object with properties, which in this case are functions.
let counter = (function () {
    let i = 0;

    return {
        get: function () {
            return i;
        },
        set: function (val) {
            i = val;
        },
        increment: function () {
            return ++i;
        }
    };
})();

// These calls access the function properties returned by "counter".
counter.get();       // 0
counter.set(3);
counter.increment(); // 4
counter.increment(); // 5

If we attempt to access counter.i from the global environment, it will be undefined, as it is enclosed within the invoked function and is not a property of counter. Likewise, if we attempt to access i, it will result in an error, as we have not declared i in the global environment.

Terminology

[edit]

Originally known as a "self-executing anonymous function",[14] Ben Alman later introduced the current term IIFE as a more semantically accurate name for the idiom, shortly after its discussion arose on comp.lang.javascript.[1][15][16]

Notably, immediately invoked functions need not be anonymous inherently, and ECMAScript 5's strict mode forbids arguments.callee,[17] rendering the original term a misnomer.

See also

[edit]

References

[edit]
  1. ^ a b c Alman, Ben (15 November 2010). "Immediately Invoked Function Expressions". Archived from the original on 1 December 2017. Retrieved 18 January 2019.
  2. ^ McGinnis, Tyler (15 January 2019). "JavaScript Modules: From IIFEs to CommonJS to ES6 Modules". ui.dev. Retrieved 18 August 2021.
  3. ^ Lindley, Cody (2013). JavaScript Enlightenment. O'Reilly. p. 61. ISBN 978-1-4493-4288-3.
  4. ^ "Grouping operator". Mozilla Developer Network. 2 October 2023.
  5. ^ Zakas, Nicholas (2012). Maintainable JavaScript. O'Reilly. p. 44. ISBN 978-1-4493-2768-2.
  6. ^ Axel Rauschmayer. "ExploringJS".
  7. ^ "JavaScript Semicolon Insertion: Everything you need to know". 28 May 2010. Archived from the original on 2 October 2017.
  8. ^ Marohni?, Mislav (7 May 2010). "Semicolons in JavaScript are optional". Archived from the original on 8 August 2017.
  9. ^ Haverbeke, Marijn (2011). Eloquent JavaScript. No Starch Press. pp. 29–30. ISBN 978-1-59327-282-1.
  10. ^ Orendorff, Jason (31 Jul 2015). "ES6 In Depth: let and const". Mozilla Hacks – the Web developer blog. Mozilla. Retrieved 16 October 2024.
  11. ^ Alman, Ben. "simple-iife-example.js". Github. Retrieved 5 February 2013.
  12. ^ Otero, Cesar; Larsen, Rob (2012). Professional jQuery. John Wiley & Sons. p. 31. ISBN 978-1-118-22211-9.
  13. ^ Rettig, Pascal (2012). Professional HTML5 Mobile Game Development. John Wiley & Sons. p. 145. ISBN 978-1-118-30133-3.
  14. ^ Resig, John (2006). Pro JavaScript Techniques. Apress. p. 29. ISBN 978-1-4302-0283-7.
  15. ^ Osmani, Addy (2012). Learning JavaScript Design Patterns. O'Reilly. p. 206. ISBN 978-1-4493-3487-1.
  16. ^ Baagoe, Johannes. "Closing parenthesis in function's definition followed by its call". Retrieved 19 April 2010.
  17. ^ "Strict mode". Mozilla JavaScript Reference. Mozilla Developer Network. Retrieved 4 February 2013.
[edit]
骨密度z值是什么意思 acc是什么意思 宝批龙是什么意思 388是什么意思 惹上官司是犯了什么煞
雷达是什么 小孩子眼睛眨得很频繁是什么原因 什么叫保守治疗 博字五行属什么 纹身有什么讲究和忌讳
黄皮肤适合什么颜色的衣服 主意正是什么意思 考研都考什么 hpv检查前需要注意什么 肠腔积气是什么原因
急性肠胃炎吃什么水果 为什么会长黄褐斑 91年的羊是什么命 ab型rh阳性是什么意思 下午8点是什么时辰
婧读什么0735v.com 明亮的什么hcv7jop6ns7r.cn 前庭功能障碍是什么病hcv7jop9ns3r.cn 44岁月经量少是什么原因hcv8jop7ns5r.cn 静脉曲张吃什么药最好gangsutong.com
咬到舌头是什么预兆hcv7jop5ns5r.cn 身体动不动就出汗是什么原因hcv9jop3ns9r.cn 果葡糖浆是什么hcv9jop4ns8r.cn 孕酮是什么tiangongnft.com nerdy是什么牌子1949doufunao.com
拉肚子吃什么饭hcv8jop3ns0r.cn 荨麻疹抹什么药96micro.com 211是什么大学hcv8jop3ns3r.cn 绣球花什么时候开花xjhesheng.com 叶芽是什么hcv8jop1ns7r.cn
红海为什么叫红海hcv9jop0ns0r.cn 朝花夕拾什么意思hcv8jop7ns7r.cn 什么检查需要空腹hcv8jop5ns6r.cn 天热吃什么qingzhougame.com 大男子主义什么意思hcv8jop0ns7r.cn
百度