-
-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathclosure.js
32 lines (26 loc) · 901 Bytes
/
closure.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
/*
# Closure - when a function has a inner function thats when a closure is created. In other word, the inner function has access
to the outer function variables even after the outer function is executed. Same goes for the outer function arguments.
## Reference
1. http://www.w3schools.com/js/js_function_closures.asp
2. http://stackoverflow.com/questions/111102/how-do-javascript-closures-work?answertab=active#tab-top
*/
// Example 1
function outerFunz() {
var x = 10;
return function(y) {
return x + y; // x is available here
}
}
var funz = outerFunz();
var innerFunz = funz(10); // Passing `y` and calling `inner anonymous` function
console.log(innerFunz); // 20
// Example 2
function outerFunz(z) {
var x = 10;
function innerFunz() {
return x + z; // x & z is available here
}
innerFunz(); // calling `innerFunz` after outer function
}
console.log(outerFunz(15)); // 25