这似乎是一个愚蠢的问题。假设我有一个接受对象的函数。如何将该对象转换为props
,但也可以解构props.id
为id
(在参数声明中)?
function go ({ id }) {
const props = arguments[0]; // how to do this with destructure?
console.log('props', props, 'id', id);
}
go({id: 2});
这似乎是一个愚蠢的问题。假设我有一个接受对象的函数。如何将该对象转换为props
,但也可以解构props.id
为id
(在参数声明中)?
function go ({ id }) {
const props = arguments[0]; // how to do this with destructure?
console.log('props', props, 'id', id);
}
go({id: 2});
你不能这样做 - 只需保留props
作为参数以使此代码更简单,更易于阅读:
function go (props) {
const { id } = props;
console.log('props', props, 'id', id);
}
go({id: 2});
您可以按照这种方法将参数命名为道具并解构该参数以提取 Id 的值。
当您需要传递额外的参数时,问题就来了。
function go (props, {id} = props) {
//const props = arguments[0]; // how to do this with destructure?
console.log('props', props, 'id', id);
}
go({id: 2});