Immediately invoked function expression: Difference between revisions

Content deleted Content added
Monkbot (talk | contribs)
m Task 16: replaced (4×) / removed (0×) deprecated |dead-url= and |deadurl= with |url-status=;
Line 1:
 
An '''immediately invoked function expression''' (or '''IIFE''', pronounced "iffy")<ref name=Alman>{{cite web |last=Alman |first=Ben |title=Immediately Invoked Function Expressions |url=http://benalman.com/news/2010/11/immediately-invoked-function-expression/ |date=15 November 2010 |accessdate=18 January 2019 |archive-url=https://web.archive.org/web/20171201033208/http://benalman.com/news/2010/11/immediately invoked-function-expression/ |archive-date=1 December 2017 |dead-url-status=nolive}}</ref> is a [[JavaScript]] [[Programming idiom|programming language idiom]] which produces a [[scope (computer science)|lexical scope]] using JavaScript's [[function scoping]].
 
Immediately invoked function expressions can be used to avoid [[JavaScript syntax#Scoping and hoisting|variable hoisting]] from within blocks, protect against polluting the [[Global variable|global environment]] and simultaneously allow public access to methods while retaining privacy for variables defined within the function.
Line 33:
</syntaxhighlight>
 
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]].<ref name=inimino>{{cite web |url=http://inimino.org/~inimino/blog/javascript_semicolons |title=JavaScript Semicolon Insertion: Everything you need to know |date=28 May 2010 |archive-url=https://web.archive.org/web/20171002224530/http://inimino.org/~inimino/blog/javascript_semicolons |archive-date=2 October 2017 |dead-url-status=nolive}}</ref><ref>{{cite web |url=https://mislav.net/2010/05/semicolons/ |title=Semicolons in JavaScript are optional |first=Mislav |last=Marohnić |date=7 May 2010 |archive-url=https://web.archive.org/web/20170808231150/https://mislav.net/2010/05/semicolons/ |archive-date=8 August 2017 |dead-url-status=nolive}}</ref> For example:
<syntaxhighlight lang="JavaScript">
a = b + c
Line 129:
== External links ==
* {{cite web |title=Functions and function scope |url=https://developer.mozilla.org/en-US/docs/JavaScript/Reference/Functions_and_function_scope |work=Mozilla JavaScript Reference |publisher=Mozilla Developer Network |accessdate=4 February 2013}}
* {{cite web |last=Soshnikov |first=Dmitry |title=ECMA-262-3 in detail. Chapter&nbsp;5. Functions. |url=http://dmitrysoshnikov.com/ecmascript/chapter-5-functions/#question-about-surrounding-parentheses |accessdate=4 February 2013 |archive-url=https://web.archive.org/web/20171201032007/http://dmitrysoshnikov.com/ecmascript/chapter-5-functions/#question-about-surrounding-parentheses |archive-date=1 December 2017 |dead-url-status=nolive}}
 
[[Category:JavaScript]]