Is it possible to use the eval command to execute something with a global scope? For example, this will cause an error:
<script>
function execute(x){
eval(x);
}
function start(){
execute("var ary = new Array()");
execute("ary.push('test');"); // This will cause exception: ary is not defined
}
</script>
<html><body onLoad="start()"></body></html>
I know the 'with' keyword will set a specific scope, but is there a keyword for the global scope? Or is it possible to define a custom scope that would allow this to work?
<script>
var scope = {};
function execute(x){
with(scope){
eval(x);
}
}
function start(){
execute("var ary = new Array()");
execute("ary.push('test');"); // This will cause exception: ary is not defined
}
</script>
<html><body onLoad="start()"></body></html>
Essentially, what I am trying to do is have a global execute funciton...
(function(){
eval.apply(this, arguments);
}(a,b,c))
This will invoke eval using the global object, window
in browsers, as the this
argument passing any arguments you've passed into the anonymous function.
eval.call(window, x, y, z)
or eval.apply(window, arguments)
is also valid if you're certain window
is the global object. This isn't always true, however. For instance, the global object in a Node.js script is process
if I remember correctly.
You should be able to use eval()
in global scope by calling it indirectly. However, not all browsers are currently doing this.
Further Reading.
Use (1, eval)('...')
.
$ node
> fooMaker = function () { (1, eval)('foo = "bar"'); };
[Function]
> typeof foo
'undefined'
> fooMaker()
undefined
> typeof foo
'string'
> foo
'bar'
To execute some JavaScript in the global scope you can call it indirectly by using setTimeout() or if you are using jQuery, take a look at $.globalEval().
Changing your execute method to the following will allow you to still use the 'var' keyword:
function execute(x) {
setTimeout("eval(" + x + ")", 0);
// Or: $.globalEval(x);
}
function start() {
try
{
execute("var ary = new Array()");
execute("ary.push('test');");
}
catch (e)
{
alert(e);
}
}
start();
I know there will be lot of comments coming in with eval is evil and I agree with that.
However, to answer your question, change your start method as follows:
function start(){
execute("ary = new Array()");
execute("ary.push('test');"); // This will cause exception: ary is not defined
}
Use eval.apply(null, ["code"]);
.
eval.apply(this, ["code"]);
does not work on Microsoft Script Host (cscript.exe).