Created
July 21, 2017 01:45
The with statement in ES6
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
// Like the with statement, but in ES6 and scoped only onto obj | |
const withES6 = function(obj, code){ | |
const scope = new Function(...Object.keys(obj), code); | |
scope(...Object.values(obj)); | |
}; | |
// Test | |
let myObj = { | |
res: {}, | |
a: 3, | |
b: 2 | |
}; | |
let code = "res.sum = a + b;\nres.mul = a * b;"; | |
withES6(myObj, code); | |
console.log(myObj.res); // {sum: 5, mul: 6} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment