为什么 Javascript 中的参数在使用 console.log 时返回 [对象参数]

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

本文关键字:参数 返回 log 对象 console Javascript 为什么      更新时间:2023-09-26

>我有以下函数

//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);

但是,当我将文本the arguments for each values are:连接arguments时,如果不显示传入的值。为什么?

连接时,我从谷歌控制台获得的输出如下;

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转换为字符串。由于参数是一个对象,因此其字符串表示形式为 [object Arguments] 。如果要显示该对象的内容,请尝试在不连接的情况下传递它:

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