Talk:Immediately invoked function expression

Latest comment: 4 years ago by 46.46.195.82 in topic other languages that support IIFE

Invalid Example edit

Note 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 yoReply

ES6 Block Scope edit

ES6 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 (talkcontribs) 01:47, 19 February 2020 (UTC)Reply

Parentheses edit

The 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)Reply

Maintenance and rating of JavaScript articles edit

Concerning editing and maintaining JavaScript-related articles...

Collaboration... edit

If 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 edit

We've found over 300 JavaScript-related articles so far. If you come across any others, please add them to that list.

User scripts edit

The 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 edit

If 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 edit

At 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)Reply

One-time initialization with garbage collected resources edit

How 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)Reply

Initial explanation edit

The 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)Reply

other languages that support IIFE edit

PHP 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)?;

46.46.195.82 (talk) 15:47, 22 April 2020 (UTC)Reply