我参考了JavaScript var hoisting
的文档,在一节中我发现了几个变量的初始化,下面给出了一个例子。
var x = 0;
function f(){
var x = y = 1;
}
f();
console.log(x, y); // outputs 0, 1
// x is the global one as expected
// y leaked outside of the function, though!
Where I Suppose get Exception as Uncaught Reference Error: y is not defined
。但没有发生,由于泄漏的范围,它显示0,1
。
我可以知道为什么它发生的细节和什么使它发生。最后还有什么与性能相关的问题吗?
您没有声明y
。
var x = y = 1;
等价于
y = 1;
var x = y; // actually, the right part is precisely the result of the assignement
未声明的变量是一个全局变量(除非你在严格模式下,否则它是一个错误)。
你提到的例子是不同的,有一个逗号,这是多重声明语法的一部分。
你可以在
中修改你的代码var y=1, x=y;