简体   繁体   English

用于声明具有相同值的多个变量的速记或糖?

[英]Shorthand or sugar for declaring multiple variables with same value?

I was wondering if there is a shorthand in ES6 or Babel to declare multiple variables with the same value, eg when using multiple refs in React:我想知道 ES6 或 Babel 中是否有简写来声明具有相同值的多个变量,例如在 React 中使用多个 ref 时:

const a = React.useRef();
const b = React.useRef();
const c = React.useRef();
const d = React.useRef();
....

But also in vanilla JS when declaring an object with multiple keys with the value null:但在 vanilla JS 中,当声明具有多个值为 null 的键的 object 时:

const obj = { a: null, b:null, c:null, d:null }

So I thought maybe there is something like所以我想也许有类似的东西

const a, b, c, d, = React.useRef();

Thanks谢谢

There is no full-shorthand but You can make it a little simple just by writing it like this-没有完整的速记,但你可以通过这样写来让它变得有点简单 -

let [a,b,c,d] =  [React.useRef(),React.useRef(),React.useRef(),React.useRef()]

This will at least compress those codes in a line.这至少会将这些代码压缩成一行。

Does this work for you?这对你有用吗?

const a=b=c="some value";

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

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