Created
May 25, 2017 14:36
-
-
Save shmidt-i/b73e351f8af98366ab01d2f1ae0b47ab to your computer and use it in GitHub Desktop.
Create your own closure for any function
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
// Lets say we have some function | |
var ar = 'Some value'; | |
const f = (...args2) => console.log(ar, ...args2); | |
f(1) //Some value, 1 | |
// Now lets change the behaviour | |
// without changing the function | |
const saveToClosure = (data) => { | |
var ar = data; | |
return f => (...args2) => typeof f === 'function' && eval(f.toString())(...args2) | |
} | |
const runFuncWithClosure = saveToClosure('Some different value'); | |
const newF = runFuncWithClosure(f); | |
newF(1); // Some different value, 1 |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment