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...
views:
172answers:
4It'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.
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.
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.
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
.