[英]Why does var a = …arguments fail, if console.log(…arguments) is OK?
[英]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.