为什么控制台上的JavaScript变量声明导致“未定义”?正在印刷? [英] Why does JavaScript variable declaration at console results in "undefined" being printed?
问题描述
我已阅读以下SO帖子:
I have already read the following SO posts:
但这些都没有解释为什么JavaScript控制台打印当我按如下方式声明变量时未定义:
But none of it explains why the JavaScript console prints undefined when I declare a variable as follows:
var a;
推荐答案
它打印此表达式的结果 - undefined
。是的, var a
本身就是一个有效的表达式。
It prints the result of this expression - which is undefined
. And yes, var a
is a valid expression on its own.
实际上,你应该选择当你写 var a = 3 $ c时,为什么
console
打印 undefined
会很开心$ c>或类似的东西。如果处理函数anyFunctionName(){}
语句,它还会打印 undefined
。事实上,如果有另一个声明,那么所有 var
和函数
声明(!)语句似乎都会被忽略真实的结果:
Actually, you should rather be amused by why console
prints undefined
when you write var a = 3
or something like this. It also prints undefined
if function anyFunctionName() {}
statement is processed. In fact, all the var
and function
declaration (!) statements seem to be ignored if there's another statement with some 'real' result:
>>> var a = 3;
undefined
>>> var a = 3; a = 4;
4
>>> var a = 3; a = 4; var a = 5; function f() {};
4 // !!!
现在,我认为背后的真正原因是 eval的行为
声明,如 此处 :
Now, I suppose the real reason behind is behaviour of eval
statement, as described here:
- 让
结果
成为评估程序prog
。
- 如果
result.type
是正常
,其完成值为值V
,然后返回值V
。
- 如果
result.type
是正常
,其完成值为清空
,然后返回值undefined
。
- Let
result
be the result of evaluating the programprog
.- If
result.type
isnormal
and its completion value is avalue V
, then return thevalue V
.- If
result.type
isnormal
and its completion value isempty
, then return the valueundefined
.
现在的问题是, var a = 4
语句返回是什么?猜猜看:它不是4。
So now the question is, what does var a = 4
statement return? Guess what: it's not 4.
生产 VariableStatement : var VariableDeclarationList; 是
评估如下:
The production VariableStatement : var VariableDeclarationList; is evaluated as follows:
- 评估VariableDeclarationList。
- 返回(正常,空,空))。
现在最有趣的部分:上一个例子中发生了什么,为什么4结果呢?这在此部分中有解释:
Now the most interesting part: what happened in the last example, why 4 is the result? That's explained in this section:
生产程序: SourceElements 的计算方法如下:
- 让结果成为评估SourceElements的结果。
[...]
生产 SourceElements : SourceElements * SourceElement *评估如下:
The production SourceElements : SourceElements *SourceElement* is evaluated as follows:
- 让headResult成为评估SourceElements的结果。
- 如果headResult突然完成,则返回headResult。
- 让tailResult是评估SourceElement的结果。
- 如果tailResult.value为空,则让V = headResult.value,否则让V => tailResult.value。
- 返回(tailResult.type,V,tailResult.target)
- Let headResult be the result of evaluating SourceElements.
- If headResult is an abrupt completion, return headResult.
- Let tailResult be result of evaluating SourceElement.
- If tailResult.value is empty, let V = headResult.value, otherwise let V = > tailResult.value.
- Return (tailResult.type, V, tailResult.target)
两个函数f(){}
和 var a = 5
语句'返回值(正常,空,空)
。所以脚本最终给出了第一个语句的结果(从脚本的结尾开始,技术上它是最后一个),它不是(正常,空,空)
。这是 a = 4
赋值语句的结果 - 4
。
Both function f() {}
and var a = 5
statements' return values were (normal, empty, empty)
. So the script ended up with giving out the result of the first statement (starting from the script's end, so technically it's the last one) that's not (normal, empty, empty)
. That is the result of a = 4
assignment statement - which is 4
.
PS现在有些锦上添花:请考虑以下因素:
P.S. And now for some icing on the cake: consider the following:
>>> function f() {}
undefined
>>> (function f() {})
function f() {}
区别在于非常微妙:第一个输入被视为函数声明
语句,根据此规则......
The difference is quite subtle: the first input is treated as a Function Declaration
statement, which, according to this rule...
生产 SourceElement : FunctionDeclaration 评估为
如下:
The production SourceElement : FunctionDeclaration is evaluated as follows:
- 返回(正常,空,空)。
..我们已经知道,当 eval
-ed时,最终会产生 undefined
。
... will eventually produce undefined
when eval
-ed, as we already know.
然而,第二个输入被视为函数表达式
,其被评估到函数本身。这意味着它将通过 eval
传递,并最终返回到控制台(格式)。
The second input, however, is treated as a Function Expression
, which is evaluated to the function itself. That means it'll be passed through eval
and eventually returned to the console (in its format).
这篇关于为什么控制台上的JavaScript变量声明导致“未定义”?正在印刷?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!