Skip to content

Instantly share code, notes, and snippets.

@ryasmi
Last active December 22, 2015 08:19
Show Gist options
  • Save ryasmi/6443970 to your computer and use it in GitHub Desktop.
Save ryasmi/6443970 to your computer and use it in GitHub Desktop.
Some crazy `new Function` use.
var crazy = function () {
var val = 10;
return {
val: function () { return val; },
changeVal: function () { val = 11; },
changeGlobal: new Function ('val = 12;')
};
};
var run = function (obj, fn) {
fn();
console.log('Obj val. ' + obj.val());
console.log('Global val. ' + val);
};
var derp = crazy();
console.log('Derp val before changeVal. ' + derp.val()); // 10.
derp.changeVal();
console.log('Derp val after changeVal. ' + derp.val()); // 11.
derp.changeGlobal();
console.log('Derp val after changeGlobal. ' + derp.val()); // 11.
console.log('Global val after changeGlobal. ' + val); // 12.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment