"foo(... arg)"(函数调用中的三个点)是什么意思?

vin*_*eel 8 javascript

有人能告诉"Angular简介"示例中的下面代码中的"..."是什么吗?

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

Fel*_*ing 10

这与jQuery或Angular无关.这是ES2015中引入的一项功能.

这种特殊用途... 实际上并没有官方名称.与其他用途一致的名称将是"spread argument"(通用术语将是"spread spread").它"爆炸"(传播)一个iterable并将每个值作为参数传递给函数.你的例子相当于:

this.heroes.push.apply(this.heroes, Array.from(heroes));
Run Code Online (Sandbox Code Playgroud)

除了更简洁之外,...这里的另一个优点是它可以更容易地与其他具体参数一起使用:

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

// as opposed to the following or something similar:
 func.apply(null, [first, second].concat(Array.from(heroes)));
Run Code Online (Sandbox Code Playgroud)