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

查看:44
本文介绍了“...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 是什么意思?

推荐答案

对于 (...args) =>...args 是一个rest 参数.它始终必须是参数列表中的最后一个条目,并且将被分配一个数组,该数组包含尚未分配给先前参数的所有参数.

With respect to (...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);

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

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天全站免登陆