箭头函数内的换行符引发“未捕获的SyntaxError:意外的令牌'=>`" [英] Line break within arrow function throws “Uncaught SyntaxError: Unexpected token `=>`”

查看:203
本文介绍了箭头函数内的换行符引发“未捕获的SyntaxError:意外的令牌'=>`"的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

当我将e参数放在括号中并使用ES6箭头功能时,在控制台上出现错误未捕获的SyntaxError:意外的令牌=>" .但是,当我从括号中删除参数时没有错误.该参数应该没有括号吗?

I’m getting the error "Uncaught SyntaxError: Unexpected token =>" on my console when I put the e parameter in parentheses and then use an ES6 arrow function. However, there’s no error when I remove the parameter from the parentheses. Should the parameter not have parentheses?

document.querySelector("#book-form").addEventListener("submit", (e) 
=> {
  // …
});

推荐答案

箭头函数在参数和=>之间不能有换行符:

Arrow functions cannot have a newline between the parameters and the =>:

14.2箭头功能定义

ArrowFunction [输入,收益,等待]:

ArrowFunction[In, Yield, Await]:

  • ArrowParameters [?Yield,?Await] [此处没有LineTerminator] => ConciseBody
  • ArrowParameters [?Yield, ?Await] [no LineTerminator here] => ConciseBody

删除换行符,或将其放在其他位置.您也可以改用命名函数,例如:

Either remove the newline, or put it somewhere else. You could also use a named function instead, eg:

const submitHandler = (e) => {
  // ...
};
document.querySelector("#book-form").addEventListener("submit", submitHandler);

这篇关于箭头函数内的换行符引发“未捕获的SyntaxError:意外的令牌'=>`"的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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