简体   繁体   English

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

[英]Javascript How to define multiple variables on a single line?

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

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 的数组解构赋值

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

There is no way to do it in one line with assignment as value.没有办法在一行中将赋值作为值来完成。

var a = b = 0;

makes b global.使 b 全球化。 A correct way (without leaking variables) is the slightly longer:正确的方法(没有泄漏变量)是稍长的:

var a = 0, b = a;

which is useful in the case:这在以下情况下很有用:

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

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.将导致bcd在窗口范围内的隐式声明。

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 *你的变量名和对象索引必须相同

Specifically to what the OP has asked, if you want to initialize N variables with the same value (eg 0 ), you can use array destructuring and Array.fill to assign to the variables an array of N 0 s:特别是OP所要求的,如果你想用相同的值(例如0 )初始化 N 变量,你可以使用数组解构Array.fill将 N 0 s的数组分配给变量:

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

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;

This is completely correct:这是完全正确的:

var str1 = str2 = str3 = "value";

And if change one of their value, the value of other variables won't change:如果更改其中一个值,其他变量的值将不会更改:

 var str1 = str2 = str3 = "value"; /* Changing value of str2 */ str2 = "Hi Web!"; document.write("str1 = " + str1 + " - str2 = " + str2 + " - str3 = " + str3);

do this if they have same value如果它们具有相同的值,请执行此操作

let x = y = z = 0

otherwise否则

let [x, y, z] = [10, 30, 50]
console.log(x, y, z) // 10 30 50

The compressed type of that is here:它的压缩类型在这里:

var a, b = a = "Hi";

& for 3 variables: & 对于 3 个变量:

var x, y, z = x = y = "Hello";

Hope to be helpful!希望能有所帮助!

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM