R
RobG
I stumbled across a function that tests if a global variable exists or
not, my version of the code is below. Other than the obvious
irrelevance of such a function (a simple typeof test should be
sufficient in every case I can imagine), and that the use of
try..catch and eval should be limited as much as possible, are there
any significant issues with it?
function doesGlobalVarExist(v) {
try {
eval(v);
return true;
} catch(e) {
return false;
}
}
Given the description of the eval function in ECMA-262 ed 3 (ES 3) §
15.1.2.1 and eval code in § 10.1.2, it seems that when using a
"namespace" with functions initialised from an anonymous function it
is impossible to use eval to run code with global scope, e.g.:
var zz = 'Global zz';
var fn = (function() {
var zz = 'Local zz';
return function(v) {
return eval(v);
}
})();
alert(fn('zz')); // shows "Local zz"
The above always resolves to the local zz because the calling context
is placed on eval's scope chain when it is called, so the global zz is
"shadowed". Can eval code be run as global code using this scenario?
Are there cases where that is desirable (such as a
"doesGlobalVarExist" function)?
There is a statement in ES5 §10.4.2 that says:
"...if the eval code is not being evaluated by a direct call
(15.1.2.1.1) to the eval function then...Initialize the
execution context as if it was a global execution context"
I can't see how to call eval other than as a direct call.
Lastly, ES 3 says:
"if the eval property is assigned to, an EvalError
exception may be thrown"
Neither browser does, they allow assignments to eval.
not, my version of the code is below. Other than the obvious
irrelevance of such a function (a simple typeof test should be
sufficient in every case I can imagine), and that the use of
try..catch and eval should be limited as much as possible, are there
any significant issues with it?
function doesGlobalVarExist(v) {
try {
eval(v);
return true;
} catch(e) {
return false;
}
}
Given the description of the eval function in ECMA-262 ed 3 (ES 3) §
15.1.2.1 and eval code in § 10.1.2, it seems that when using a
"namespace" with functions initialised from an anonymous function it
is impossible to use eval to run code with global scope, e.g.:
var zz = 'Global zz';
var fn = (function() {
var zz = 'Local zz';
return function(v) {
return eval(v);
}
})();
alert(fn('zz')); // shows "Local zz"
The above always resolves to the local zz because the calling context
is placed on eval's scope chain when it is called, so the global zz is
"shadowed". Can eval code be run as global code using this scenario?
Are there cases where that is desirable (such as a
"doesGlobalVarExist" function)?
There is a statement in ES5 §10.4.2 that says:
"...if the eval code is not being evaluated by a direct call
(15.1.2.1.1) to the eval function then...Initialize the
execution context as if it was a global execution context"
I can't see how to call eval other than as a direct call.
Lastly, ES 3 says:
"if the eval property is assigned to, an EvalError
exception may be thrown"
Neither browser does, they allow assignments to eval.