2017-02-08 3 views
7

「Intro to Angular」の例の下のコードで、「...」とは何かを教えてもらえますか?"foo(... arg)"(関数呼び出しの3つの点)の意味は何ですか?

getHeroes() { 
    this.backend.getAll(Hero).then((heroes: Hero[]) => { 
     this.logger.log(`Fetched ${heroes.length} heroes.`); 
     this.heroes.push(...heroes); // fill cache 
    }); 
+1

ます。https://developer.mozillaを

this.heroes.push.apply(this.heroes, Array.from(heroes)); 

より簡潔であることに加え、ここで...の別の利点は、それがより簡単に他の具体的な引数を指定して使用することができることである:あなたの例は同等です。 org/nl/docs/Web/JavaScript /リファレンス/演算子/ Spread_operator – Shilly

答えて

9

これはjQueryまたはAngularとは関係ありません。これはES2015で導入された機能です。

...doesn't actually have an official nameのこの特定の使用。他の用途に沿った名前は、「広がり引数」(一般用語は"spread syntax")です。それは反復可能を "爆発"(スプレッド)し、各値を引数として関数に渡します。

func(first, second, ...theRest); 

// as opposed to the following or something similar: 
func.apply(null, [first, second].concat(Array.from(heroes))); 
関連する問題