Created
April 30, 2017 15:27
-
-
Save gpbaculio/6b5cef44a95bb1deb46c40648e4b8efc to your computer and use it in GitHub Desktop.
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
// Sample 1 | |
function wrapValue(n) { | |
var localVariable = n; | |
return function() { return localVariable; }; | |
} | |
var wrap1 = wrapValue(1); /* invoke wrapValue with argument 1(n on parameter), 1 will then be assigned to the localVariable. | |
Remember that wrapValue returns a function, so it's like you're assigning a function to wrap1 that | |
returns the localVariable value, in this case, 1. */ | |
var wrap2 = wrapValue(2); | |
console.log(wrap1()); // invoking wrap1 will return 1, this logs 1 on the console | |
// → 1 | |
console.log(wrap2()); | |
// → 2 | |
// Sample 2 | |
function multiplier(factor) { | |
return function(number) { | |
return number * factor; | |
}; | |
} | |
var twice = multiplier(2); // the return value of invoking multiplier(2) will be assigned to variable twice, it will return: | |
function(number) { | |
return number * 2; | |
}; | |
console.log(twice(5)); | |
// → 10 |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment