Re: JavaScript global object
- From: Michael Winter <m.winter@xxxxxxxxxxxxxxxx>
- Date: Mon, 27 Mar 2006 09:49:02 GMT
On 27/03/2006 00:18, Roman Ziak wrote:
[snip]
myGlobal = this;
Variables should always be declared using a var statement. Though this particular assignment is not likely to ever cause problems, it's good practice to explicitly declare myGlobal.
[snip]
function EvalCode (code)
{
myGlobal.eval(code);
}
This is a problem, and seems to also produce different results in different browsers. The first issue is that you are calling the eval function as a method, however:
If value of the eval property is used in any way other than a
direct call (that is, other than by the explicit use of its
name as an Identifier which is the MemberExpression in a
CallExpression), or if the eval property is assigned to, an
EvalError exception may be thrown.
-- 15.1.2.1 eval(x), ECMA-262 3rd Ed.
Calling the function as a method seems to cause Firefox and Opera to violate 10.2.2, whereas IE continues to follow it regardless.
[snip]
Function EvalCode() will not work for function declaration,
It works perfectly, but it doesn't create a global variable (in IE, and Fx and Op when eval is called as a function). It's not supposed to.
Section 10.2.2 of ECMA-262 states:
When control enters an execution context for eval code, the
previous active execution context, referred to as the calling
context, is used to determine the scope chain, the variable
object, and the this value.
In this case, the calling context is the EvalCode function. Evaluating a function declaration within that context is /almost/ the same as writing:
function EvalCode(code) {
function TestEval1(x, y) {
return x + y;
}
}
That is, evaluation of the code creates a nested function.
I said 'almost the same' because, as you should know, the variable instantiation process would have function declarations evaluated before execution of the code begins. That is, TestEval1 would be available as a nested function at any point within the body of EvalCode. However, because the function declaration is evaluated using eval, it doesn't exist until execution of that function call. It's closer to:
function EvalCode(code) {
var TestEval1 = function(x, y) {
return x + y;
};
}
but still not exactly the same as this would create a local variable during the variable instantiation process.
but it will work for assigning a function literal to new (global)
variable (which is variable rather than function definition).
For comparison, this evaluation would produce the equivalent of:
function EvalCode(code) {
TestEval2 = function(x, y) {
return x - y;
};
}
Notice that this version does not include a var statement.
The variable, TestEval2, does not exist anywhere within the scope chain of the EvalCode function, therefore assignment will proceed (as normal) by creating a new global variable, and assigning to it a reference to the new function object.
I hope that helps,
Mike
--
Michael Winter
Prefix subject with [News] before replying by e-mail.
.
- Follow-Ups:
- Re: JavaScript global object
- From: Roman Ziak
- Re: JavaScript global object
- References:
- JavaScript global object
- From: Roman Ziak
- JavaScript global object
- Prev by Date: Re: video in javascript
- Next by Date: Javascript and special characters
- Previous by thread: Re: JavaScript global object
- Next by thread: Re: JavaScript global object
- Index(es):
Relevant Pages
|