什么是...语法? (在Angular2中找到) [英] what is ... syntax? (found in Angular2)
本文介绍了什么是...语法? (在Angular2中找到)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我不知道此语法或符号或任何内容是否属于Angular2
或其他...
I don't know if this syntax or symbol or whatever belongs to Angular2
or something else...
但我想知道,
...
的语法是什么,以及它在幕后如何工作?
what ...
syntax is and how it works behind the scene?
ngOnChanges(...args:any[])
{
console.log(args);
}
推荐答案
It's the TypeScript rest operator. In this case, it means any number of arguments of any type can occur there; the function will see them as an array of any
. (JavaScript recently got rest and spread operators as well, as of ES2015, but the :any[]
in your example tells us this is TypeScript.)
例如:
ngOnChanges('a', 42, null);
...将显示
["a", 42, null]
在控制台中.
Here's a complete example (live copy):
function foo(...args:any[]) {
console.log("args.length = " + args.length);
args.forEach((arg:any, index:Number) => {
console.log("args[" + index + "]: " + arg);
});
}
foo('a', 42, null);
输出
args.length = 3
args[0]: a
args[1]: 42
args[2]: null
这篇关于什么是...语法? (在Angular2中找到)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文