为什么 Javascript 中的参数在使用 console.log 时返回 [对象参数]
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);
但是,当我将文本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);
相关文章:
- 函数参数在内部不可见,返回新的Promise(函数(resolve,reject).传递给被promise化的函数的
- 我如何才能继续'如果'语句来比较作为参数的多个函数返回值
- 仅使用特定参数返回特定值
- 当我将参数设置为对象时,JavaScript返回TypeError
- 为什么Coderbyte.com's的Javascript模板喜欢返回函数的原始参数
- 为什么我需要使用函数作为参数,并且可以'不要只使用它返回的值
- 如何从两个参数函数返回随机整数
- 提交输入类型=“;图像“;到另一个servlet时,未能返回任何参数
- Javascript-如何通过以字符串作为参数返回数组
- 如何将jQuery中某个函数的字符串作为参数返回给另一个jQuery函数
- JS函数返回函数供以后使用-未定义参数
- 如何从批处理文件调用一个带有2个参数的java脚本函数,并将结果返回到环境变量
- JavaScript 窗口['func'],参数返回 undefined 而不是函数
- 弹出窗口将参数返回给父级
- JavaScript unshift 参数返回意外结果
- Angular UI Router-子状态和父状态中的参数返回错误的模板
- 可以从Javascript函数参数返回一个值
- 强制传递一个匿名函数,该函数将对象作为javascript中的参数返回
- javascript块中的XSLT参数返回不正确的值
- 带参数的调用函数和带其他参数返回值的调用函数有什么不同