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?
Arrow functions treat this keyword differently. They don't define their own context since it doesn't have its own this context. They inherit that from the parent scope whenever you call this .
The arrow function, on the opposite, doesn't define arguments (but you can easily access the arrow function arguments using a rest parameter ... args ). If the arrow function has one expression, then the expression is returned implicitly, even without using the return keyword.
An arrow function expression is a syntactically compact alternative to a regular function expression, although without its own bindings to the this, arguments, super, or new. target keywords. Arrow function expressions are ill suited as methods, and they cannot be used as constructors.
An arrow function doesn't have its own this value. Instead, it uses the this value of the enclosing lexical scope. An arrow function also doesn't have the arguments object. Avoid using the arrow function for event handlers, object methods, prototype methods, and functions that use the arguments object.
From the spec:
Any reference to
arguments
,super
,this
, ornew.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).
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
, ornew.target
. Any reference toarguments
,super
,this
, ornew.target
within an ArrowFunction must resolve to a binding in a lexically enclosing environment. Typically this will be theFunction Environment
of an immediately enclosing function.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With