55

Since it seems like the first thing people do is convert arguments into a real array, I'm interested in why the Javascript language authors and implementers decided, and continue to think, that arguments should not be a real Array. I don't mean this as flamebait, I'm sincerely interested in the thinking behind it. Since the function is naturally being called when you're in its body, I don't think it's because the objects arguments are referencing can change, like with some of the DOM results...

pr1001
  • 21,727
  • 17
  • 79
  • 125
  • 1
    This is a non-issue in ES6. You can use [rest parameters](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Functions/rest_parameters), which is a real array. – Dan Dascalescu Jan 08 '18 at 09:33

4 Answers4

52

My conjecture:

The concept of the arguments object has been on the language since the very beginning, it's even described in the ECMAScript First Edition Standard(PDF).

In that version of ECMAScript, the Array.prototype was really basic, array objects contained only 4 methods!: toString, join, reverse and sort.

I think that's one of the major reasons about they make arguments to inherit from Object.prototype, at that time those Array methods didn't look too useful.

But the Array.prototype object was extended in the next versions of the standard, now on ES5, Array objects have methods such as map, reduce, every, some, etc, that are really powerful.

The last year, there was a proposal in ES5 to make arguments inherit from Array.prototype, in the draft stages of the standard, but was dropped off time later.

In those drafts, arguments inherited from Array.prototype, but for backwards compatibility with ES3, the arguments object had defined two own properties, toString and toLocaleString, both pointing to the same methods on Object.prototype, but finally, the committee decided to keep inheriting from Object.prototype.

Christian C. Salvadó
  • 807,428
  • 183
  • 922
  • 838
  • 18
    conjecture? looks like you were present in all those committee meetings.. lol – Anurag Jul 14 '10 at 01:18
  • 5
    And who was there doesn't know about it: http://www.slideshare.net/douglascrockford/newandimproved, nice wrap up btw +1. But it doesn't tell you **why** the committee " **continue** to think that `arguments` should not be a real `Array`" – gblazex Jul 14 '10 at 10:34
  • 2
    @galambalazs: IMO the reason about why the committee decided that, it's about the *fear of breaking the web*, the ES5 standard was designed very carefully, avoiding any radical change, in fact no new syntax was introduced to the language. The proposal I talk about, IIRC was dropped out because they discussed about *extreme edge* cases of incompatibility, such as re-defining the `Object.prototype`. We'll see, maybe in the future... – Christian C. Salvadó Jul 15 '10 at 00:55
  • 1
    @CMS: It's not conjecture that changing its prototype breaks sites, it's a known fact. Opera shipped with it as `Array.prototype` for just under two years. – gsnedders May 19 '13 at 15:40
34

The arguments object has the very unusual feature that its array-like elements are synonyms for the local variables that hold the function arguments. For example:

function f(x) {
   console.log(arguments[0]);   // Displays the initial value of the argument x
   x = 5;                       // Changes the value of the local variable x
   console.log(arguments[0]);   // Now displays 5
}

I always had the impression that this "magical behaviour" is the reason why arguments is not an array.

Daniel Vassallo
  • 337,827
  • 72
  • 505
  • 443
5

It's important to note that without one of the designers present, we can only really conjecture why. But we can come up with some decent reasons... here's mine:

From the perspective of a function, one reason could be because you can't - obviously - actually change the arguments that were passed into you. You could change an array that represents the arguments passed into you, but the arguments as they were passed is set in stone before you ever receive execution scope.

You can splice, dice and pop arrays, and if you did that to the arguments object then you just ruined what is conceptually an immutable structure (sad face!). The design of the real arguments object is closer to a kind of immutability JavaScript can offer.

It is similar to querystring parameters. You get a collection handed to you by the client sending the request. It's part of the request information, which is already set and done.

Rex M
  • 142,167
  • 33
  • 283
  • 313
  • 5
    I'm not sure if I fully agree with the reasoning here. `arguments` is just an object, and although we can't technically change the actual arguments, we can do whatever we want to the entire `arguments` object or individual arguments it represents through array indices - `arguments[0]`, `arguments[1]`, ... Why wasn't it made an `Array` then, or given an array-like interface is still worth contemplating I'd say. Same problem applies to NodeList's. – Anurag Jul 14 '10 at 00:20
  • @Anurag I don't necessarily disagree... as I said, we can only conjecture why, and this is my theory :) – Rex M Jul 14 '10 at 00:28
  • 1
    good points, I think the option to create tamper-free objects in ES5 is a good step forward and `arguments` can make very good use of it. One reason I can think of for really basic interfaces is that the ES5 committee is basically responsible for the entire web when they make breaking changes, so they are slow and hard to come by unfortunately. – Anurag Jul 14 '10 at 00:55
3

arguments doesn't just return the arguments. It returns callee object, and the array of arguments. If it were just an array, the first element might be the callee object and be more confusing.

Andir
  • 2,063
  • 1
  • 15
  • 22
  • 2
    Perhaps the question then should be, why isn't there a separate `callee` object? Why should be be a property of `arguments`? – pr1001 Jul 14 '10 at 00:25