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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-28 07:20:42  来源:igfitidea点击:

Why a is undefined while b is 3 in var a=b=3?

javascript

提问by Hans Qi

In the following code, I expected both aand bto be 3. However, ais undefinedand bis 3. Why?

在下面的代码中,我希望ab都是3. 然而,aisundefinedbis 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 varstatement applies only to a, and not to b. You can check the syntax of varstatement here.

Andvar声明仅适用于a,不适用于b。您可以在此处检查var语句的语法。

Therefore awill be defined in local scope and bwill be defined in global scope. Inside function both aand bare 3 but after function returns registered local variable (a) is deleted. Since bis defined in global scope it is not deleted.

因此a将在本地范围内b定义,并将在全局范围内定义。在函数内部ab都是 3 但在函数返回后注册的局部变量 ( a) 被删除。由于b是在全局范围内定义的,因此不会被删除。

回答by Leo

aIS 3 indeed, but it's within the scope of the anounymous fucntion.

aIS 3确实是,但它在匿名功能的范围内。

this line var a = b = 3is actually:

这一行var a = b = 3实际上是:

  1. b = 3, whereas bis declared as a global variable.

  2. return value of the assignment express b=3is, yes, the assigned value 3.

  3. var a = the return value of previous expression, thus equals var a = 3, but this time ais a local variable.

  1. b = 3, 而b被声明为全局变量。

  2. 赋值表达式的返回值b=3是,是的,赋值3

  3. var a = the return value of previous expression,因此等于var a = 3,但这次a是局部变量。

Outside the anounymous function, bis accessible, but adoesn'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 ais unrecognized as it is not in global scope. BUT bis treated as global. varis 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