Skip to content

Instantly share code, notes, and snippets.

@sarfraznawaz2005
Last active October 12, 2015 18:30
Show Gist options
  • Save sarfraznawaz2005/f02854d16d5e213e95a1 to your computer and use it in GitHub Desktop.
Save sarfraznawaz2005/f02854d16d5e213e95a1 to your computer and use it in GitHub Desktop.
Closure Example
/*
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