Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What do the three dots before a function argument represent?

Tags:

I was working with Laravel 5.3 and in one of the functions, I found this piece of code:

public function handle($request, Closure $next, ...$guards) {     $this->authenticate($guards);      return $next($request); } 

The code comes from \Illuminate\Auth\Middleware\Authenticate::class.

What are those 3 dots before $guards variable?

like image 515
Salar Avatar asked Nov 05 '16 15:11

Salar


People also ask

What is the significance of the three dots in this function signature?

operator in it, and it basically means " ... and everything else should go into $strings ". You can pass 2 or more arguments into this function and the second and subsequent ones will be added to the $strings array , ready to be used.

What does the 3 dots mean in JavaScript?

(three dots in JavaScript) is called the Spread Syntax or Spread Operator. This allows an iterable such as an array expression or string to be expanded or an object expression to be expanded wherever placed. This is not specific to React. It is a JavaScript operator.

What do 3 dots above and below code mean?

When we see three dots (…) in the code, it's either rest parameters or the spread operator. There's an easy way to distinguish between them: When three dots (…) is at the end of function parameters, it's "rest parameters" and gathers the rest of the list of arguments into an array.

What does 3 dots mean in Typescript?

The three dots are known as the spread operator from Typescript (also from ES7). The spread operator return all elements of an array. Like you would write each element separately: let myArr = [1, 2, 3]; return [1, 2, 3]; //is the same as: return [...myArr];


1 Answers

It indicates that there may be a variable number of arguments.

When the function is called with more than 3 arguments, all the arguments after $next will be added to the $guards array.

You can read about it here.

like image 85
Daniel Enache Avatar answered Oct 01 '22 10:10

Daniel Enache