簡體   English   中英

訪問 javascript function arguments 的替代語法

[英]Alternative syntax to access javascript function arguments

(function(arguments = {})
{
    console.log(arguments)
}
)("a","b","c")

印刷

$ node args.js 
a
$ node --version
v8.9.4

在這種情況下,有沒有辦法訪問實際的 arguments?

我建議不要在function定義中覆蓋內置的arguments變量。

您可以使用...vargs

 (function(...vargs) { console.log(arguments); // Built-in arguments console.log(vargs); // Variable (spread) arguments })("a", "b", "c");
 .as-console-wrapper { top: 0; max-height: 100%;important; }

請查看 MDN 上arguments object了解更多信息。

該文檔指出,如果您使用 ES6 語法,則必須傳播 arguments,因為arguments不存在於箭頭(lambda 或匿名)function 內。

 ((...vargs) => { try { console.log(arguments); // Not accessible } catch(e) { console.log(e); // Expected to fail... } console.log(vargs); // Variable (spread) arguments })("a", "b", "c");
 .as-console-wrapper { top: 0; max-height: 100%;important; }

 function f1() { console.log(Array.from(arguments)) } f1(1, 2, 3) function f2(...args) { console.log(args) } f2(4, 5, 6)

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM