我知道我可以执行 const { name: userName }= getName()
,然后 return { userName }
。
function returnName(){
return { name: "Jason" }
}
function getUserName(){
return {
userName : getName()
}
}
我可以在 getUserName
中只有一个返回 block 内销毁它吗?
function getUserName(){
return {
userName : {[getName()] : name} // I know it doesnt work, but want something like this
}
}
最佳答案
我认为您正在寻找一个简单的属性访问:
function getUserName() {
return {
userName: getName().name
};
}
不要让它变得比它需要的更复杂,你不需要在这里进行任何解构!
关于javascript - 从另一个返回对象中的函数的返回对象解构变量,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52679358/