为什么箭头函数没有参数数组? [英] Why do arrow functions not have the arguments array?

查看:34
本文介绍了为什么箭头函数没有参数数组?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

function foo(x) {
   console.log(arguments)
} //foo(1) prints [1]

但是

var bar = x => console.log(arguments) 

以相同方式调用时会出现以下错误:

gives the following error when invoked in the same way:

Uncaught ReferenceError: arguments is not defined

推荐答案

箭头函数没有这个,因为 arguments 类数组对象是一种解决方法,ES6 已经解决了这个问题rest 参数:

Arrow functions don't have this since the arguments array-like object was a workaround to begin with, which ES6 has solved with a rest parameter:

var bar = (...arguments) => console.log(arguments);

arguments 在这里绝不是保留的,而是被选择的.您可以随心所欲地调用它,也可以将其与普通参数结合使用:

arguments is by no means reserved here but just chosen. You can call it whatever you'd like and it can be combined with normal parameters:

var test = (one, two, ...rest) => [one, two, rest];

你甚至可以走另一条路,如这个奇特的应用所示:

You can even go the other way, illustrated by this fancy apply:

var fapply = (fun, args) => fun(...args);

这篇关于为什么箭头函数没有参数数组?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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