admin 管理员组文章数量: 1087649
On executing
var a=b=c=d=e=f=a;
//no error(a has not initialize before)
var x=y;
//ReferenceError: y is not defined
How can the first code just execute as if a
has already been initialize before.
On executing
var a=b=c=d=e=f=a;
//no error(a has not initialize before)
var x=y;
//ReferenceError: y is not defined
How can the first code just execute as if a
has already been initialize before.
-
5
first statement is like
var a = undefined; window.b=window.c=window.d=window.e=window.f=a;
in secondy is not defined
– rab Commented Jul 8, 2013 at 7:03 - 1 @rab just post that as an answer. It's the correct one – rossipedia Commented Jul 8, 2013 at 7:04
2 Answers
Reset to default 11It's because of variable hoisting. var x = EXPR;
is actually converted to this:
// beginning of the block (function/file)
var x; // === undefined
// ...
// the actual position of the statement
x = EXPR
For your example this means:
var a; // === undefined
a = b = c = d = e = f = a;
Note that only a
is declared using var
- so you are creating tons of globals which is always a bad thing!
Your first statement is like
var a = undefined;
a = window.b = window.c = window.d = window.e = window.f = a;
where a is defined, and others are global scoped . suppose you execute a function .
(function(){
var a=b=c=d=e=f=a;
b = 10;
}());
the b
can accessed outside .
in second var x=y
, y
is not defined yet
本文标签: javascriptAssigning the variable name to the same variable nameStack Overflow
版权声明:本文标题:javascript - Assigning the variable name to the same variable name - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://roclinux.cn/p/1744084342a2530837.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论