我的应用程序中有多个案例,如下所示:
getVariables() {
const {
allowCustomValues,
budgets,
budgetsToAdd,
budgetsToRemove,
isGlobal,
isRequired,
name,
tagTypeId,
valuesToAdd,
valuesToDelete,
} = this.props;
return {
allowCustomValues,
budgets,
budgetsToAdd,
budgetsToRemove,
isGlobal,
isRequired,
name,
tagTypeId,
valuesToAdd,
valuesToDelete,
};
}
这看起来很麻烦,但更短的解决方案包括在我想要的每个变量之前加上this.props.
,从维护的角度来看,这并不是更好。
我想要的是这样的:
getVariables() {
return this.props.{
allowCustomValues,
budgets,
budgetsToAdd,
budgetsToRemove,
isGlobal,
isRequired,
name,
tagTypeId,
valuesToAdd,
valuesToDelete,
};
}
是否有某种 ES6 语法可以让我使这段代码更加干燥?
编辑:
可能是重复的return 语句中的 ES6 解构 https://stackoverflow.com/questions/41794086/es6-destructuring-within-a-return-statement?rq=1,但那个人有一个公认的答案,但不能解决问题。