Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What's the JavaScript syntax to cast and destructure a param?

This seems like a stupid question. Say I have a function which accepts an object. How can I cast that object as props, but also destructure props.id to id (in the parameter declaration)?

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

go({id: 2});
like image 803
neaumusic Avatar asked Jan 02 '23 03:01

neaumusic


1 Answers

You can't do it - just keep props as an argument as well to make this code simpler and easier to read:

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

go({id: 2});
like image 160
Jack Bashford Avatar answered Jan 14 '23 15:01

Jack Bashford