javascript 为什么在 var a=b=3 中 a 未定义而 b 为 3?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/27329444/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Why a is undefined while b is 3 in var a=b=3?
提问by Hans Qi
In the following code, I expected both a
and b
to be 3
. However, a
is undefined
and b
is 3
. Why?
在下面的代码中,我希望a
和b
都是3
. 然而,a
isundefined
和b
is 3
。为什么?
(function(){
var a = b = 3;
})();
console.log(typeof a);//"undefined"
console.log(b);//3
回答by user2466202
The issue here is that most developers understand the statement var a = b = 3;
to be shorthand for:
这里的问题是大多数开发人员都理解该语句var a = b = 3;
是以下的简写:
var b = 3;
var a = b;
But in fact, var a = b = 3; is actually shorthand for:
但实际上,var a = b = 3; 实际上是以下的简写:
b = 3;
var a = b;
Therefore, b ends up being a global variable (since it is not preceded by the var keyword) and is still in scope even outside of the enclosing function.
因此, b 最终成为一个全局变量(因为它前面没有 var 关键字)并且即使在封闭函数之外仍然在范围内。
The reason a is undefined is that a is a local variable to that self-executing anonymous function
a 未定义的原因是 a 是该自执行匿名函数的局部变量
(function(){
var a = b = 3;
})();
回答by Piotr Dabkowski
var a=b=3
Is the same as:
是相同的:
var a = (b = 3)
And var
statement applies only to a
, and not to b
. You can check the syntax of var
statement here.
Andvar
声明仅适用于a
,不适用于b
。您可以在此处检查var
语句的语法。
Therefore a
will be defined in local scope and b
will be defined in global scope. Inside function both a
and b
are 3 but after function returns registered local variable (a
) is deleted. Since b
is defined in global scope it is not deleted.
因此a
将在本地范围内b
定义,并将在全局范围内定义。在函数内部a
和b
都是 3 但在函数返回后注册的局部变量 ( a
) 被删除。由于b
是在全局范围内定义的,因此不会被删除。
回答by Leo
a
IS 3 indeed, but it's within the scope of the anounymous fucntion.
a
IS 3确实是,但它在匿名功能的范围内。
this line var a = b = 3
is actually:
这一行var a = b = 3
实际上是:
b = 3
, whereasb
is declared as a global variable.return value of the assignment express
b=3
is, yes, the assigned value3
.var a = the return value of previous expression
, thus equalsvar a = 3
, but this timea
is a local variable.
b = 3
, 而b
被声明为全局变量。赋值表达式的返回值
b=3
是,是的,赋值3
。var a = the return value of previous expression
,因此等于var a = 3
,但这次a
是局部变量。
Outside the anounymous function, b
is accessible, but a
doesn't even exist, due to not defined in global scope.
在匿名函数之外,b
可以访问,但a
由于未在global scope 中定义,因此甚至不存在。
回答by Riad
I wrote in the following style:
我写了以下风格:
console.log(typeof b);//"number"
console.log(a);// error with a is not defined
So in case of yours a
is unrecognized as it is not in global scope. BUT b
is treated as global. var
is used to declare a local variable.
因此,如果您的a
情况无法识别,因为它不在全球范围内。BUTb
被视为全球性的。var
用于声明局部变量。
回答by tsuz
Compare the code below
比较下面的代码
'use strict'
(function(){
var a = b = 3; // ERROR. Prevents global variable name clash
})();
And
和
(function(){
var a = b = 3; // Silently assigns 3 to a global variable b
})();
console.log(typeof a);//"undefined"
console.log(b);// 3