Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why are the rest parameters in JavaScript called so? [closed]

Why are rest parameters in JavaScript called so?

like image 322
Phi_1.618 Avatar asked Apr 25 '13 15:04

Phi_1.618


People also ask

What are rest parameters in JavaScript?

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.

What type of object is a rest parameter JavaScript?

The rest parameter, however, is a real array object. As such, you can use all array methods on it.

What is the rest operator in JavaScript?

The rest operator (…) allows us to call a function with any number of arguments and then access those excess arguments as an array. The rest operator also allows us in destructuring array or objects.

What is difference between spread and rest operator in JavaScript?

Rest Parameter collects all remaining elements into an array. Spread Operator expands collected elements such as arrays into single elements. The spread syntax only does a shallow copy one level deep. Only the last parameter can be a rest parameter.


2 Answers

They are called rest parameters because they capture the rest of the parameters when the function was invoked.

function multiply(multiplier, ...theArgs) {
  return theArgs.map(function (element) {
    return multiplier * element;
  });
}

Example from https://developer.mozilla.org/en-US/docs/JavaScript/Reference/rest_parameters#Browser_compatibility

...theArgs captures parameters 2+. In other words, multiplier is the first parameter and theArgs is the rest.

like image 119
Rick Viscomi Avatar answered Oct 19 '22 23:10

Rick Viscomi


The word "rest" is used there to mean a container for the rest of the argument values, of which there may be any number.

It's historical usage possibly starting with Lisp Machine Lisp, definitely documented in the 1981 third edition of the Lisp Machine Manual. There were no "rest parameters" by that behavior or that name in Maclisp or Interlisp, both in 1974. Rest parameters in Common Lisp at present have the same syntax as they had in the Lisp Machine Manual. http://www.lispworks.com/documentation/HyperSpec/Body/03_dac.htm

The phrase "rest parameters" is first introduced in relation to ECMAScript in the 2012-07-12 ECMAScript 6 draft. It seems clear that the phrase there is to be understood as common parlance previously established by Lisp. If it really matters, I suppose we could ask the secretary of ECMA Technical Committee 39, Dr. Istvan Sebestyen, whose address is his first name at ecma-international.org, whether anyone would be willing to say that in so many words.

like image 23
minopret Avatar answered Oct 19 '22 23:10

minopret