Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

what is ... syntax? (found in Angular2)

Tags:

angular

I don't know if this syntax or symbol or whatever belongs to Angular2 or something else...

but I wonder with,

what ... syntax is and how it works behind the scene?

ngOnChanges(...args:any[])
{
   console.log(args);
}
like image 967
Nikhil Shah Avatar asked Mar 15 '16 10:03

Nikhil Shah


People also ask

What is the language of the Angular template?

HTML is the language of the Angular template.

What is the use of !: In Angular?

The exclamation mark ! tells, that the field is non-null non-undefined (see Microsoft's TypeScript documentation).

What is template binding in Angular?

The template can change the value of the data-bound class property and the class can change the value of the property in the template. The template and the class are bound to each other, any change in either of them results in data updates in both of them. Example import { Component } from '@angular/core';@Component({

What is operator in Angular?

An operator is a pure function which takes in observable as input and the output is also an observable. To work with operators we need a pipe() method.


1 Answers

It's the TypeScript rest operator. In this case, it means any number of arguments of any type can occur there; the function will see them as an array of any. (JavaScript recently got rest and spread operators as well, as of ES2015, but the :any[] in your example tells us this is TypeScript.)

E.g.:

ngOnChanges('a', 42, null);

...would show

["a", 42, null]

in the console.

Here's a complete example (live copy):

function foo(...args:any[]) {
    console.log("args.length = " + args.length);
    args.forEach((arg:any, index:Number) => {
        console.log("args[" + index + "]: " + arg);
    });
}
foo('a', 42, null);

outputs

args.length = 3
args[0]: a
args[1]: 42
args[2]: null
like image 131
T.J. Crowder Avatar answered Sep 18 '22 02:09

T.J. Crowder