Last active
August 29, 2015 14:09
-
-
Save dinks/809313a28ca30735b8bc to your computer and use it in GitHub Desktop.
Eval Gotcha
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 x = 'global'; | |
| function indirectEval() { | |
| 'use strict'; | |
| var x = 'local'; | |
| console.log(eval('x')); // Local | |
| // Call eval in a different way | |
| console.log(eval.call(null, 'x')); // global | |
| console.log(window.eval('x')); // global | |
| console.log((1, eval)('x')); // global (1) | |
| // Store eval somewhere else | |
| var xeval = eval; | |
| console.log(xeval('x')); // global | |
| var obj = { eval: eval }; | |
| console.log(obj.eval('x')); // global | |
| } | |
| indirectEval(); | |
| function strictFunc() { | |
| 'use strict'; | |
| var code = '(function () { return this }())'; | |
| var result = eval.call(null, code); | |
| console.log(result !== undefined); // true, sloppy mode | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment