简体   繁体   English

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

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

Arrow functions do not have an array of arguments; 箭头函数没有参数数组; how good is using ...arguments ? 使用...arguments有多好? It will not break something in the future? 将来不会破坏某些东西吗?

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

Arrow functions do not have a arguments of their own, so using a arguments as a parameter is not a problem but it might be confusing. 箭头函数没有自己的arguments ,因此将arguments用作参数不是问题,但可能会造成混淆。

But an arrow function in the scope of an outer function has access to the arguments object of the outer function. 但是,外部函数范围内的箭头函数可以访问外部函数的arguments对象。 So the arrow function can use the arguments of the outer function in its logic as shown below: 因此,箭头函数可以在其逻辑中使用外部函数的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' )); 

So if you have a parameter called arguments in your arrow function it would shadow the arguments from the outer function, if you have the arrow function in the outer function scope. 因此,如果您在arrow函数中有一个名为argumentsarguments ,那么如果您在外部函数作用域中具有arrow函数,它将使外部函数的arguments不可见。

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

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