31

I don’t know whether arrow functions bind arguments to a lexical scope or not.

Take a look at this example (the same concept can be used for this):

var b = function() { return () => console.log(arguments); };
b(1,2,3)(4,5,6); // different result of chrome vs FF.

When I run this on Chrome, I get [1,2,3], but on Firefox, I get [4,5,6]. What’s going on?

dakab
  • 5,379
  • 9
  • 43
  • 67
Amin Roosta
  • 1,080
  • 1
  • 11
  • 26

4 Answers4

23

From the spec:

Any reference to arguments, super, this, or new.target within an ArrowFunction must resolve to a binding in a lexically enclosing environment.

Therefore, the correct answer would be [1,2,3]. Firefox has fixed the problem in version 43 (bug 889158).

Oriol
  • 274,082
  • 63
  • 437
  • 513
11

No, arrow functions don't have their own arguments, this, super, or new.target.

See the note at 14.2.16 Runtime Semantics: Evaluation:

An ArrowFunction does not define local bindings for arguments, super, this, or new.target. Any reference to arguments, super, this, or new.target within an ArrowFunction must resolve to a binding in a lexically enclosing environment. Typically this will be the Function Environment of an immediately enclosing function.

JMM
  • 26,019
  • 3
  • 50
  • 55
2

Arrow functions don't have their own arguments object.

Arrow functions do not expose an arguments object to their code: arguments.length, arguments[0], arguments[1], and so forth do not refer to the arguments provided to the arrow function when called.

Arrow_functions

For this example

var b = function() {
  return () => console.log(arguments);
};

b(1,2,3)(4,5,6);

correct answer should be [1, 2, 3]

Oleksandr T.
  • 76,493
  • 17
  • 173
  • 144
-1

What's going on is in fact pretty simple. Chrome does not seem to add an arguments object to the scope of the inner (arrow) function, while Firefox does.

This means that the arguments logged in Chrome are the arguments passed to the parent function, which is a "normal" function.

Firefox believes (and in my opinion they're right to) that the arrow functions should also have the arguments object, and thus that is why they log the second set of numbers.

As others said, what Firefox does is against the specification.

Stephan Bijzitter
  • 4,425
  • 5
  • 24
  • 44