gpt4 book ai didi

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

转载 作者:数据小太阳 更新时间:2023-10-29 04:05:51 26 4
gpt4 key购买 nike

谁能说出“Angular 简介”示例中以下代码中的“...”是什么?

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

最佳答案

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

... 的特殊用途 doesn't actually have an official name .符合其他用途的名称将是“传播参数”(通用术语将是 "spread syntax" )。它“分解”(展开)一个 iterable 并将每个值作为参数传递给函数。您的示例等效于:

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

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

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

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

关于javascript - "foo(...arg)"(函数调用中的三个点)是什么意思?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42118201/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com