Skip to content

Instantly share code, notes, and snippets.

@ifkas
Last active February 2, 2018 10:08
Show Gist options
  • Save ifkas/11e24284c6884241b09f9a603f0b3cc7 to your computer and use it in GitHub Desktop.
Save ifkas/11e24284c6884241b09f9a603f0b3cc7 to your computer and use it in GitHub Desktop.
JS Closure
// Example with parameter only, no variable in scope and anonymous inner function
function orange(color) {
return function(brand) {
return "The oranges are with " + color + " color, but the computer brand can be " + brand;
}
}
let coloring = orange("orange");
console.log(coloring("apple"));
// > The oranges are with orange color, but the computer brand can be apple
// Or in some situations we can simplify the above without declaring variable:
orange("Relishes")("Red");
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment