This question already has answers here:
Official information on `arguments` in ES6 Arrow functions?
(2 answers)
Closed 6 years ago.
function foo(x) {
console.log(arguments)
} //foo(1) prints [1]
but
var bar = x => console.log(arguments)
gives the following error when invoked in the same way:
Uncaught ReferenceError: arguments is not defined
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);
Related
This question already has answers here:
Functions that return a function
(10 answers)
How do JavaScript closures work?
(86 answers)
Closed 1 year ago.
What is the meaning of returning a function from another function in javascript. Is this is same as if I call a function which is in another function
function fun(c){
let a=3;
return function(){
return a+c;
}
}
let add=fun(2);
console.log(add);
. And my second question is why this add variable is a function.
Currently, the object which you are returning is the function.
In this context, variable add contains a reference to the function which you have to invoke.
This is called currying. It allows you to apply arguments to functions one by one.
Example:
const add = a => b => a + b // Function that takes `a` and returns a function that adds `a` to `b`
const addThree = add(3) // bind `a` to 3. This is a function that takes `b` and adds 3 to it
console.log(addThree(1)) // now `b` is 1, it's gonna be added to `a` and return the final value
There's a lot of literature about currying and partial application. Google is your friend
This question already has answers here:
SyntaxError Unexpected token, expected ","
(1 answer)
The use of brackets in javascript arrow function declaration
(1 answer)
Syntax for destructuring arrays into function parameters in ES6
(1 answer)
Closed 1 year ago.
Is there a way in which I can destructure an array in the parameters of its prototype functions?
For example, I can use an Array.prototype function such as forEach to evaluate the value of each array element and log an individual sub-array value using bracket notation:
const array = [[0,1],[3,4],[5,6]];
array.forEach(element => console.log(element[0]));
// -> 0
// -> 3
// -> 5
If I want to refer to a sub-array element by an assigned name, can I do so with destructuring? Currently, I know I can do it like this:
let array = [[0,1],[3,4],[5,6]];
array.forEach(element => {
let first = element[0];
let second = element[1];
console.log(first);
// -> 0
// -> 3
// -> 5
});
What I would like to achieve using destructuring is assigning these variable names the way you normally would with destructuring in a typical function's parameters:
let array = [[0,1],[3,4],[5,6]];
array.forEach([first, second] => console.log(first));
// Uncaught SyntaxError: Malformed arrow function parameter list
Is this possible?
After a little bit of trial and error, it appears this is fully possible. You just have to wrap the restructuring parameter in parentheses the same way you would if there were multiple parameters:
let array = [[0,1],[3,4],[5,6]];
array.forEach(([first, second]) => console.log(first));
// -> 0
// -> 3
// -> 5
This question already has answers here:
ES6 immediately invoked arrow function
(4 answers)
Closed 4 years ago.
Sometimes I want to assign a variable the result of an Arrow function, but if I assign the Arrow Function to that variable it get the function and not the return of that Function as is obvious.
let theNumber2 = ()=>{return 2};
theNumber2 will contain the function and only if I do theNumber2() I will get returned 2.
Is there a way to do something like this to get directly the return of the arrow function? For example:
let theNumber2 = ()=>{return 2}(); // Note the () at the final
Does there exist something to do that? I mean, to assign the arrow function called?
Just wrap in parenthesis and call the function.
let theNumber2 = (() => { return 2; })();
console.log(theNumber2);
This question already has answers here:
Are 'Arrow Functions' and 'Functions' equivalent / interchangeable?
(4 answers)
Closed 4 years ago.
I wanted to understand the behavior of a normal function vs arrow functions.
Arrow Function:
function arrowFunc() {
return () => arguments
}
console.log(arrowFunc(1, 2, 3)(1))
Normal Function
function normalFunc() {
return function() {
return arguments
}
}
console.log(normalFunc(1, 2, 3)(1))
Both the results are expected to be same, but looks like arrowFunc defined above considers the first arg list, where as the normalFunc considers the second set of arg list.
Also tried babel-compilation to understand the difference, but looks like the behavior is different as shown below:
Babel Output:
"use strict";
function arrowFunc() {
var _arguments = arguments;
return function() {
return _arguments;
};
}
console.log(arrowFunc(1, 2, 3)(1));
function normalFunc() {
return function() {
return arguments;
};
}
console.log(normalFunc(1, 2, 3)(1));
Both the results are expected to be same
No, they're not.
From the first line of the MDN page on arrow functions (emphasis mine):
An arrow function expression has a shorter syntax than a function
expression and does not have its own this, arguments, super, or
new.target.
And further down the same page:
Arrow functions do not have their own arguments object. Thus, in
this example, arguments is simply a reference to the arguments of the
enclosing scope [...]
And in the ECMAScript specification:
NOTE: Arrow functions never have an arguments objects. (sic)
This question already has answers here:
JavaScript ES6: Test for arrow function, built-in function, regular function?
(10 answers)
Closed 6 years ago.
Because of the big difference in the context difference between normal and ES6 arrow functions I'd like to be able to find out which one was received on a callback fn.
typeof will return function for both. Is there any way to distinguish?
Arrow functions can't be used as constructors and show typeof arrowFunc.prototype as undefined, whereas a non-arrow function shows `"object".
You can use Function.toString() to return a string representation of the source code of the function, then look for an arrow (=>) in the string.
var arrowFunc = x => 2 * x
var regFunc = function (x) {return 2 * x}
arrowFunc.toString().indexOf("=>") // 2
regFunc.toString().indexOf("=>") // -1