简体   繁体   中英

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:

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:

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";

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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