“ foo(... arg)”(函数调用中的三个点)的含义是什么?

有人可以在“ 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中引入的功能。

的这种特殊用法... 实际上没有正式名称与其他用途一致的名称将是“ spread arguments”(通用术语将是“ spread语法”)。它“爆炸”(传播)一个可迭代对象,并将每个值作为参数传递给该函数。您的示例等效于:

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)));

本文收集自互联网,转载请注明来源。

如有侵权,请联系 [email protected] 删除。

编辑于
0

我来说两句

0 条评论
登录 后参与评论

相关文章

函数定义中“ ... args”(三个点)的含义是什么?

Java中的“ new Foo(){}”与“ new Foo()”

角@NGRX中三个点的含义是什么

'goto * foo'其中foo不是指针。这是什么?

什么是`int foo :: * bar :: *`?

为什么以这种方式使用函数参数'foo':*(&foo)?

为什么Foo({})调用Foo(0)而不是Foo()?

函数签名中typedef void FOO与#define FOO void的含义

JS-导入'@ foo / bar'中@的含义

foo,bar,baz等的含义

Clojure中的:foo,:: foo,:: bar / foo和:bar / foo有什么区别?

JavaScript中的forEach回调函数中的第三个参数的含义是什么

什么是允许`p.foo = o.foo`返回对函数`foo`的引用的JavaScript机制/规则?

是foo吗?Foo与kotlin中的foo完全等同于foo吗?

为什么代码“ foo :: foo :: foo :: foob”正在编译?

var [foo] = bar的含义?

Rust中的Foo(bar)= zar是什么?

方法调用语法`foo.method()`和UFCS`Foo :: method(&foo)`有什么区别?

为什么Lib :: Foo不隐藏:: Foo?

foo是什么意思?

当foo被声明为可变的时,要求使用&mut foo而不是&foo的原因是什么?

防止foo =“ *”中的glob扩展;回声$ foo

为什么foo = bar与foo = bar不同?

Bash 中的 `declare foo` 和 `foo=` 有什么区别?

语法的含义是什么:<element #foo />

从 Foo::MyClass 调用模块 Foo 中的 Ruby 方法

理解 Python 中的 [foo for bar, foo in arr]

什么是 window["foo"]=function() {alert("foo");}

在 C++ 类成员函数中调用 foo() 和 ::foo() 有什么区别?