簡體   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