coderboy
coderboy

Reputation: 1859

Why can't I access arguments.callee in this sloppy-mode function?

I am trying to access the arguments.callee property in the simple function below, but I couldn't since the function throws an error.

function foo(a = 50, b) {
  console.log(arguments.callee);
}

foo(10, 50);

Why is this happening? It seems that the function is running in strict mode, but how is that possible since I didn't mention the statement 'use strict';

I am running this on Chrome.

Upvotes: 3

Views: 730

Answers (1)

Bergi
Bergi

Reputation: 664548

An arguments object is automatically created like in strict mode if you are using advanced (modern, ES6+) syntax in your function declaration - in your case it's the default parameter initialiser. The function is not actually strict mode - e.g. you can still use a with statement in there, but the arguments object is no longer backwards-compatible with pre-ES5 code.

See the note on FunctionDeclarationInstantiation:

A mapped argument object is only provided for non-strict functions that don't have a rest parameter, any parameter default value initializers, or any destructured parameters.

Apart from no longer having a .callee, it doesn't reflect assignments to the parameter variables either (which was always rather surprising, see JavaScript: why does changing an argument variable change the `arguments` "array"? or Changing JavaScript function's parameter value using arguments array not working).

Basically arguments.callee was deprecated with ES5, but for backward compatibility the language can only opt out of providing it when you opt in to modern syntax, like using strict mode or non-simple parameter declarations.

Upvotes: 5

Related Questions