Skip to main content
Stack Overflow
  1. About
  2. For Teams

Return to Answer

added 10 characters in body
Source Link
Ry-
  • 226.3k
  • 56
  • 496
  • 504

That construct is called an Immediately Invoked Function Expression (IIFE) which means it gets executed immediately. Think of it as a function getting called automatically when the interpreter reaches that function.

Most Common Use-case:

One of its most common use casecases is to limit the scope of a variable made via var. Variables created via var have a scope limited to a function so this construct (which is a function wrapper around certain code) will make sure that your variable scope doesn't leak out of that function.

In following example, countcount will not be available outside the immediately invoked function i.e. the scope of count will not leak out of the function. You should get a Reference ErrorReferenceError, should you try to access it outside of the immediately invoked function anyway.

(function () { 
 var count = 10;
})();
console.log(count); // Reference Error: count is not defined

ES6 Alternative (Recommended)

In ES6, we now can have variables created via let and const. Both of them are block-scoped (unlike var which is function-scoped).

Therefore, instead of using that complex construct of IIFE for the use case I mentioned above, you can now write much much simpler code to make sure that a variable's scope does not leak out of your desired block.

{ 
 let count = 10;
};
console.log(count); // Reference ErrorReferenceError: count is not defined

In this example, we used let to define count variable which makes count limited to the block of code, we created with the curly brackets {...}.

I call it a Curly Jail"Curly Jail".

That construct is called Immediately Invoked Function Expression (IIFE) which means it gets executed immediately. Think of it as a function getting called automatically when interpreter reaches that function.

Most Common Use-case:

One of its most common use case is to limit scope of a variable made via var. Variables created via var have a scope limited to a function so this construct (which is a function wrapper around certain code) will make sure that your variable scope doesn't leak out of that function.

In following example, count will not be available outside the immediately invoked function i.e. scope of count will not leak out of the function. You should get a Reference Error, should you try to access it outside of the immediately invoked function anyway.

(function () { 
 var count = 10;
})();
console.log(count); // Reference Error: count is not defined

ES6 Alternative (Recommended)

In ES6, we now can have variables created via let and const. Both of them are block-scoped (unlike var which is function-scoped).

Therefore, instead of using that complex construct of IIFE for the use case I mentioned above, you can now write much much simpler code to make sure that a variable's scope does not leak out of your desired block.

{ 
 let count = 10;
};
console.log(count); // Reference Error: count is not defined

In this example, we used let to define count variable which makes count limited to the block of code, we created with the curly brackets {...}.

I call it a Curly Jail.

That construct is called an Immediately Invoked Function Expression (IIFE) which means it gets executed immediately. Think of it as a function getting called automatically when the interpreter reaches that function.

Most Common Use-case:

One of its most common use cases is to limit the scope of a variable made via var. Variables created via var have a scope limited to a function so this construct (which is a function wrapper around certain code) will make sure that your variable scope doesn't leak out of that function.

In following example, count will not be available outside the immediately invoked function i.e. the scope of count will not leak out of the function. You should get a ReferenceError, should you try to access it outside of the immediately invoked function anyway.

(function () { 
 var count = 10;
})();
console.log(count); // Reference Error: count is not defined

ES6 Alternative (Recommended)

In ES6, we now can have variables created via let and const. Both of them are block-scoped (unlike var which is function-scoped).

Therefore, instead of using that complex construct of IIFE for the use case I mentioned above, you can now write much simpler code to make sure that a variable's scope does not leak out of your desired block.

{ 
 let count = 10;
}
console.log(count); // ReferenceError: count is not defined

In this example, we used let to define count variable which makes count limited to the block of code, we created with the curly brackets {...}.

I call it a "Curly Jail".

Rollback to Revision 2 - Edit approval overridden by post owner or moderator
Source Link
Ry-
  • 226.3k
  • 56
  • 496
  • 504

That construct is called Immediately Invoked Function Expression (IIFE) which means it gets executed immediately. Think of it as a function getting called automatically when the interpreter reaches that function.

Most Common Use-case:

One of its most common use case is to limit the scope of a variable made via var. Variables created via var have a scope limited to a function so this construct (which is a function wrapper around certain code) will make sure that your variable scope doesn't leak out of that function.

In following example, count will not be available outside the immediately invoked function i.e. Scopescope of count will not leak out of the function. You should get a Reference Error, should you try to access it outside of the immediately invoked function anyway.

(function () { 
 var count = 10;
})();
console.log(count); // Reference Error: count is not defined

ES6 Alternative (Recommended)

In ES6, we now can have variables created via let and const. Both of them are block-scoped (unlike var which is a function-scoped).

Therefore, instead of using that complex construct of IIFE for the use case I mentioned above, you can now write much, much simpler code to make sure that a variable's scope does not leak out of your desired block.

{ 
 let count = 10;
};
console.log(count); // Reference Error: count is not defined

In this example, we used let to define a count variable which makes count limited to the block of code, we created with the curly brackets {...}.

I call it a Curly Jail.

That construct is called Immediately Invoked Function Expression (IIFE) which means it gets executed immediately. Think of it as a function getting called automatically when the interpreter reaches that function.

Most Common Use-case:

One of its most common use case is to limit the scope of a variable made via var. Variables created via var have a scope limited to a function so this construct (which is a function wrapper around certain code) will make sure that your variable scope doesn't leak out of that function.

In following example, count will not be available outside the immediately invoked function i.e. Scope of count will not leak out of the function. You should get a Reference Error, should you try to access it outside of the immediately invoked function anyway.

(function () { 
 var count = 10;
})();
console.log(count); // Reference Error: count is not defined

ES6 Alternative (Recommended)

In ES6, we now can have variables created via let and const. Both of them are block-scoped (unlike var which is a function-scoped).

Therefore, instead of using that complex construct of IIFE for the use case I mentioned above, you can now write much, much simpler code to make sure that a variable's scope does not leak out of your desired block.

{ 
 let count = 10;
};
console.log(count); // Reference Error: count is not defined

In this example, we used let to define a count variable which makes count limited to the block of code, we created with the curly brackets {...}.

I call it a Curly Jail.

That construct is called Immediately Invoked Function Expression (IIFE) which means it gets executed immediately. Think of it as a function getting called automatically when interpreter reaches that function.

Most Common Use-case:

One of its most common use case is to limit scope of a variable made via var. Variables created via var have a scope limited to a function so this construct (which is a function wrapper around certain code) will make sure that your variable scope doesn't leak out of that function.

In following example, count will not be available outside the immediately invoked function i.e. scope of count will not leak out of the function. You should get a Reference Error, should you try to access it outside of the immediately invoked function anyway.

(function () { 
 var count = 10;
})();
console.log(count); // Reference Error: count is not defined

ES6 Alternative (Recommended)

In ES6, we now can have variables created via let and const. Both of them are block-scoped (unlike var which is function-scoped).

Therefore, instead of using that complex construct of IIFE for the use case I mentioned above, you can now write much much simpler code to make sure that a variable's scope does not leak out of your desired block.

{ 
 let count = 10;
};
console.log(count); // Reference Error: count is not defined

In this example, we used let to define count variable which makes count limited to the block of code, we created with the curly brackets {...}.

I call it a Curly Jail.

That construct is called Immediately Invoked Function Expression (IIFE) which means it gets executed immediately. Think of it as a function getting called automatically when the interpreter reaches that function.

Most Common Use-case:

One of its most common use case is to limit the scope of a variable made via var. Variables created via var have a scope limited to a function so this construct (which is a function wrapper around certain code) will make sure that your variable scope doesn't leak out of that function.

In following example, count will not be available outside the immediately invoked function i.e. scopeScope of count will not leak out of the function. You should get a Reference Error, should you try to access it outside of the immediately invoked function anyway.

(function () { 
 var count = 10;
})();
console.log(count); // Reference Error: count is not defined

ES6 Alternative (Recommended)

In ES6, we now can have variables created via let and const. Both of them are block-scoped (unlike var which is a function-scoped).

Therefore, instead of using that complex construct of IIFE for the use case I mentioned above, you can now write much, much simpler code to make sure that a variable's scope does not leak out of your desired block.

{ 
 let count = 10;
};
console.log(count); // Reference Error: count is not defined

In this example, we used let to define a count variable which makes count limited to the block of code, we created with the curly brackets {...}.

I call it a Curly Jail.

That construct is called Immediately Invoked Function Expression (IIFE) which means it gets executed immediately. Think of it as a function getting called automatically when interpreter reaches that function.

Most Common Use-case:

One of its most common use case is to limit scope of a variable made via var. Variables created via var have a scope limited to a function so this construct (which is a function wrapper around certain code) will make sure that your variable scope doesn't leak out of that function.

In following example, count will not be available outside the immediately invoked function i.e. scope of count will not leak out of the function. You should get a Reference Error, should you try to access it outside of the immediately invoked function anyway.

(function () { 
 var count = 10;
})();
console.log(count); // Reference Error: count is not defined

ES6 Alternative (Recommended)

In ES6, we now can have variables created via let and const. Both of them are block-scoped (unlike var which is function-scoped).

Therefore, instead of using that complex construct of IIFE for the use case I mentioned above, you can now write much much simpler code to make sure that a variable's scope does not leak out of your desired block.

{ 
 let count = 10;
};
console.log(count); // Reference Error: count is not defined

In this example, we used let to define count variable which makes count limited to the block of code, we created with the curly brackets {...}.

I call it a Curly Jail.

That construct is called Immediately Invoked Function Expression (IIFE) which means it gets executed immediately. Think of it as a function getting called automatically when the interpreter reaches that function.

Most Common Use-case:

One of its most common use case is to limit the scope of a variable made via var. Variables created via var have a scope limited to a function so this construct (which is a function wrapper around certain code) will make sure that your variable scope doesn't leak out of that function.

In following example, count will not be available outside the immediately invoked function i.e. Scope of count will not leak out of the function. You should get a Reference Error, should you try to access it outside of the immediately invoked function anyway.

(function () { 
 var count = 10;
})();
console.log(count); // Reference Error: count is not defined

ES6 Alternative (Recommended)

In ES6, we now can have variables created via let and const. Both of them are block-scoped (unlike var which is a function-scoped).

Therefore, instead of using that complex construct of IIFE for the use case I mentioned above, you can now write much, much simpler code to make sure that a variable's scope does not leak out of your desired block.

{ 
 let count = 10;
};
console.log(count); // Reference Error: count is not defined

In this example, we used let to define a count variable which makes count limited to the block of code, we created with the curly brackets {...}.

I call it a Curly Jail.

added 170 characters in body
Source Link
Uthman
  • 9.9k
  • 19
  • 80
  • 109
Loading
Source Link
Uthman
  • 9.9k
  • 19
  • 80
  • 109
Loading
lang-js

AltStyle によって変換されたページ (->オリジナル) /