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);
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.
Wsy this arrow function doesn't work with 'arguments.length'?
You have to parse the arguments to the arrow function like this
const countArg2 = (...arguments) => arguments.length;
console.log(countArg2(1, 2, 3));
// VM6745:1 Uncaught ReferenceError: arguments is not defined
// at mArgs (<anonymous>:1:29)
// at <anonymous>:2:1
Do ES6 arrow functions have their own arguments or not?
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).
Different between normal function and arrow function: arguments not defined return an error
Use the Spread_syntax :
const plus = (...arguments) => { let sum = 0; for (let i = arguments.length - 1; i >= 0; i--) { sum += arguments[i]; // console.log(i); } // console.log(arguments); return sum;}
console.log(plus(2, 4, 6, 8));
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.".
How to use context parameter of the filter() array with an arrow function
This happens because lambda functions (arrow) get the context of where they're defined, in this case Window
, which doesn't have lower
or upper
props. Just use range.upper
and range.lower
instead of this
and should work just fine.
Why different value of arguments inside and outside foo function
arguments
has a special meaning in some circumstances: it refers to the arguments passed to the closest ancestor function
. For this reason, it's a good idea to never define variables named arguments
to avoid confusion. Try it:
const arr = [1, 2, 3];
const someFn = () => arr[2];
someFn()
console.log(someFn());
console.log(arr[0]);
function foo(n) {
console.log(arr[0]);
const f = () => arr[0] + n;
return f();
}
foo(3);
console.log(arr[0]);
Related Topics
How to Send a Message to a Particular Client with Socket.Io
Orderby Multiple Fields in Angular
How Dangerous Is It in JavaScript, Really, to Assume Undefined Is Not Overwritten
Nested Element (Web Component) Can't Get Its Template
Passing Variables to the Next Middleware Using Next() in Express.Js
How to Remove Element from Array in Foreach Loop
Unsigned Integer in JavaScript
Reactjs: Prevent Multiple Times Button Press
Angularjs Toggle Class Using Ng-Class
Why Are Objects Not Iterable in JavaScript
Why Isn't This Object Being Passed by Reference When Assigning Something Else to It
What Is Property in Hasownproperty in JavaScript
Advanced JavaScript: Why Is This Function Wrapped in Parentheses