“... args”是什么意思? (函数定义中的三个点)? [英] What is the meaning of "...args" (three dots) in a function definition?

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

问题描述

在Javascript中阅读此语法让我很困惑:

It was really confusing for me to read this syntax in Javascript:

router.route('/:id')
.put((...args) => controller.update(...args))
.get((...args) => controller.findById(...args));

... args 是什么意思?

推荐答案

尊重o (... args)=> ... args 休息参数 。它总是必须是参数列表中的最后一个条目,它将被分配一个数组,其中包含尚未分配给先前参数的所有参数。

With respect o (...args) =>, ...args is a rest parameter. It always has to be the last entry in the parameter list and it will be assigned an array that contains all arguments that haven't been assigned to previous parameters.

它基本上是替换 参数对象。而不是写

It's basically the replacement for the arguments object. Instead of writing

function max() {
  var values = Array.prototype.slice.call(arguments, 0);
  // ...
}
max(1,2,3);

你可以写

function max(...value) {
  // ...
}
max(1,2,3);

此外,由于箭头函数没有参数 object ,这是创建可变参数(箭头)函数的唯一方法。

Also, since arrow functions don't have an arguments object, this is the only way to create variadic (arrow) functions.

controller.update(... args),请参阅这是什么意思foo(... arg) (函数调用中的三个点)?

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

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