What is the meaning of “foo(…arg)” (three dots in a function call)?

后端 未结 4 2037
南旧
南旧 2021-01-12 07:30

Can someone tell what is \"...\" in the below code in the \"Intro to Angular\" example?

getHeroes() {
    this.backend.getAll(Hero).then( (heroes: Hero[]) =&         


        
4条回答
  •  挽巷
    挽巷 (楼主)
    2021-01-12 07:56

    It is a JavaScript feature called Rest Parameters. By using it your function can take any number of arguments. You put the three dots just before the argument (without a whitespace character) and the mechanism spreads it for you as if it was a list of several arguments. In Eloquent Javascript you have a good example of it.

    let numbers = [5, 1, 7];
    console.log(max(...numbers));
    // -> 7
    

提交回复
热议问题