Reading documentation online, I'm getting confused how to properly define multiple JavaScript variables on a single line.
在线阅读文档,我对如何在一行中正确定义多个JavaScript变量感到困惑。
If I want to condense the following code, what's the proper JavaScript "strict" way to define multiple javascript variables on a single line?
如果我想压缩以下代码,那么在一行中定义多个javascript变量的正确JavaScript“严格”方法是什么?
var a = 0;
var b = 0;
Is it:
是吗:
var a = b = 0;
or
要么
var a = var b = 0;
etc...
等等...
5 个解决方案
#1
57
You want to rely on commas because if you rely on the multiple assignment construct, you'll shoot yourself in the foot at one point or another.
你想依靠逗号,因为如果你依赖于多重赋值结构,你就会在一个或另一个角落射击自己。
An example would be:
一个例子是:
>>> var a = b = c = [];
>>> c.push(1)
[1]
>>> a
[1]
They all refer to the same object in memory, they are not "unique" since anytime you make a reference to an object ( array, object literal, function ) it's passed by reference and not value. So if you change just one of those variables, and wanted them to act individually you will not get what you want because they are not individual objects.
它们都引用内存中的同一个对象,它们不是“唯一的”,因为任何时候你引用一个对象(数组,对象文字,函数)它都是通过引用而不是值传递的。因此,如果您只更改其中一个变量,并希望它们单独行动,那么您将无法得到您想要的变量,因为它们不是单个对象。
There is also a downside in multiple assignment, in that the secondary variables become globals, and you don't want to leak into the global namespace.
多重赋值还有一个缺点,即辅助变量变为全局变量,并且您不希望泄漏到全局命名空间中。
(function() { var a = global = 5 })();
alert(window.global) // 5
It's best to just use commas and preferably with lots of whitespace so it's readable:
最好只使用逗号,最好使用大量的空格,以便它可读:
var a = 5
, b = 2
, c = 3
, d = {}
, e = [];
#2
36
There is no way to do it in one line with assignment as value.
没有办法在一行中将赋值作为值。
var a = b = 0;
makes b global. A correct way (without leaking variables) is the slightly longer:
让b全球化。正确的方法(没有泄漏变量)稍长:
var a = 0, b = a;
#3
18
Using Javascript's es6 or node, you can do the following:
使用Javascript的es6或节点,您可以执行以下操作:
var [a,b,c,d] = [0,1,2,3]
And if you want to easily print multiple variables in a single line, just do this:
如果您想在一行中轻松打印多个变量,只需执行以下操作:
console.log(a, b, c, d)
0 1 2 3
0 1 2 3
This is similar to @alex gray 's answer here, but this example is in Javascript instead of CoffeeScript.
这类似于@alex grey的答案,但这个例子是用Javascript而不是CoffeeScript。
#4
16
Why not doing it in two lines?
为什么不分两行呢?
var a, b, c, d; //All in the same scope
a = b = c = d = 1; // Set value to all.
The reason why, is to preserve the local scope on variable declarations, as this:
原因是,保留变量声明的局部范围,如下所示:
var a = b = c = d = 1;
will lead to the implicit declarations of b
, c
and d
on the window scope.
将导致窗口范围上的b,c和d的隐式声明。
#5
1
note you can only do this with Numbers and Strings
请注意,您只能使用数字和字符串执行此操作
you could do...
你可以做......
var a, b, c; a = b = c = 0; //but why?
c++;
// c = 1, b = 0, a = 0;
#1
57
You want to rely on commas because if you rely on the multiple assignment construct, you'll shoot yourself in the foot at one point or another.
你想依靠逗号,因为如果你依赖于多重赋值结构,你就会在一个或另一个角落射击自己。
An example would be:
一个例子是:
>>> var a = b = c = [];
>>> c.push(1)
[1]
>>> a
[1]
They all refer to the same object in memory, they are not "unique" since anytime you make a reference to an object ( array, object literal, function ) it's passed by reference and not value. So if you change just one of those variables, and wanted them to act individually you will not get what you want because they are not individual objects.
它们都引用内存中的同一个对象,它们不是“唯一的”,因为任何时候你引用一个对象(数组,对象文字,函数)它都是通过引用而不是值传递的。因此,如果您只更改其中一个变量,并希望它们单独行动,那么您将无法得到您想要的变量,因为它们不是单个对象。
There is also a downside in multiple assignment, in that the secondary variables become globals, and you don't want to leak into the global namespace.
多重赋值还有一个缺点,即辅助变量变为全局变量,并且您不希望泄漏到全局命名空间中。
(function() { var a = global = 5 })();
alert(window.global) // 5
It's best to just use commas and preferably with lots of whitespace so it's readable:
最好只使用逗号,最好使用大量的空格,以便它可读:
var a = 5
, b = 2
, c = 3
, d = {}
, e = [];
#2
36
There is no way to do it in one line with assignment as value.
没有办法在一行中将赋值作为值。
var a = b = 0;
makes b global. A correct way (without leaking variables) is the slightly longer:
让b全球化。正确的方法(没有泄漏变量)稍长:
var a = 0, b = a;
#3
18
Using Javascript's es6 or node, you can do the following:
使用Javascript的es6或节点,您可以执行以下操作:
var [a,b,c,d] = [0,1,2,3]
And if you want to easily print multiple variables in a single line, just do this:
如果您想在一行中轻松打印多个变量,只需执行以下操作:
console.log(a, b, c, d)
0 1 2 3
0 1 2 3
This is similar to @alex gray 's answer here, but this example is in Javascript instead of CoffeeScript.
这类似于@alex grey的答案,但这个例子是用Javascript而不是CoffeeScript。
#4
16
Why not doing it in two lines?
为什么不分两行呢?
var a, b, c, d; //All in the same scope
a = b = c = d = 1; // Set value to all.
The reason why, is to preserve the local scope on variable declarations, as this:
原因是,保留变量声明的局部范围,如下所示:
var a = b = c = d = 1;
will lead to the implicit declarations of b
, c
and d
on the window scope.
将导致窗口范围上的b,c和d的隐式声明。
#5
1
note you can only do this with Numbers and Strings
请注意,您只能使用数字和字符串执行此操作
you could do...
你可以做......
var a, b, c; a = b = c = 0; //but why?
c++;
// c = 1, b = 0, a = 0;