javascript var a, b, c = {}

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/6941837/
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-25 22:25:25  来源:igfitidea点击:

var a, b, c = {}

javascriptobjectreferenceassignment-operator

提问by Wesley

I thought the syntax:

我认为语法:

var a, b, c = {};

would mean that the three variables are separate, not references to the same {}.

意味着这三个变量是分开的,而不是对同一个 {} 的引用。

Is this because {} is an object and this is the standard behavior?

这是因为 {} 是一个对象并且这是标准行为吗?

So if I do:

所以如果我这样做:

var a, b, c = 0;

the three would indeed be separate and not references?

这三个确实是分开的而不是引用?

Thanks, Wesley

谢谢,韦斯利

回答by Flambino

They shouldn't be the same, no. Only cwill be assigned the value.

他们不应该是一样的,不。只会c被赋值。

aand bwould just be declared, but not initialized to anything (they'd be undefined). cwould, as the only one of them, be initialized to {}

a并且b只会被声明,但不会被初始化为任何东西(它们是未定义的)。c作为其中唯一的一个,将被初始化为{}

Perhaps it's clearer when written on several lines:

分几行写可能更清楚:

var a,      // no assignment
    b,      // no assignment
    c = {}; // assign {} to c

回答by Kaken Bok

var a, b, c = {};

This will declare 3 variables (a, b, c) but define only 1 variable (c).

这将声明 3 个变量 (a, b, c) 但只定义 1 个变量 (c)。

var a, b, c = 0;

This will declare 3 variables (a, b, c) but define only 1 variable (c).

这将声明 3 个变量 (a, b, c) 但只定义 1 个变量 (c)。

回答by Rob Fox

In the examples you only define the last element

在示例中,您只定义了最后一个元素

aand bare undefined

a并且b是未定义的

回答by Joseph Marikle

var a, b, c = {};only defines the last var namely c. it's similar to the syntax var a = {}, b = [], c = 0;

var a, b, c = {};只定义最后一个 var 即c. 它类似于语法var a = {}, b = [], c = 0;

it's just short hand for declaring multiple vars.

它只是声明多个变量的简写。

回答by zatatatata

Using coma to separate variables enables to define multiple variables in the same scope more compactly. In your sample, though, only c = {}and aand bwould be undefined. You could use similar construct to assign something like

使用 coma 分隔变量可以更紧凑地在同一范围内定义多个变量。但是,在您的示例中,只有c = {}andab将是undefined。您可以使用类似的构造来分配类似的东西

var a = {first: 'foo'}, 
    b = {second: 'bar'}, 
    c = {third: 'foobar'};

which would be equal to

这将等于

var a = {first: 'foo'}, b = {second: 'bar'}, c = {third: 'foobar'};

and also to

还有

var a = {first: 'foo'};
var b = {second: 'bar'};
var c = {third: 'foobar'};

And each one would only reference to their respective object. Using comas is basically just a visual aspect.

而且每个人只会引用他们各自的对象。使用昏迷基本上只是一个视觉方面。

回答by Michael Taufen

Yes, the three would be separate.

是的,这三个是分开的。

The comma operator evaluates the left operand, then evaluates the right operand, then returns the value of the right operand.

逗号运算符计算左操作数,然后计算右操作数,然后返回右操作数的值。

This makes for a nice way to declare a bunch of variables in a row if you just rely on the evaluation of the operands and don't do anything with the returned value.

如果您仅依赖于操作数的计算并且不对返回值执行任何操作,这将是一种连续声明一堆变量的好方法。

var i = 0, j = 1, k = 2;is basically equivalent to var i = 0; var j = 1; var k = 2

var i = 0, j = 1, k = 2;基本上相当于 var i = 0; var j = 1; var k = 2

Another use for this is to squeeze multiple operations onto one line without using ;, like in a for loop with multiple loop variables:

另一个用途是将多个操作压缩到一行而不使用;,就像在具有多个循环变量的 for 循环中一样:

for(var x=0,y=5; x < y; x++,y--)

for(var x=0,y=5; x < y; x++,y--)

Notice how both x++ and y-- can be performed on the same line.

注意 x++ 和 y-- 是如何在同一行上执行的。