1

我有以下功能

//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:。这是为什么?

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

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);,控制台中的输出是[]or [1, 2]

4

1 回答 1

5

编写console.log("..." + arguments)它将强制转换arguments为字符串。由于arguments 是一个对象,它的字符串表示是[object Arguments]. 如果您想显示该对象的内容,请尝试在不连接的情况下传递它:

console.log("the arguments for each values are: ", arguments);
于 2012-06-19T21:48:28.220 回答