Last active
May 14, 2022 18:49
-
-
Save worldoptimizer/f1b407e53d1db3d075c1b32eace5a1d7 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
function selfFullFilling(fn) { | |
let value; | |
return function() { | |
return value || (value = fn.apply(this, arguments)); | |
}; | |
} | |
const add = selfFullFilling(function(a, b) { | |
console.log("calculated!") | |
return a + b; | |
}); | |
add(4,5) // returns 9 … logs "calculated" | |
add(4,5) // only returns 9 |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment