0

Can the following function (which is inside a class) be shortened:

getCurrentMoves(moves = this.state.moves, stepNumber = this.state.stepNumber) {
  return moves.slice(0, stepNumber);
}

All I managed to do is to refactor it to the following. However, this refactor changed the number of parameters from 2 to 1:

getCurrentMoves({moves, stepNumber} = this.state) {
  return moves.slice(0, stepNumber);
}

Is there a way to keep it 2 parameters and use default parameter syntax, as well as destructuring syntax in order to avoid code duplication?

Utku
  • 2,025
  • 22
  • 42
  • 2
    What you have now is readable and efficient. Don't change it. – trincot Aug 21 '20 at 21:25
  • @trincot I'm not planning to change it. Just wondering if this is possible with modern JavaScript. – Utku Aug 21 '20 at 21:26
  • 2
    @Utku You can think of arguments as arrays, so you would need to have a way to 1. know the order of the keys or 2. implement a way to pass named parameters python-style. As far as I know, this is not possible, and using an object as a parameter is the closest you'll get. – OFRBG Aug 23 '20 at 03:19

0 Answers0