Questions tagged [self-executing-function]

An immediately-invoked function expression (or IIFE, pronounced "iffy") is a JavaScript design pattern which produces a lexical scope using a JavaScript closure. This pattern has been referred to as a self-executing anonymous function.

An immediately-invoked function expression (or IIFE, pronounced "iffy") is a JavaScript design pattern which produces a lexical scope using JavaScript's function scoping.

Immediately-invoked function expressions can be used to avoid variable hoisting from within blocks, protect against polluting the global environment and simultaneously allow public access to methods while retaining privacy for variables defined within the function.

This pattern has been referred to as a self-executing anonymous function, but Ben Alman introduced the term IIFE as a more semantically accurate term for the pattern.

Immediately-invoked function expressions may be written in a number of different ways, although a common convention is to enclose both the function expression and invocation in parentheses.

(function(){
  /* code */ 
}());

Key to understanding design patterns such as immediately-invoked function expressions is to realize JavaScript has function scope (but not block scope) and passes values by reference inside a closure.

Source:https://en.wikipedia.org/wiki/Immediately-invoked_function_expression

49 questions
515
votes
21 answers

What is the purpose of a self executing function in javascript?

In javascript, when would you want to use this: (function(){ //Bunch of code... })(); over this: //Bunch of code...
Ej.
  • 5,429
  • 3
  • 18
  • 15
6
votes
1 answer

NodeJS - How to assign constructor to module.exports in self-executing function?

I'm trying to assign a constructor in a self-executing function in NodeJS. I'm pretty sure it's not working because my parameter is a variable pointing to module.exports, but I'm curious if there's a way to make it work while staying as close to…
Tim Hardy
  • 1,654
  • 1
  • 17
  • 36
5
votes
2 answers

Cost of self-executing C++11 lambdas

Out of a window procedure, I'm writing a switch statement using self-executing lambdas, like this: LRESULT CALLBACK proc(HWND hWnd, UINT msg, WPARAM wp, LPARAM lp) { switch (msg) { case WM_CREATE: return [&](WPARAM wp, LPARAM lp) { …
rodrigocfd
  • 6,450
  • 6
  • 34
  • 68
5
votes
2 answers

JavaScript: Self-executing function with parameter

CodeMirror.net uses this construct (I'm simplifying slightly) to introduce the code for its JavaScript editor: (function(mod) { this.CodeMirror = mod(); })(function() { "use strict"; (15,000-odd lines of advanced JS) …
5
votes
2 answers

Why do you need to pass in arguments to a self executing function in javascript if the variable is global?

I was looking at the code for the underscore.js library (jQuery does the same thing) and just wanted some clarification on why the window object is getting passed into the self executing function. For example: (function() { //Line 6 var…
KingKongFrog
  • 13,946
  • 21
  • 75
  • 124
4
votes
1 answer

scope of this in self-executing function

Can anybody tell why the global scope is not applicable in the following case ? Why does the line#6 print undefined ? Shouldn't the outer "this" be available in the inner self-executing function ? var myObj = { test1 : 4, func : function(){ …
sandy
  • 283
  • 1
  • 7
  • 27
4
votes
4 answers

Using self invoking functions to be variable independent

Possible Duplicate: Javascript closure inside loops - simple practical example I'm trying to use a self invoking function so that each function in objects will return a different message.