"Arguments" doesn’t work on an Arrow Function?

Asked

Viewed 49 times

2

I’m doing a simple test with Arrow Function, but when I use the arguments he returns

Uncaught Referenceerror: Arguments is not defined

How can I use the parameters?

const teste = () => {
  for(i in arguments){
    return arguments[i];
  }
}

teste('teste', 123);

1 answer

2


I think this is what you want, although it doesn’t make much sense because it will print the first one and it won’t do anything else.

As the error stated, it was missing to declare the arguments and say that he would be a array of arguments and not just one of them, so I used the ... which is a construction for this very.

const teste = (...args) => { for(i in args) return args[i]; }

console.log(teste('teste', 123));

I put in the Github for future reference.

Maybe he wanted to use the yield, but then you can’t use the syntax of Arrow Function?

I changed the variable name according to the bfavaretto comment. He is right that when it comes to the ecosystem of the JS it is possible that some conflict occurs if not today, some day, since arguments is "magical".

And according to the comment of the AP can not do without stating because he wants a semantics different from what exists "magically" with the use of arguments. Leave the created convention, you need the configuration that is always more verbose.

  • The thing was not to pass the (...Arguments) even, as it was a normal function. But I saw that the Arrow functions did not create this.

  • Thank you, Maniero!

  • I think it would be more prudent to use ...args instead of arguments. It’s not a reserved word, but I imagine it can be a problem in some cases. Maybe if you transpired into ES5, if the transpilator doesn’t take certain care.

  • @bfavaretto indeed, changed.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.