问题
I am using the latest Firefox (4.0.1) and Firebug (1.7.2).
Any time I enter a variable declaration into the console, an italicized "undefined" warning is returned.
So for example if I enter "var x = 5;" then the response is "undefined", rather than "5".
Afterward if I enter "x" into the console, the proper value of 5 is returned. However the error/warning is a bit of a nuisance, would really like to know the cause and resolution, and if I'm the only one experiencing this.
Interestingly if I don't use "var" but just declare the value using "x=5" then the correct behavior exhibits and "5" is returned in the console.
回答1:
(This is just a guess, I'm not an expert on the details of Javascript's language rules or on Firebug.)
The feedback the console gives you is the result of the evaluation of the line you entered. I assume the declaration var x = ... is a statement that doesn't have a value, while simple assignment (x = ...) is, in line with the C heritage and the "everything is an expression" attitude of functional languages, an expression that evaluates to the assigned value.
回答2:
Firebug is reporting the result of evaluating the expression, equivalent to:
typeof eval("var x = 5;");
"undefined"
typeof eval("x = 5;");
"number"
来源:https://stackoverflow.com/questions/6297332/why-do-variable-declarations-in-the-console-keep-returning-undefined