Skip to content

Instantly share code, notes, and snippets.

@thmain
Created December 25, 2022 05:30
Show Gist options
  • Save thmain/591d2cd806e0ef2e73b882d341bfe36b to your computer and use it in GitHub Desktop.
Save thmain/591d2cd806e0ef2e73b882d341bfe36b to your computer and use it in GitHub Desktop.
function realSum(a, b) {
return a + b;
};
function sum(a, b) {
return b ?
realSum(a, b) :
function(b) {
// This anonymous function has access to variable `a` via closure
return realSum(a, b);
}
}
console.log(sum(5, 3)); // 8
var sum5 = sum(5);
console.log(sum5(4)); // 9
var sum3 = sum(3);
console.log(sum3(4)); // 7
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment