JavaScript var 关键字:在闭包内重新定义变量的值
JavaScript var keyword: redefining a variable's value inside a closure
我在这里错过了什么?这符合预期:
var x = 1;
(function(){
// x === 1
})();
但是,
var x = 1;
(function(){
var x = x;
// x is undefined
})();
我认为 x 应该是 1
。似乎 var x = x
在 x 被赋值之前核对了它的值。这是一个错误吗?这看起来不是很直观。
这种行为改变了吗?我记得以前做过一件事like this
供参考:
var x = 1;
(function(){
var y = x;
// y === 1
})();
并且:
var x = 1;
(function(){
x = x;
// x === 1
})();
你在这里所做的是将 x 设置为全局变量,然后将其设置为引用自身,覆盖 1,使 x => x 成为未定义的。
每次键入 var 时,您都在重新分配一个新变量。当您在函数内部引用 x 时,它会查找上面的赋值语句。
function f(){
var x = x; //this tries to reassign var x to undefined.
}
var x = 1;
(function(){
var x = x;
})();
var x = 1;
(function(){
var x;
x = x;
})();
https://developer.mozilla.org/en/docs/Web/JavaScript/Reference/Statements/var
Because variable declarations (and declarations in general) are processed before any code is executed, declaring a variable anywhere in the code is equivalent to declaring it at the top. This also means that a variable can appear to be used before it's declared. This behavior is called "hoisting", as it appears that the variable declaration is moved to the top of the function or global code.
这就是为什么有时您会在插件中看到类似
的代码
var i,j,abc, d;
//code
在你的例子中,代码被转换成这样:
function() {
var x;
x = x;
}
带有函数参数的示例不同,您只需更改函数参数本身,var
声明将被忽略。
如果用 let
声明了作用域变量,它只会向上移动到该作用域的开头,而不是函数的开头,因此此代码有效:
var x = 1;
(function(){
var y = x;
{
let x = y;
console.log(x);
}
})();
如前所述,这是一项新功能,因此 not supported everywhere。
最后,在这里:
var x = 1;
(function(){
x = x;
// x === 1
})();
你没有在本地声明 x
,所以如果你编辑它,它也会在全局范围内编辑它。
In JavaScript 在全局范围内或在 whole 函数范围内声明的所有变量。考虑示例:
var x = 1;
function f()
{
console.log(x);
if (true) {
var x;
}
}
f();
这是一种奇怪的编程语言设计,但由于这条规则,这段代码也会打印 "undefined"。
我在这里错过了什么?这符合预期:
var x = 1;
(function(){
// x === 1
})();
但是,
var x = 1;
(function(){
var x = x;
// x is undefined
})();
我认为 x 应该是 1
。似乎 var x = x
在 x 被赋值之前核对了它的值。这是一个错误吗?这看起来不是很直观。
这种行为改变了吗?我记得以前做过一件事like this
供参考:
var x = 1;
(function(){
var y = x;
// y === 1
})();
并且:
var x = 1;
(function(){
x = x;
// x === 1
})();
你在这里所做的是将 x 设置为全局变量,然后将其设置为引用自身,覆盖 1,使 x => x 成为未定义的。
每次键入 var 时,您都在重新分配一个新变量。当您在函数内部引用 x 时,它会查找上面的赋值语句。
function f(){
var x = x; //this tries to reassign var x to undefined.
}
var x = 1;
(function(){
var x = x;
})();
var x = 1;
(function(){
var x;
x = x;
})();
https://developer.mozilla.org/en/docs/Web/JavaScript/Reference/Statements/var
Because variable declarations (and declarations in general) are processed before any code is executed, declaring a variable anywhere in the code is equivalent to declaring it at the top. This also means that a variable can appear to be used before it's declared. This behavior is called "hoisting", as it appears that the variable declaration is moved to the top of the function or global code.
这就是为什么有时您会在插件中看到类似
的代码var i,j,abc, d;
//code
在你的例子中,代码被转换成这样:
function() {
var x;
x = x;
}
带有函数参数的示例不同,您只需更改函数参数本身,var
声明将被忽略。
如果用 let
声明了作用域变量,它只会向上移动到该作用域的开头,而不是函数的开头,因此此代码有效:
var x = 1;
(function(){
var y = x;
{
let x = y;
console.log(x);
}
})();
如前所述,这是一项新功能,因此 not supported everywhere。
最后,在这里:
var x = 1;
(function(){
x = x;
// x === 1
})();
你没有在本地声明 x
,所以如果你编辑它,它也会在全局范围内编辑它。
In JavaScript 在全局范围内或在 whole 函数范围内声明的所有变量。考虑示例:
var x = 1;
function f()
{
console.log(x);
if (true) {
var x;
}
}
f();
这是一种奇怪的编程语言设计,但由于这条规则,这段代码也会打印 "undefined"。