Last active
December 22, 2015 08:19
-
-
Save ryasmi/6443970 to your computer and use it in GitHub Desktop.
Some crazy `new Function` use.
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
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