簡體   English   中英

如何使用 arguments.length 找到傳遞給 function 的 arguments 的數量?

[英]How can you use arguments.length to find the number of arguments passed to a function?

我正在寫一個 function 需要確定有多少 arguments 已傳遞給自己。 在聲明 function 后,我能夠獲得此信息,但是每當我使用arguments.length從 function 中調用它時,我都會得到同樣不准確的 answer.length

在聲明 function 之后使用{function}.length的示例:
 const noParameters = () => {}; const oneParameter = (a) => {}; const twoParameters = (a, b) => {}; console.log(noParameters.length); // 0 console.log(oneParameter.length); // 1 console.log(twoParameters.length); // 2
在 function 中使用argument.length的示例
const whatIsArgument = () => {
  return arguments
}

console.log(whatIsArgument());

// Console returns: 
[Arguments] {
  '0': {},
  '1': 
   { [Function: require]
     resolve: { [Function: resolve] paths: [Function: paths] },
     main: 
      Module {
        id: '.',
        exports: {},
        parent: null,
        filename: '{redacted filepath}/test.js',
        loaded: false,
        children: [],
        paths: [Array] },
     extensions: { '.js': [Function], '.json': [Function], '.node': [Function] },
     cache: 
      { '{redacted filepath}/test.js': [Module] } },
  '2': 
   Module {
     id: '.',
     exports: {},
     parent: null,
     filename: '/{redacted filepath}/test.js',
     loaded: false,
     children: [],
     paths: 
      [ {array of all the files in my project directory } ] },
  '3': '{redacted filepath}/test.js',
  '4': '{redacted filepath}' }

我試圖了解arguments object 本身,但它似乎與它返回的 function 無關。 從打印arguments到控制台,它似乎與我正在運行的文件有關:

 const whatIsArgument = () => { return arguments } console.log(whatIsArgument()); // Console returns: [Arguments] { '0': {}, '1': { [Function: require] resolve: { [Function: resolve] paths: [Function: paths] }, main: Module { id: '.', exports: {}, parent: null, filename: '{redacted filepath}/test.js', loaded: false, children: [], paths: [Array] }, extensions: { '.js': [Function], '.json': [Function], '.node': [Function] }, cache: { '{redacted filepath}/test.js': [Module] } }, '2': Module { id: '.', exports: {}, parent: null, filename: '/{redacted filepath}/test.js', loaded: false, children: [], paths: [ {array of all the files in my project directory } ] }, '3': '{redacted filepath}/test.js', '4': '{redacted filepath}' }

注意:為了隱私,文件路徑已被編輯。

有誰知道這里發生了什么? 網上的一切都表明 arguments.length 應該返回我正在尋找的東西,所以我很茫然。

arguments object 是處理變量 arguments 的老式方法。 arguments上的MDN 頁面

如果您正在編寫 ES6 兼容代碼,則應首選rest 參數

由於 ES6 箭頭函數的怪癖,您正在觀察這種奇怪的行為,詳見MDN 頁面(強調我的):

箭頭 function 表達式是常規 function 表達式的語法緊湊替代方案,盡管沒有與thisargumentssupernew.target關鍵字的綁定。

由於您使用的是箭頭函數,因此您應該使用rest 參數處理變量 arguments 。

 const noArguments = (...args) => { return args.length } const oneArgument = (...args) => { return args.length } const twoArguments = (...args) => { return args.length } console.log(noArguments()); // 0 console.log(oneArgument(1)); // 1 console.log(twoArguments(1, 2)); // 2

這不是您通過 arguments 的方式。 arguments 的工作原理:

 const myFunc = (...args) => { return args.length }; console.log(myFunc(1,2,3,4)) console.log(myFunc())

雖然這不能直接回答您的問題, arguments並沒有真正用於現代 Javascript,除了在將 es6 代碼編譯和調整為 es5 時。

你應該知道這個關鍵字的行為和它的內容從一個實現到另一個實現。

使用 rest 參數會更好,如下所示:

const myFunc = (...args) => args.forEach(console.log)

另請注意,不推薦使用myFunc.arguments訪問它,並且不能保證其支持。

暫無
暫無

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

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