0

I have confused about dynamic parameter function from another file, compare with normal function. for example :

1. Normal Function

 function foo() {
    let arr=[]

    for (var i = 0; i < arguments.length; i++) {
      arr.push(arguments[i]);
    }
    console.log(arr.length)
 }

foo('aa','bb') ==> output 2 -> Correct

2. Function from another file

 *otherFunc.js

 module.exports = {
     running: () => {

         let arr = []

         for (var i = 0; i < arguments.length; i++) {
            arr.push(arguments[i]);
         }
        console.log(arr.length)
    }
 }


 *tes.js

 var db = require('./otherfunc');
 db.running('aa','bb') ==> output 5 -> Not Correct

Why output two function is difference? how to solve this? thank you

sembilanlangit
  • 245
  • 1
  • 3
  • 19
  • 1
    `arguments` in an arrow function can't be accessed – Nick Parsons Jan 20 '20 at 04:07
  • Thank @NickParsons the problem solve... but why with arrow can't be accessed? function and arrow are similar right? – sembilanlangit Jan 20 '20 at 04:13
  • not sure for the exact reason why, but my best guess is that arrow functions and the spread syntax were both introduced in ES6. Using `(...args) =>` will allow you to access the arguments in an arrow function, so there is no need for `arguments` really – Nick Parsons Jan 20 '20 at 04:15

0 Answers0