簡體   English   中英

使用console.log時,為什么Javascript中的參數返回[object Arguments]?

[英]Why does arguments in Javascript return [object Arguments] when using console.log?

我有以下功能

//simple function with parameters and variable
        function thirdfunction(a,b,c,d){
            console.log("the value of a is: " + a);
            console.log("the value of b is: " + b);
            console.log("the value of c is: " + c);
            console.log("the value of d is: " + d);
            console.log("the arguments for each values are: " + arguments);
            console.log("the number of arguments passed are: " + arguments.length);
        }

        console.log("no parameter values are passed");
        thirdfunction();

        console.log("only a and b parameter values are passed");
        thirdfunction(1,2);

但是,如果我連接文本時不顯示傳入arguments的值,則the arguments for each values are: 這是為什么?

連接時,我從Google控制台獲得的輸出如下:

no parameter values are passed
the value of a is: undefined
the value of b is: undefined
the value of c is: undefined
the value of d is: undefined
the arguments for each values are: [object Arguments]
the number of arguments passed are: 0
only a and b parameter values are passed
the value of a is: 1
the value of b is: 2
the value of c is: undefined
the value of d is: undefined
the arguments for each values are: [object Arguments]
the number of arguments passed are: 2 

當我不連接時,將傳遞以下值。

no parameter values are passed
the value of a is: undefined
the value of b is: undefined
the value of c is: undefined
the value of d is: undefined
[]
the number of arguments passed are: 0
only a and b parameter values are passed
the value of a is: 1
the value of b is: 2
the value of c is: undefined
the value of d is: undefined
[1, 2]
the number of arguments passed are: 2 

編輯

不知道為什么這個問題被否決了,但是我遇到的問題是,當我使用語句console.log("the arguments for each values are: " + arguments); 控制台中的輸出為console.log("the arguments for each values are: " + arguments); 但是,如果我通過語句console.log(arguments); 控制台中的輸出是[]還是[1, 2]

編寫console.log("..." + arguments)會強制將arguments轉換為字符串。 由於arguments是一個對象 ,因此其字符串表示形式為[object Arguments] 如果您想顯示該對象的內容,請嘗試不進行串聯傳遞它:

console.log("the arguments for each values are: ", arguments);

暫無
暫無

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

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