'I'm using regular function give me correct output when I convert it to arrow function give me error [duplicate]
I'm using regular function give me correct output when I convert it to arrow function give me error /////////////////////////working prefect////////////////////
function sum() {
total = 0
for (i in arguments) {
total += arguments[i]
}
console.log(total);
}
sum(20, 80, 90, 70);
output = 260
/////////////not working prefect using arrow function/////////////
var sum = () => {
total = 0
for (i in arguments) {
total += arguments[i]
}
console.log(total);
}
sum(20, 80, 90, 70);
Error
0[object Object]function require(path) {
return mod.require(path);
}[object Object]c:
Solution 1:[1]
Arrow functions do not have an arguments binding. However, they have access to the arguments object of the closest non-arrow parent function. Named and rest parameters are heavily relied upon to capture the arguments passed to arrow functions. In most cases, using rest parameters is a good alternative to using arguments.
var sum = (...arguments) => {
total = 0;
for (i in arguments) {
total += arguments[i]
}
console.log(total);
}
sum(20, 80, 90, 70); // returns 260
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 |
