解构 JS 中的不同类型
Destructuring different types in JS
对于一个对象x
const x = {
a: 'foo',
b: () => { return 'bar' )
}
是否可以解构 x
以在一步中将 a
和 b
作为字符串获取?
{a, <something with b?>} = x
console.log(a, b) // 'foo bar'
在更广泛的范围内,我对如何干净地解构单个对象中包含的不同类型感到困惑
在现实世界中,a
是调用,b
是嵌套对象
const x = {
a: c(), // returns an object
b: {
x: {...},
y: () => {...}
} // is an object
}
// is this even doable
{< some magic stuff >} = x
typeof a === typeof b //true
AFAIK,只有 property getter 才有可能,如果这是一个选项:
const x = {
a: 'foo',
get b() { return 'bar' }
}
const {a, b} = x
console.log(a, b) // 'foo bar'
对于一个对象x
const x = {
a: 'foo',
b: () => { return 'bar' )
}
是否可以解构 x
以在一步中将 a
和 b
作为字符串获取?
{a, <something with b?>} = x
console.log(a, b) // 'foo bar'
在更广泛的范围内,我对如何干净地解构单个对象中包含的不同类型感到困惑
在现实世界中,a
是调用,b
是嵌套对象
const x = {
a: c(), // returns an object
b: {
x: {...},
y: () => {...}
} // is an object
}
// is this even doable
{< some magic stuff >} = x
typeof a === typeof b //true
AFAIK,只有 property getter 才有可能,如果这是一个选项:
const x = {
a: 'foo',
get b() { return 'bar' }
}
const {a, b} = x
console.log(a, b) // 'foo bar'