1

When I access arguments from inside called function in nodejs...

echo '!function(){ console.log(arguments) }(1,2,3)' | node

... it outputs an object ...

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

... but in google chrome ...

!function(){ console.log(arguments) }(1,2,3)

... outputs ...

[1, 2, 3]

Why is it not consistent? What other js environments have this different behaviour? Is it ok for me to use arguments? How can I ensure it is always an array before I use it?

1
  • 1
    Arrays are objects. You're just seeing variation in the way that the console objects work. Commented Nov 29, 2014 at 22:27

2 Answers 2

6

There are no rules for console output. The output doesn't represent anything that has to do with the language standard.

If you create a program that logs values from within a JS program, you're free to display those values however you want.

The arguments object is the same in both places. Only how it's being displayed is different.

How can I ensure it is always an array before I use it?

It's never an array. It's always an array-like object. In both cases its showing a value at object members 0, 1 and 2. Just uses different display syntax to do it.

In Chrome, try creating an object like this:

var my_obj = {
    "0": 1,
    "2": 3,
    "1": 2,
    length: 3,
    splice: function(){},
};

And the log it in the console. It'll probably look like an Array literal, though it's obviously not.

Sign up to request clarification or add additional context in comments.

1 Comment

that is interesting. Additionally, I solved my problem with: var args_array = Array.prototype.slice.call(arguments)
0

It's not an Array in the browser, either. The arguments object has actually been in JavaScript longer than Arrays have, and when Arrays were first added to JavaScript (back in its 1.1 days), they didn't update arguments to be an Array. They haven't done it since then either, because of worries about backward-compatibility. It shows up in some browser consoles as though it were an Array because the debug console knows that most people plan to treat it as one anyway.

But the arguments object is close enough to an Array that it's easy to convert. One way to do it is like this:

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

Array.prototype.slice, like most of Array.prototype's methods, is written to be generic: you can call it on Objects that aren't Arrays, and as long as they have properties with numeric names and a length property that's a Number, it'll work. The arguments object has both of these things, so these functions will work on it.

The slice function returns an Array which is a shallow copy of whatever object was passed into it, starting and ending at whatever numbers you specify. We tell it to start at zero, and because we didn't say where to stop, it assumes we want it to stop at the end, wherever that might be. This way, we copy all of the elements of arguments into a new Array, and now you can do whatever other Array things you want with it.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.