gpt4 book ai didi

javascript - 重构解构语句

转载 作者:行者123 更新时间:2023-12-03 11:14:50 25 4
gpt4 key购买 nike

const question = this.props.question
const answer = question.answer

重构上面的代码片段以使用解构。
const { question } = this.props
const { answer } = question

如何将这种解构重构为一行?
如果我这样做, const { question : { answer } } = this.props , 我不会得到 question 的引用?

最佳答案

解构时可以多次引用同一个属性:

const { question, question: { answer } } = this.props;

const props = {
question: {
answer: 'answer'
}
};
const { question, question: { answer } } = props;
console.log(question);
console.log(answer);

关于javascript - 重构解构语句,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58928318/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com