Talk:Immediately invoked function expression
![]() | This article is rated C-class on Wikipedia's content assessment scale. It is of interest to the following WikiProjects: | ||||||||||
|
Invalid Example
editNote that code samples from Alman's page are licensed under the GPL and so may be used with attribution.
This code snippet is incorrect:
var v, getValue;
v = 1;
getValue = (function(v) {
return v;
}(v));
v = 2;
getValue(); // 1
`getValue` is the number 1, not a function. 88.90.244.215 (talk) 19:40, 3 April 2013 (UTC) Later yo
ES6 Block Scope
editES6 block scope and let/ const replaces some use cases of IIFE and may need to be mentioned in this article.
- IEFEs blocks cannot replace the module pattern. Blocks don't have return values so IIFE is needed for objects that have private state.
- For function declaration scoping an IIFE is still necessary because it will be hoisted out of a block scope.
See: https://stackoverflow.com/questions/33534485/will-const-and-let-make-the-iife-pattern-unnecessary — Preceding unsigned comment added by Jpvosloo (talk • contribs) 01:47, 19 February 2020 (UTC)
Parentheses
editThe Usage section says: a common convention is to enclose both the function expression and invocation in parentheses but the following example (the one with the parameters) does not follow this convention (the invocation is not enclosed).
I changed that example in order to have both expression and invocation enclosed in parentheses. --Mariana de El Mondongo (talk) 00:28, 24 February 2015 (UTC)
Maintenance and rating of JavaScript articles
editConcerning editing and maintaining JavaScript-related articles...
Collaboration...
editIf you are interested in collaborating on JavaScript articles or would like to see where you could help, stop by Wikipedia:WikiProject JavaScript and feel free to add your name to the participants list. Both editors and programmers are welcome.
Where to list JavaScript articles
editWe've found over 300 JavaScript-related articles so far. If you come across any others, please add them to that list.
User scripts
editThe WikiProject is also taking on the organization of the Wikipedia community's user script support pages. If you are interested in helping to organize information on the user scripts (or are curious about what we are up to), let us know!
If you have need for a user script that does not yet exist, or you have a cool idea for a user script or gadget, you can post it at Wikipedia:User scripts/Requests. And if you are a JavaScript programmer, that's a great place to find tasks if you are bored.
How to report JavaScript articles in need of attention
editIf you come across a JavaScript article desperately in need of editor attention, and it's beyond your ability to handle, you can add it to our list of JavaScript-related articles that need attention.
Rating JavaScript articles
editAt the top of the talk page of most every JavaScript-related article is a WikiProject JavaScript template where you can record the quality class and importance of the article. Doing so will help the community track the stage of completion and watch the highest priority articles more closely.
Thank you. The Transhumanist 01:09, 12 April 2017 (UTC)
One-time initialization with garbage collected resources
editHow strange this is not mentioned. One of the frequent reasons to use IIFE's is initialization code. Especially when resources are used that can be discarded after initialization is done. Afterwards, the function and its internal variables are available to be garbage-collected. As one not-so-good tutorial mentions:
- The variable and functions that were declared in the scope of IIFE are available for garbage collection, once the IIFE is executed. Hence managing Memory in an efficient manner and ensures that the function and variables do not bind itself to the global scope.
Shenme (talk) 05:09, 20 June 2018 (UTC)
Initial explanation
editThe initial explanation is not readily understandable and needs to be greatly simplified. (Put another way, it would only be understandable by someone who didn't need to view this page.) A more technical explanation can follow afterward. — Preceding unsigned comment added by 203.161.100.28 (talk) 06:46, 1 November 2018 (UTC)
other languages that support IIFE
editPHP has the same syntax for IIFE as JavaScript:
<?php (function(){ echo "Hello from IIFE"; })();
and you can make an IIFE in C++ like:
([]()->void{ std::cout << "Hello from IIFE"; })();
Rust also supports this, which is useful for scoping errors:
(|| {
do_thing_one()?;
do_thing_two()?;
Ok(())
})().map_err(CustomError::ThisError)?;