Javascript 如何在一行上定义多个变量?

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

Javascript How to define multiple variables on a single line?

javascript

提问by HeatherK

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...

等等...

回答by meder omuraliev

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 = [];

回答by modulitos

Using Javascript's es6 or node, you can do the following:

使用 Javascript 的 es6 或 node,您可以执行以下操作:

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 gray 在此处的回答,但此示例使用 Javascript 而不是 CoffeeScript。

Note that this uses Javascript's array destructuring assignment

请注意,这使用了 Javascript 的数组解构赋值

回答by Matthew Flaschen

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;

which is useful in the case:

这在以下情况下很有用:

var a = <someLargeExpressionHere>, b = a, c = a, d = a;

回答by Nick Louloudakis

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, cand don the window scope.

将导致b,cd在窗口范围内的隐式声明。

回答by Prince Ahmed

Here is the new ES6 method of declaration multiple variables in one line:

这是在一行中声明多个变量的新 ES6 方法:

const person = { name: 'Prince', age: 22, id: 1 };

let {name, age, id} = person;

console.log(name);
console.log(age);
console.log(id);

* Your variable name and object index need be same

*你的变量名和对象索引必须相同

回答by Dan Dascalescu

Specifically to what the OP has asked, if you want to initialize N variables with the same value (e.g. 0), you can use array destructuringand Array.fillto assign to the variables an array of N 0s:

特别是 OP 所要求的,如果你想用相同的值(例如0)初始化 N 个变量,你可以使用数组解构Array.fill为变量分配一个 N 数组0

let [a, b, c, d] = Array(4).fill(0);

console.log(a, b, c, d);

回答by PDA

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;