Object Object Arguments in Node.js is Different from JavaScript JavaScript

I got confused with the argument object of the Node.js function.

Suppose I have the following code:

function x() { return arguments; } console.log(x(1, 2, 3)); 

In developer chrome tools, it is returned as an array:

 [1, 2, 3] 

But I got a different result in Node.js:

 { '0': 1, '1': 2, '2': 3 } 

How did it happen?

+7
source share
3 answers

arguments is a magic variable that is not really an array. It behaves like an array, but it does not have all the functions that the array has.

Other objects, for example, NodeList , for example.

+5
source

You see another representation of an object that is not an array in Chrome, either in Node or javascript in general.

If you want the array to come out of it, you do this:

 var args = Array.prototype.slice.call(arguments, 0); 
+6
source

console.log is not part of javascript, not part of v8. That is why both chrome and node.js have their own implementations of console.log. They have a simular apis, but not the same. The documentation for node console.log is here: http://nodejs.org/api/stdio.html

+2
source

All Articles