当我将e
参数放在括号中,然后使用ES6箭头函数时,我在控制台上得到错误“uncapted syntaxerror:undermented token=>
”。 但是,当我从括号中移除参数时,不会出现错误。 参数不应该有括号吗?
document.querySelector("#book-form").addEventListener("submit", (e)
=> {
// …
});
箭头函数的参数和=>
:
14.2箭头函数定义
ArrowFunction[In,Yield,Await]:
要么删除换行,要么将其放到其他地方。 您也可以使用命名函数,例如:
const submitHandler = (e) => {
// ...
};
document.querySelector("#book-form").addEventListener("submit", submitHandler);
首先,您的函数调用和函数声明没有关闭。 其次,箭不能独在其线上。
//Event: add book
document.querySelector("#book-form").addEventListener("submit", (e) => {
//prevent default
e.preventDefault();
// get form value
const title = document.querySelector("#title").value;
const author = document.querySelector("#author").value;
const isbn = document.querySelector("#isbn").value;
//Close function body, then function call.
});