http://ecma-international.org/ecma-262/5.1/#sec-10.6 says:
> When control enters an execution context for function code, an arguments
> object is created unless (as specified in 10.5) the identifier arguments
> occurs as an Identifier in the function’s FormalParameterList or occurs as
> the Identifier of a VariableDeclaration or FunctionDeclaration contained in
> the function code.
Following that logic, I would expect the following code to return `"undefined"`:
function fn() {
var arguments;
return typeof arguments;
};
fn();
However, the order of steps in the normative part of the spec (http://ecma-international.org/ecma-262/5.1/#sec-10.5, especially step 6) makes clear that variable declarations are not taken into account (although parameters and function declarations are). So, the example above should in fact return `"object"`.
As Brendan Eich noted, this looks like a regression in the spec from ES3 to ES5.
As noted, the actual normative algorithm is correct.
The error is in the non-normative introduction to 10.6. For the next edition, we are generally either eliminating such text or explicitly marking it as non-normative.
To further explain my confusion: `var arguments = undefined;` is equivalent to `var arguments; arguments = undefined;`. But then there’s http://ecma-international.org/ecma-262/5.1/#sec-12.2, which says:
> Variables are initialised to `undefined` when created.
Consider the following example. I’ve added comments explaining what gets alerted in all the engines I tested in.
function fn() {
alert(typeof arguments); // alerts "object"
var arguments; // not initialised to `undefined`?
alert(typeof arguments); // alerts "object"
arguments = undefined;
alert(typeof arguments); // alerts "undefined"
};
fn();
Why doesn’t `var arguments;` initialize to `undefined`, as it says in the spec? Is that another error in non-normative text?
(In reply to comment #2)
> Why doesn’t `var arguments;` initialize to `undefined`, as it says in the spec?
> Is that another error in non-normative text?
I see now — step 8c in http://ecma-international.org/ecma-262/5.1/#sec-10.5. `varAlreadyDeclared` is `true` because the `arguments` object has already been created at that point. So, only when `[var] arguments = undefined;` is executed, it gets overwritten.
So I guess it’s only comment #0 that’s an error.
Bulk resolving ES5.1 errata issues as a sampling suggests these are all fixed. If this is in error, please open a new issue on GitHub.