Official Information on 'Arguments' in Es6 Arrow Functions

Official information on `arguments` in ES6 Arrow functions?

Chrome, FF, and node seem to be wrong here, Babel is correct:

Arrow functions do not have an own arguments binding in their scope; no arguments object is created when calling them.

looking for official docs here

Arrow function expressions evaluate to functions that have their [[ThisMode]] set to lexical, and when such are called the declaration instantiation does not create an arguments object. There is even a specifc note (18 a) stating that "Arrow functions never have an arguments objects.".

Do ES6 arrow functions have their own arguments or not?

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).

Why do arrow functions not have the arguments array?

Arrow functions don't have this since the arguments array-like object was a workaround to begin with, which ES6 has solved with a rest parameter:

var bar = (...arguments) => console.log(arguments);

arguments is by no means reserved here but just chosen. You can call it whatever you'd like and it can be combined with normal parameters:

var test = (one, two, ...rest) => [one, two, rest];

You can even go the other way, illustrated by this fancy apply:

var fapply = (fun, args) => fun(...args);

Access to callee in arrow functions

How do I access that value from an arrow function? Is it possible?

Short answer is No.

In fact an arrow function don't bind its own this, arguments, super ..., they were meant to provide a shorter syntax with less details, if we check the MDN Reference for Arrow functions we can see that:

An arrow function expression has a shorter syntax than a function expression and does not bind its own this, arguments, super, or new.target. These function expressions are best suited for non-method functions, and they cannot be used as constructors.

So you won't be able to get a reference to the function itself with Arrow functions, and as stated in comments arguments.callee "feature" was a pretty bad idea.

Why in Fat arrow functions arguments object is undefined

According to MDN

An arrow function expression has a shorter syntax than a function expression and does not have its own this, arguments, super, or new.target. These function expressions are best suited for non-method functions, and they cannot be used as constructors.

arrow functions: destructuring arguments and grabbing them as a whole at the same time

You can use default parameters and object rest at target of destructuring assignment for first parameter, where a single object is expected, and optionally define subsequent parameters by destructuring previously defined object parameter

const fn = ({...args} = {}, {a, b} = args) => console.log(a, b, args);

Why can't I return an arrow function?

Arrow functions doesn't have the arguments object, instead you can use the rest parameter syntax (...) like this:

var square = (a) => a * a;var callAndLog = (func) => {  return ((...args) => {    var res = func.apply(undefined, args);    console.log("Result is: " + res);    return res;  })};var squareAndLog = callAndLog(square);squareAndLog(5);

In ES6, can I use Arrow functions as short hand for .bind(this, ...) with arguments?

The problem I essentially was trying to solve was the classic "use setTimeout to call a function with parameters" problem, except with arrow functions. (But more generally, with any function, not just setTimeout.)

There are three nearly identical ways to solve this (the last one with arrow functions):

Using closures

var self = this;
functionThatReturnsAPromise()
.then((function() { return self.someFunction(obj.a + obj.b); }));

It works, but I would explicitly have to set this to some other variable, like self.

Using bind

functionThatReturnsAPromise()
.then((function(a, b) { return this.someFunction(a + b); }).bind(this, obj.a, obj.b));

A little better than the first, since I can just use this without any hackery, but it gets quite a bit verbose.

Using ES6 arrow functions

functionThatReturnsAPromise()
.then(() => this.someFunction(obj.a + obj.b));

A ha! Very concise. I no longer have to specify this, and I don't have to pass in any parameters either, since the arrow function preserves the parent closures stack frame. When the arrow function gets executed, it knows what obj.a and obj.b refer to. I have now achieved bind functionality with ES6 arrow function syntax.



Related Topics



Leave a reply



Submit