我对Node.js函数参数对象感到困惑.
假设我有以下代码:
function x() {
return arguments;
}
console.log(x(1, 2, 3));
Run Code Online (Sandbox Code Playgroud)
在chrome开发人员工具中,它作为数组返回:
[1, 2, 3]
Run Code Online (Sandbox Code Playgroud)
但是我在node.js中得到了不同的结果:
{ '0': 1, '1': 2, '2': 3 }
Run Code Online (Sandbox Code Playgroud)
怎么会?
你会看到一个对象的不同表示形式,它在Chrome中既不是在Node中也不是在javascript中.
如果你想要一个数组,你可以这样做:
var args = Array.prototype.slice.call(arguments, 0);
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
7338 次 |
| 最近记录: |