问题
The following code, when executed,
true && () => {}
yields
Uncaught SyntaxError: Malformed arrow function parameter list
Why ?
Edit: I know wrapping the function in parenthesis works, thanks everyone, but I'd like to understand why can't the parser figure out it's a function in the first place.
回答1:
The reason is due the first part true || (a)
being parsed by itself and THEN the parser is trying to parse the rest => {}
, which causes the error.
回答2:
It's parsing true && ()
as the parameter list.
回答3:
Because arrow functions has special parsing rules. See official documentation at Parsing order paragraph.
来源:https://stackoverflow.com/questions/55456281/why-does-true-produce-uncaught-syntaxerror-malformed-arrow-funct