简体   繁体   中英

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 ? 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.

But an arrow function in the scope of an outer function has access to the arguments object of the outer function. So the arrow function can use the arguments of the outer function in its logic as shown below:

 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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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