Re: JavaScript global object



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.
.



Relevant Pages

  • Re: Are decorators really that different from metaclasses...
    ... >>declaration, just as we do with its docstring. ... Assignment in Python has quite ... > bodies are executed when the class statement is executed, ... Maybe we just need to be clear as to which lines under the def statement ...
    (comp.lang.python)
  • Re: C99: Suggestions for style(9)
    ... M. Warner Losh schrieb: ... It is worrisome that somebody is inclined to obfuscate the code (in this case replacing multiple variables with descriptive names by a single variable with a generic name) because it is less hassle to conform to stylethis way. ... So it is very convenient to have this single assignment and its declaration at the same place. ...
    (freebsd-hackers)
  • Re: Destructor that implies another destructor call (using new/delete)
    ... the assignment you have: ... First an array of pointers to X is created on the heap, ... For the 'explicit' keyword I used in the constructor overload, ... declared in A as a singular pointer vs your correct declaration: ...
    (microsoft.public.vc.language)
  • Re: extended operators
    ... this one would not reflect the spirit: first simplify C by removing ... maybe more clearly splitting out what is part of the declaration and what is ... context independence, if added, would require either eliminating typedefs, ... dos boot @C. ...
    (comp.std.c)
  • Re: Copying Aggregate Data Types in C
    ... assignment _and_ structure compare to work the same way. ... Force all padding to a known value such as everybodys favorite ... compare would include the padding for a net effect of nothing. ... a definition causes some code to be generated, while a declaration ...
    (comp.lang.c)