卖关子是什么意思| 梦见和死去的人说话是什么意思| 四月初八是什么节日| 乳腺4a类是什么意思| 真菌是什么原因引起的| 过命之交是什么意思| 水银是什么东西| 二十七岁属什么生肖| 喝酸梅汤有什么好处| 悦己是什么意思| 杨贵妃属什么生肖| 龟毛的性格指什么性格| 甲鱼吃什么| 孔子是什么学派的创始人| 常州有什么特产| 梦见自己鞋子破了是什么意思| 奶奶的妈妈叫什么| 梦见虱子是什么意思| 嗓子有异物感堵得慌吃什么药| 反复低烧是什么原因| hitachi是什么品牌| vdo是什么牌子| 红细胞低吃什么补得快| 七月二十九是什么星座| 什么减肥有效| slogan是什么意思| 条形码的数字代表什么| 乙肝有抗体是什么意思| 胆汁为什么会反流到胃里面| 北京大裤衩建筑叫什么| 梦见捞鱼是什么意思| 撇清关系是什么意思| 白细胞酯酶弱阳性是什么意思| ta是什么| 梦见自己娶媳妇是什么意思| dsa检查是什么意思| 健硕是什么意思| 小知了叫什么| 被螨虫咬了用什么药膏| 氧化锆是什么材料| 慢性胃炎吃什么药效果好| 静的部首是什么| 什么护肤品比较好| 穗字五行属什么| 男女双修什么意思| 传染源是什么| 九转大肠是什么菜系| 海之蓝是什么香型| 腹泻输液用什么药| 指甲盖凹凸不平是什么原因| 钾低会出现什么症状| lee属于什么档次| 画什么才好看| 今年是农历的什么年| 无名指戴戒指代表什么| 鸡属于什么科| 榴莲树长什么样子| 淡淡的什么| 为什么母乳妈妈会便秘| 肺心病是什么原因引起的| 血糖高喝什么牛奶好| ltp什么意思| 脚后跟疼是什么病| 姊妹是什么意思| 胃胀想吐是什么原因| 处事不惊是什么意思| 阴道有腥味是什么原因| 义诊是什么意思| 送孕妇什么礼物最贴心| 我们到底什么关系| 红日是什么意思| 天体是什么意思| 高压氧治疗有什么作用| 胆囊结石挂什么科| 艾滋病英文缩写是什么| 右侧胸口疼是什么原因| 为什么会一直拉肚子| 为什么会有| 缺血灶是什么意思| 长期吃避孕药有什么副作用| 老婆妹妹叫什么| 经常感冒是什么原因| 天井是什么意思| 嗓子痒是什么原因| 为什么早上起床头晕| 刘字是什么偏旁| 肚脐下方硬硬的是什么| 心慌应该挂什么科| 什么飞船| 包皮是什么样子的| semir是什么牌子| 杂交金毛犬长什么样子| 脾胃湿热吃什么中成药| 血压低压低是什么原因| 大队书记是什么级别| 儿童过敏性咳嗽吃什么药| 血小板为什么会高| 三月一日是什么星座| 日单是什么意思| 什么叫做罹患疾病| 比干是什么神| 结婚证需要什么资料| 怀孕两个月出血是什么原因| 野鸡大学是什么意思| 2月8日什么星座| 旅游穿什么鞋最舒服| 血小板降低是什么病| 一箭双雕是什么生肖| 金字旁加匀念什么| 人得了猫藓用什么药膏| 老是低血糖是什么原因| 血糖高适合吃什么水果| 老人家脚肿是什么原因引起的| 流清口水是什么原因| 印度什么教| 口酸是什么原因| 例假推迟是什么原因| 新生儿一直哭闹是什么原因| 为什么叫书记| 抹茶是什么茶叶做的| 铁剂是什么| 羟基是什么| 岩茶属于什么茶| 你喜欢吃什么用英语怎么说| 薄姬为什么讨厌窦漪房| 酒后手麻什么原因| 缢死是什么意思| 茶学专业学什么| 健身有什么好处| 什么时候闰十二月| 恍然大悟什么意思| 甲减不能吃什么东西| 急性阑尾炎什么症状| 装孙子是什么意思| 痔疮吃什么食物| 葡萄是什么季节的水果| 骨密度高是什么意思| 气场强大是什么意思| 煞是什么意思| 肚子左边是什么器官| 包皮龟头炎用什么药膏| 冰糖和白砂糖有什么区别| 白癜风吃什么药| 危日是什么意思| 咀嚼什么意思| 肉桂跟桂皮有什么区别| 东字五行属什么| 无犯罪证明需要什么材料| 百合是什么植物| 单身领养孩子需要什么条件| 餐后胆囊是什么意思| 月经几个月不来是什么原因| 盖世英雄是什么意思| 什么药吃了死的快| 山竹树长什么样| 然五行属什么| 脂蛋白高是什么意思| 南瓜子不能和什么一起吃| 陈醋泡花生米有什么功效| 两毛二是什么军衔| 什么的意思| 拔牙前需要做什么检查| 背后长痘痘是什么原因| hh是什么品牌| 清明节的习俗是什么| 改良碱性土壤用什么| 正装是什么样的衣服| 姻缘是什么意思| 胰子是什么意思| 十余年是什么意思| 铅中毒有什么症状| 气血不足吃什么东西| 心里空落落的是什么意思| 武夷山在什么地方| 八仙过海是什么生肖| 眼睛充血用什么眼药水| 伤口不结痂是什么原因| 药物过敏挂什么科| pci是什么| 椒盐是什么调料| 荤菜是什么意思| 为什么不嫁丧妻之男| 金乌是什么| 什么是hpv病毒| 四月二十是什么星座| 脑溢血有什么后遗症| 胃穿孔是什么症状| 手指甲出现双层是什么原因| 做梦捡到钱是什么预兆| 山药有什么功效和作用| 为什么合欢树又叫鬼树| 吃什么提高免疫力最好最快| 赢荡为什么传位嬴稷| 留个念想是什么意思| 吃鸡蛋胃疼是什么原因| 247什么意思| 土字生肖有钱收是什么生肖| 荷花和睡莲有什么区别| 副旅长是什么军衔| 铁蛋白高是什么意思| 大面念什么| 儿童尿路感染吃什么药| 美业是做什么的| 断袖是什么意思| 突然吐血是什么原因| 言字旁可念什么| 幻觉幻听是什么症状| 怀孕皮肤变差什么原因| 基础代谢是什么意思| 静谧时光什么意思| 脾胃不好吃什么水果好| 故事梗概是什么意思| 什么叫强迫症| 斑秃挂什么科| 金牛座的幸运色是什么| 银耳有什么功效| 处女座什么性格| 痛风吃什么药效果最好| 渡人渡己什么意思| 儿童干咳吃什么药| 孕中期宫缩是什么感觉| 子宫内膜厚是什么原因造成的| 肠胃炎可以吃什么水果| 过境签是什么意思| 美国为什么制裁伊朗| 伤到什么程度打破伤风| 心心相惜是什么意思| 余光是什么意思| 农历9月28日是什么星座| 性生活后尿路感染是什么原因| 肺部肿瘤切除后吃什么| 拆线去医院挂什么科| 房性早搏吃什么药| 盆腔静脉石是什么意思| 痤疮是什么引起的| jk什么意思| 上午九点多是什么时辰| 脂肪肝看什么科| menu是什么意思| 太后是皇上的什么人| 生气容易得什么病| 吃什么水果对肠胃好| 合什么意思| 一阴一阳是什么数字| 悬钟为什么叫绝骨| 什么是考生号| 推崇是什么意思| 白细胞偏高是什么原因| 溪字五行属什么| 卡密是什么| 肠系膜淋巴结肿大吃什么药| 大便咖啡色什么原因| 卵巢囊肿有什么症状| 早搏心律不齐吃什么药| 皮肤溃烂是什么病| 墨鱼干和什么煲汤最好| 低脂高钙牛奶适合什么人群| 自诩是什么意思| 线性是什么意思| 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]
海豹油有什么功效 眼睛肿胀是什么原因 吃什么东西可以减肥 重阳节吃什么好 什么样的人不能吃海参
4.22是什么日子 盛情款待是什么意思 渴望是什么意思 脑梗长期吃什么药好 为什么新生儿有胎记
黄曲霉素是什么 什么颜色最吸热 什么是http 指甲凹凸不平什么原因 什么减肥药好使
小寄居蟹吃什么 胃不舒服恶心想吐吃什么药 水星是什么颜色的 福州有什么好玩的地方 hbeag阳性是什么意思
胃寒湿气重吃什么药效果最好hcv7jop9ns2r.cn 什么叫自然拼读hcv8jop7ns2r.cn 什么夫妻百事哀hcv9jop7ns9r.cn 大麦茶是什么做的adwl56.com 上热下寒吃什么食物好hcv7jop6ns0r.cn
美国为什么要打伊朗hcv8jop1ns6r.cn 农历五月是什么月hcv8jop4ns0r.cn 飞秒是什么hcv9jop4ns1r.cn 什么多么什么造句hcv7jop4ns7r.cn 胰腺的作用和功能是什么hcv7jop6ns5r.cn
肌病是什么病hcv7jop5ns6r.cn 麻痹是什么意思hcv8jop1ns8r.cn bb霜和cc霜有什么区别hcv8jop0ns3r.cn 汞中毒是什么症状hcv8jop8ns8r.cn 尿黄是什么原因男性hcv8jop8ns7r.cn
副鼻窦炎是什么意思hcv9jop3ns3r.cn 虎头什么尾hcv8jop2ns1r.cn 盐酸对人体有什么危害hcv8jop5ns9r.cn 汗多尿少是什么原因hcv8jop8ns2r.cn 风寒咳嗽吃什么药hcv9jop1ns7r.cn
百度