Skip to content

Instantly share code, notes, and snippets.

@jsguy
Created July 21, 2017 01:45
Show Gist options
  • Save jsguy/60c65185344d76d44b0dccd6ff638fa7 to your computer and use it in GitHub Desktop.
Save jsguy/60c65185344d76d44b0dccd6ff638fa7 to your computer and use it in GitHub Desktop.
The with statement in ES6
// 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