I need to use eval() on global scope...
If you literally mean you need to eval something at global scope, you can do that with indirect eval:
(0, eval)("your code here");
It looks weird, but it makes eval work in global scope rather than local scope.
var n; // A global `n`
function direct() {
var n;
eval("n = 'a';");
console.log("Direct:");
console.log(" local n = " + n);
console.log(" global n = " + window.n);
}
function indirect() {
var n;
(0, eval)("n = 'b';");
console.log("Indirect:");
console.log(" local n = " + n);
console.log(" global n = " + window.n);
}
direct();
indirect();
...and I am missing the name of the global object. Is there a way of finding this out?
If you're running your code in loose mode, you can get access to the global object by calling a function, which will run with this referencing the global object. So:
var global = (function() { return this; })();
...gives you a variable called global that references the global object. It doesn't work in strict mode, because in strict mode, this in that function will be undefined.
It's also entirely possible that your code is being called with this already referencing the global object, so you might check that first.