Shorter way to declare multiple variables in JavaS

2020-03-09 02:31发布

Is there a faster/more efficient way of declaring multiple variables in react native? Instead of

let foo = 'foo';
let bar = 'bar';
let foobar = 'foobar';

Obviously not problem for three variables, but for bigger sets of variables, is there a better way?

2条回答
走好不送
2楼-- · 2020-03-09 02:54

"Faster"? You've determined there's a performance bottleneck here?!

In any case, you can declare multiple variables:

let foo = 'foo'
  , bar = 'bar'
  , foobar = 'foobar'
  ;

But this is JS 101 and trivially searchable. What are you really asking?

Also, if you have a "large" number of variables, I suspect the problem is more systemic, and you're missing multiple types of refactorings.

查看更多
男人必须洒脱
3楼-- · 2020-03-09 03:13

This is more of a general JS syntax question.

Depending on your use case, you can just destructure an array as such:

const [ foo, bar, foobar ] = [ 'foo', 'bar', 'foobar' ]

Note that these are constants, having a lot of variables in a scope makes it poorly readable. See here for more

查看更多
登录 后发表回答