“foo(... arg)”的含义是什么? (函数调用中的三个点)? [英] What is the meaning of "foo(...arg)" (three dots in a function call)?

查看:100
本文介绍了“foo(... arg)”的含义是什么? (函数调用中的三个点)?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

有人可以在Angular简介示例的下面代码中说出...是什么?

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

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


推荐答案

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

This has nothing to do with jQuery or Angular. It's a feature that was introduced in ES2015.

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

This particular use of ... doesn't actually have an official name. A name that is in line with other uses would be "spread argument" (a generic term would be "spread syntax"). It "explodes" (spreads) an iterable and passes each value as argument to the function. Your example is equivalent to:

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

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

Besides being more concise, another advantage of ... here is that it can be more easily used with other concrete arguments:

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

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

这篇关于“foo(... arg)”的含义是什么? (函数调用中的三个点)?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆