Created
December 25, 2022 05:30
-
-
Save thmain/591d2cd806e0ef2e73b882d341bfe36b to your computer and use it in GitHub Desktop.
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
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