Last active
October 12, 2015 18:30
-
-
Save sarfraznawaz2005/f02854d16d5e213e95a1 to your computer and use it in GitHub Desktop.
Closure Example
This file contains hidden or 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
/* | |
Defination: | |
Inner functions referring to local variables of its outer function create closures | |
OR | |
Declaring a function within another function, and that inner function has access to its parent function's variables, even after that parent function has returned. | |
*/ | |
// http://robertnyman.com/2008/10/09/explaining-javascript-scope-and-closures/ | |
function add (x) { | |
return function (y) { | |
return x + y; | |
}; | |
} | |
var add5 = add(5); | |
var no8 = add5(3); | |
alert(no8); // Returns 8 |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment