为什么“真实"()=>{}";产生“未捕获的SyntaxError:箭头函数参数列表格式错误"? [英] Why does "true && () => {}" produce "Uncaught SyntaxError: Malformed arrow function parameter list"?

查看:318
本文介绍了为什么“真实"()=>{}";产生“未捕获的SyntaxError:箭头函数参数列表格式错误"?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

以下代码在执行时

true && () => {}

收益

Uncaught SyntaxError: Malformed arrow function parameter list

为什么?

谢谢大家,我知道将函数包装在括号中是有效的,但是我想理解为什么解析器首先不能弄清楚它是一个函数.

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.

推荐答案

原因是由于第一部分 true ||(a)自己解析,然后解析器尝试解析其余的 =>{} ,这会导致错误.

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.

这篇关于为什么“真实"()=>{}";产生“未捕获的SyntaxError:箭头函数参数列表格式错误"?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆