繁体   English   中英

好的做法是在箭头函数中使用name = arguments作为函数参数?

[英]It is good practice to use name=arguments as function arguments in arrow functions?

箭头函数没有参数数组; 使用...arguments有多好? 将来不会破坏某些东西吗?

const getStr = (...arguments) => [].slice.call(arguments, 1).join(arguments[0])
getStr( '*', '1', 'b', '1c' ) // '1*b*1c'

箭头函数没有自己的arguments ,因此将arguments用作参数不是问题,但可能会造成混淆。

但是,外部函数范围内的箭头函数可以访问外部函数的arguments对象。 因此,箭头函数可以在其逻辑中使用外部函数的arguments ,如下所示:

 const getStr = (...anotherArguments) => { console.log("arguments here is ", typeof arguments); return [].slice.call(anotherArguments, 1).join(anotherArguments[0]); } console.log(getStr( '*', '1', 'b', '1c' )); function outer(){ //arguments captured from the outer function scope return (() => { console.log("arguments here is" , typeof arguments); return [].slice.call(arguments, 1).join(arguments[0]); })() } console.log(outer( '*', '1', 'b', '1c' )); 

因此,如果您在arrow函数中有一个名为argumentsarguments ,那么如果您在外部函数作用域中具有arrow函数,它将使外部函数的arguments不可见。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM