Creation of the `arguments` object

# Mathias Bynens (13 years ago)

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(); // "object"

However, in all engines I tested this in, it returns "object" instead.

On the other hand, this:

function fn() {
  var arguments = undefined;
  return typeof arguments;
};
fn(); // "undefined"

…does return "undefined". And so does this, in all engines except Opera:

function fn(arguments) {
  return typeof arguments;
};
fn(); // "object" in Opera, "undefined" everywhere else

Did I misunderstand the spec?

# Andreas Rossberg (13 years ago)

On 4 July 2012 16:03, Mathias Bynens <mathias at qiwi.be> wrote:

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.

The text is wrong. The order of steps in the normative part of the spec (Section 10.5, esp. step 6) makes clear that variable declarations are not taken into account (but parameters and function declarations are).

I suggest filing a bug, if there isn't one already: bugs.ecmascript.org/describecomponents.cgi?product=ECMA

# Brendan Eich (13 years ago)

This looks like a regression in the spec from ES3 to ES5.

# Mathias Bynens (13 years ago)

On Wed, Jul 4, 2012 at 4:27 PM, Andreas Rossberg <rossberg at google.com> wrote:

The text is wrong. The order of steps in the normative part of the spec (Section 10.5, esp. step 6) makes clear that variable declarations are not taken into account (but parameters and function declarations are).

Thanks for confirming. I filed it here: ecmascript#440.

# Mathias Bynens (13 years ago)

The text is wrong. The order of steps in the normative part of the spec (Section 10.5, esp. step 6) makes clear that variable declarations are not taken into account (but parameters and function declarations are).

If I understand correctly, the arguments object is created in step 7, but then overwritten by the variable declaration in step 8.

So shouldn’t the first two examples in my mail (quoted below) get the same result, then? Or are var arguments; and var arguments = undefined; not both VariableDeclarations?

On Wed, Jul 4, 2012 at 4:03 PM, Mathias Bynens <mathias at qiwi.be> wrote:

Following that logic, I would expect the following code to return "undefined":

function fn() {
  var arguments;
  return typeof arguments;
};
fn(); // "object"

However, in all engines I tested this in, it returns "object" instead.

On the other hand, this:

function fn() {
  var arguments = undefined;
  return typeof arguments;
};
fn(); // "undefined"

…does return "undefined".

Thanks for helping me understand!

# Yusuke Suzuki (13 years ago)

In the execution phase, their behavior is different.

section 12.2 ecma-international.org/ecma-262/5.1/#sec-12.2

var arguments = undefined

performs PutValue with reference and undefined.

  1. Let lhs be the result of evaluating Identifier as described in 11.1.2.
  1. Let rhs be the result of evaluating Initialiser.
  2. Let value be GetValue(rhs).
  3. Call PutValue(lhs, value).
  4. Return a String value containing the same sequence of characters as in the Identifier.

But

var arguments;

doesn't perform any assignment.

  1. Return a String value containing the same sequence of characters as in

the Identifier.

, Yusuke Suzuki

# Mathias Bynens (13 years ago)

Thanks, Yusuke!