Skip to content

Instantly share code, notes, and snippets.

@shmidt-i
Created May 25, 2017 14:36
Show Gist options
  • Save shmidt-i/b73e351f8af98366ab01d2f1ae0b47ab to your computer and use it in GitHub Desktop.
Save shmidt-i/b73e351f8af98366ab01d2f1ae0b47ab to your computer and use it in GitHub Desktop.
Create your own closure for any function
// 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