开发者

In which order are variables assigned in Javascript?

开发者 https://www.devze.com 2023-03-28 01:30 出处:网络
Apparently this is identical in my Firebug console: var x = \"A\", y = x; x + y === \"AA\"; and var x = y, y = \"A\";

Apparently this is identical in my Firebug console:

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开发者_如何学JAVA var assignments?

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. If you run the JSFiddle provided by David Thomas you always get an "undefinedA". Case settled.


var x = y; will raise an exception if y is not defined.

However, the window object is the default context for Javascript interpreters embedded in browsers. If you previously issued:

y = "A";

Then you actually assigned "A" to window.y, therefore var x = y; becomes valid and assigns window.y to x.

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号