Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Use the rest parameters instead of 'arguments' (prefer-rest-params)

Tags:

ecmascript-6

I can not solve this case - linter points me to other solution. This is my original code:

if (arguments[0] && typeof arguments[0] === 'object') {
  this.options = extendDefaultProperties(defaultProperties, arguments[0]);
}

Any help? Thank you in advance.

like image 454
be-codified Avatar asked Nov 01 '17 18:11

be-codified


People also ask

What is the difference between rest parameters and arguments object?

Rest parameters represent the unknown number of arguments inside the function, whereas the arguments object represents all arguments passed to the function. Rest parameters are pure array, whereas the arguments object is not a real array.

What is the purpose of rest parameters?

The rest parameter syntax allows a function to accept an indefinite number of arguments as an array, providing a way to represent variadic functions in JavaScript.

Does rest parameters are similar to variable arguments in Java?

The rest parameter and arguments object are different from each other. Let's see the difference between the rest parameter and the arguments object: The arguments object is an array-like (but not array), while the rest parameters are array instances.

What is rest parameter in typescript?

A rest parameter allows us to pass zero or any number of arguments of the specified type to a function. In the function definition where we specify function parameters rest parameters should always come at last or the typescript compiler will raise errors.


1 Answers

Use the rest parameters to collect the params into the array args:

demo(...args) {
  if (typeof args[0] === 'object' && args[0] !== null) {
    this.options = extendDefaultProperties(defaultProperties, args[0]);
  }
}
like image 194
Ori Drori Avatar answered Oct 09 '22 06:10

Ori Drori