Skip to content

Instantly share code, notes, and snippets.

@dinks
Last active August 29, 2015 14:09
Show Gist options
  • Select an option

  • Save dinks/809313a28ca30735b8bc to your computer and use it in GitHub Desktop.

Select an option

Save dinks/809313a28ca30735b8bc to your computer and use it in GitHub Desktop.
Eval Gotcha
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