3

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.

Bergi
  • 630,263
  • 148
  • 957
  • 1,375
coderboy
  • 1,710
  • 1
  • 6
  • 16
  • 2
    Are you in a module? ie, do you use `import` or `export`? If so, then you are automatically in strict mode. – Nicholas Tower Jun 26 '21 at 14:33
  • It could be that modern browsers are automatically using strict mode? – evolutionxbox Jun 26 '21 at 14:34
  • @NicholasTower no. Nothing like that. – coderboy Jun 26 '21 at 14:34
  • @evolutionxbox I think so as well, but weirdly I couldn't find any resource mentioning that. – coderboy Jun 26 '21 at 14:35
  • 1
    Are you using Typescript? Running in Chrome console? There are a few ways you can automatically be in strict mode without specifying it. Please add details about your runtime environment if the answer you're looking for is _why_ it's in strict mode. – jmargolisvt Jun 26 '21 at 14:39
  • [Getting the same error in Chrome when running in sloppy mode](https://jsfiddle.net/ou68q7j5/). – Ivar Jun 26 '21 at 14:45

1 Answers1

5

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.

Bergi
  • 630,263
  • 148
  • 957
  • 1,375