在箭头函数中使用 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' ));
所以如果你的箭头函数中有一个名为 arguments
的参数,它会从外部函数中隐藏 arguments
,如果你在外部函数范围内有箭头函数。
箭头函数没有参数数组;使用 ...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' ));
所以如果你的箭头函数中有一个名为 arguments
的参数,它会从外部函数中隐藏 arguments
,如果你在外部函数范围内有箭头函数。