Javascript如何在一行上定义多个变量? [英] Javascript How to define multiple variables on a single line?
问题描述
在线阅读文档,我很困惑如何在一行上正确定义多个JavaScript变量.
Reading documentation online, I'm getting confused how to properly define multiple JavaScript variables on a single line.
如果我想压缩以下代码,那么在一行上定义多个javascript变量的正确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?
var a = 0;
var b = 0;
是吗
var a = b = 0;
或
var a = var b = 0;
等...
推荐答案
使用Javascript的es6或节点,您可以执行以下操作:
Using Javascript's es6 or node, you can do the following:
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
这与@alex gray的答案类似,但是此示例使用Javascript而不是CoffeeScript.
This is similar to @alex gray 's answer here, but this example is in Javascript instead of CoffeeScript.
请注意,这使用了Javascript的数组解构赋值
Note that this uses Javascript's array destructuring assignment
这篇关于Javascript如何在一行上定义多个变量?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!