Dear Lazzie
|
2005-09-28Abject expressionHenry and I were looking at the debugger and wondering why:
In the first case, a function named ‘foo’ is created, but it is not the value of the global It turns out the debugger evaluator will first try to evaluate what you type as an expression, and if that fails, it will try to evaluate it as a statement. The second form is clearly not an expression (think of what you type as having parens around it — the semi-colon forces the second form to be a statement list, with an empty second statement). It turns out that this is correct ECMA semantics. It is not whether you name a function or not that determines when a global definition will be made; two things must be true: 1) the function must be named, and 2) the function declaration must occur in a statement context. If the function declaration occurs in an expression context, all you have done is to create a named function object as the value of that expression, you have not defined that name as the function… Subtle. There is a bug in the current debugger, though, because:
In the case of But in the mean time, if you ever wonder why when you define a function in the debugger it does not take, take a clue from what the evaluator prints out: if it prints out a function object, all you did was create a function, not define a function. Add the semi-colon after your definition and you will define the function (and not see a value printed). |
|