为什么Javascript中的参数在使用console.log时返回[object Arguments]?

时间:2012-06-19 21:43:22

标签: javascript

我有以下功能

//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]

1 个答案:

答案 0 :(得分:5)

console.log("..." + arguments)它会强制将arguments转换为字符串。自arguments is an object起,其字符串表示为[object Arguments]。如果您想要显示该对象的内容,请尝试传递它而不连接:

console.log("the arguments for each values are: ", arguments);
相关问题