[英]In which order are variables assigned in Javascript?
Apparently this is identical in my Firebug console:显然这在我的 Firebug 控制台中是相同的:
var x = "A", y = x;
x + y === "AA";
and和
var x = y, y = "A";
x + y === "AA";
Is this standard ECMAScript behaviour, that the order doesn't play a role in comma-separated var
assignments?这是标准的 ECMAScript 行为,订单在逗号分隔的
var
分配中不起作用吗?
Edit: The "mystery" is solved.编辑: “谜”解开了。 I tested the first example first, then cleared the console and ran the second.
我首先测试了第一个示例,然后清除了控制台并运行了第二个。 However, at this time,
y
and x
were already defined.但是,此时
y
和x
已经定义。 If you run the JSFiddle provided by David Thomas you always get an "undefinedA".如果你运行 David Thomas 提供的 JSFiddle,你总是会得到一个“undefinedA”。 Case settled.
案件结案。
var x = y;
will raise an exception if y
is not defined.如果
y
未定义,将引发异常。
However, the window object is the default context for Javascript interpreters embedded in browsers.但是, window object 是嵌入在浏览器中的 Javascript 解释器的默认上下文。 If you previously issued:
如果您之前发布过:
y = "A";
Then you actually assigned "A"
to window.y
, therefore var x = y;
然后您实际上将
"A"
分配给window.y
,因此var x = y;
becomes valid and assigns window.y
to x
.变为有效并将
window.y
分配给x
。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.