Re: What do people use Venkman for?
"7stud" <[email protected]>
| Newsgroups | gmane.comp.mozilla.devel.jsdebugger |
|---|---|
| Message-ID | <[email protected]> |
Hi,
I wanted to post this bit of code that demonstrates javascript's "look
ahead" feature and is the reason for Venkman's seemingly strange behavior
when stepping through code:
----------------------
var b = "global b";
function f()
{
alert(b); //void
var b = "local b";
}
f();
-----------------------
If you try that code, you will see that 'b' is void. How is that possible if
the local 'b' hasn't even been declared yet? Why isn't the global 'b'
displayed? Because javascript looks ahead for variable declarations, so the
local 'b' exists when the alert() statement is executed, and it hides the
global 'b', but the local 'b' hasn't been assigned a value yet, so it's
void.
So, when you step through code, Venkman follows javascript as it goes
looking for variable declarations, and you have to go through all that
before the script can begin execution. Personally, I think Venkman should
be programmed to silently fetch all the declarations, and then when you
click on Step Into, it should start following the javascript steps that
occur after javascript has created all the variables.
"Martin Honnen" <[email protected]> wrote in message
news:[email protected]...
>
>
> 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.
>
> --
>
> Martin Honnen
> http://JavaScript.FAQTs.com/