-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
49 Immediately Invoked Function Expressions (IIFE)
README's Index updated.
- Loading branch information
Showing
3 changed files
with
54 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
<!DOCTYPE html> | ||
<html lang="en"> | ||
<head> | ||
<meta charset="UTF-8"/> | ||
<meta name="viewport" content="width=device-width, initial-scale=1.0"/> | ||
<title>Advanced JS: Objects & Functions - Immediately Invoked Function Expressions (IIFE)</title> | ||
</head> | ||
<body> | ||
<h1>Immediately Invoked Function Expressions (IIFE)</h1> | ||
<p><em>Check the developer console for the log</em></p> | ||
<script src="./scripts/iife.js" type="text/javascript"></script> | ||
</body> | ||
</html> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,39 @@ | ||
/******************************************************************************************** | ||
* Immediately Invoked Function Expressions (IIFE) | ||
* ----------------------------------------------- | ||
* IIFE (pronounced as "iffy") is used to hide the data. If we want some data to be | ||
* unaccessible from the Global Execution Context, then we use IIFE. | ||
* | ||
* A normal function is like the following: | ||
* function game() { // function declaration | ||
* var score = Math.random() * 10; | ||
* console.log(score >= 5); | ||
* } | ||
* game(); // Function call | ||
* We can call the game() function and access the score variable from the global scope | ||
* But if we don't want that to happen, we use IIFE, as follows: | ||
*/ | ||
|
||
// Demo of IIFE | ||
( // We trick the parse to think that inside this paranthesis, there's an expression, | ||
// but it is an anonymous function. | ||
function() { | ||
var score = Math.random() * 10; | ||
console.log(score >= 5); | ||
} | ||
)(); // We call it here itself using "();" | ||
|
||
// We cannot access the score variable here in the global scope. | ||
// console.log(score); // ReferenceError | ||
|
||
|
||
// This time, we will pass in an argument to the IIFE as follows | ||
( | ||
function(goodLuck) { | ||
var score = Math.random() * 10; | ||
console.log(score >= 5 - goodLuck); | ||
} | ||
)(5); // We are passing 5 into the function that we just declared as the IIFE | ||
|
||
|
||
// IIFE's are a really powerful tool to be used in JavaScript |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters