Why does “true && () => {}” produce “Uncaught SyntaxError: Malformed arrow function parameter list”? [duplicate]

萝らか妹 提交于 2020-08-22 11:47:21

问题


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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!