5

这似乎是一个愚蠢的问题。假设我有一个接受对象的函数。如何将该对象转换为props,但也可以解构props.idid(在参数声明中)?

function go ({ id }) {
  const props = arguments[0]; // how to do this with destructure?
  console.log('props', props, 'id', id);
}

go({id: 2});

4

2 回答 2

6

你不能这样做 - 只需保留props作为参数以使此代码更简单,更易于阅读:

function go (props) {
  const { id } = props;
  console.log('props', props, 'id', id);
}

go({id: 2});

于 2019-03-20T23:44:39.600 回答
3

您可以按照这种方法将参数命名为道具并解构该参数以提取 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});

于 2019-03-20T23:50:30.303 回答