定义var时未定义 [英] undefined thrown when defining var
问题描述
下一个代码在Google-chrome控制台中进行解释:
The next code is interpreted in Google-chrome console:
a = 123
123
% Ok!
var b = 123
undefined
% `undefined`? why? b is not undefined, it contains `123`.
推荐答案
它显示了操作的结果。
赋值表达式的结果是赋值的值。但是 var
语句不会产生结果,这不仅仅是因为它们不会出现在通常的代码流程中。我们可以在spec中的 var
语句的定义中看到:§13.3.2.4告诉我们 var
是抽象规范调用 NormalCompletion(空)
,它是只是的完整别名
完成{[[type]]:normal,[[value]]:argument,[[target]] :empty} 这是指出如何完成一个陈述/表达。在这种情况下,它完成没有价值。 (所有的JavaScript语句都不是这样,令人惊讶的是。)
The result of an assignment expression is the value that was assigned. But var
statements don't have a result, not least because they don't occur within the usual step-by-step flow of code. We can see this in the definition of the var
statement in the spec: §13.3.2.4 tells us that the result of var
is the abstract specification call NormalCompletion(empty)
, which is just an alias for Completion{[[type]]: normal, [[value]]: argument, [[target]]:empty}
which is for indicating how a statement/expression completed. In this case, it completes with no value. (Which isn't true of all JavaScript statements, surprisingly.)
这篇关于定义var时未定义的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!