简体   繁体   English

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

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

I have the following function 我有以下功能

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

However when the values passed in arguments are not displayed if I concatenate the text the arguments for each values are: . 但是,如果我连接文本时不显示传入arguments的值,则the arguments for each values are: Why is that? 这是为什么?

The output I have from Google's console is as follows when concatenating; 连接时,我从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 

The following values are passed when I don't concatenate. 当我不连接时,将传递以下值。

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 

EDIT 编辑

Not sure why the question got down voted but the problem I have is that when I use the statement console.log("the arguments for each values are: " + arguments); 不知道为什么这个问题被否决了,但是我遇到的问题是,当我使用语句console.log("the arguments for each values are: " + arguments); the output in the console is console.log("the arguments for each values are: " + arguments); 控制台中的输出为console.log("the arguments for each values are: " + arguments); however if I pass the statement console.log(arguments); 但是,如果我通过语句console.log(arguments); the output in the console is [] or [1, 2] ? 控制台中的输出是[]还是[1, 2]

Writing console.log("..." + arguments) it will force conversion of arguments into string. 编写console.log("..." + arguments)会强制将arguments转换为字符串。 Since arguments is an object , its string representation is [object Arguments] . 由于arguments是一个对象 ,因此其字符串表示形式为[object Arguments] If you instead want to display contents of that object, try passing it without concatenation: 如果您想显示该对象的内容,请尝试不进行串联传递它:

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM