Martin Honnen wrote:
Robert North wrote:
I think the debugger is working, but it does a first pass over the
code, creating global variables, and setting them to null.
Then, it goes back and runs the code *properly*.
Well check the ECMAScript specification, it is *proper* execution to
first processs variable declarations to create variables. You can find
details about that in the section "12.2 Variable statement" of the
ECMAScript edition 3 specification where it says
"Variables are created when the execution scope is entered. A Block
does not define a new execution
scope. Only Program and FunctionDeclaration produce a new scope.
Variables are initialised to undefined
when created. A variable with an Initialiser is assigned the value of
its AssignmentExpression when the
VariableStatement is executed, not when the variable is created."
So what you see in Venkman is simply the script engine creating the
variables first and initializing them to undefined, there is nothing
wrong with that.
This is different from languages like Java where variables have block
scope. In JavaScript/ECMAScript you can even access a variable before
its declaration e.g.
if (x == 3) {
print(x);
}
var x;
as the execution of that program first looks for variable declarations
and creates variables. You could say that in ECMAScript variable
declarations are "hoisted" to the beginning of the execution scope.
Thank you Martin.
I think that's about a concise and definitive answer as anyone could expect.
I think that lays this issue to rest once and for all.
Cheers
-Rob.
|