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?
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.
(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.
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.
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];
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With