"arguments" does not work in an arrow function?

2

I'm doing a simple test with arrow function , but when I use arguments it 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);
    
asked by anonymous 01.07.2018 / 23:08

1 answer

2

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

As the error reported, I needed to declare arguments and say that it would be an array of arguments and not just one of them, so I used ... which is a construct for this .

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

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

Maybe you wanted to use yield , but there you will not can you use the syntax of arrow function ?

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

And according to the AP's comment, you can not do it without declaring why it wants a semantics different from what exists "magically" with the use of arguments . Exit the created convention, you need the configuration that is always more verbose.

    
01.07.2018 / 23:41