Created
July 21, 2017 01:45
-
-
Save jsguy/60c65185344d76d44b0dccd6ff638fa7 to your computer and use it in GitHub Desktop.
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