Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
445 views
in Technique[技术] by (71.8m points)

javascript - Clean way to keep original variable and destructure at the same time

Is there a cleaner way to do this (with anything that is at least an ES draft and has a babel plugin, i.e., ES6, ES7, etc.):

const { a, b } = result = doSomething();

Where I want to keep the overall result as one singular object, but also destructure it at the same time. It technically works, but result is implicitly declared (with an implicit var), while I'd really like it to also be a const.

I'm currently doing this:

const result = doSomething();
const { a, b } = result;

Which again works, but it's slightly on the verbose side, since I need to repeat this pattern dozens of times.

I'd ideally want something along the lines of:

const { a, b } = const result = doSomething();

But that is obviously an invalid syntax.

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

One possible way:

const result = doSomething(), 
    { a, b } = result;

You still have to duplicate the name, though. const token isn't quite right-handy. )


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...