-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathimmediate-function.js
More file actions
18 lines (15 loc) · 858 Bytes
/
immediate-function.js
File metadata and controls
18 lines (15 loc) · 858 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
// An immediate function is one that executes as soon as it is defined.
// Creating an immediate function is simple: you add the open/close parentheses after the closing curly bracket,
// and then wrap the entire function in parentheses. That’s it!
// It's not about an immediately executed function vs. a regular function; in fact it has very little to nothing in relation.
// The sole purpose of an immediately invoked wrapping-function is to scope variables local to the wrapping function.
// Wrapped variables will be in the function scope, not the global. That's the scope advantage.
(function(){
name = 'RGT'
console.log('hello, ' + name + '. I am an immediate function');
}())
// same with passing a variable to immediate function
var myName = 'RGT';
(function(thisName){
console.log( 'hello, my name is: ' + thisName );
}(myName))