SyntaxError: missing } after function body - JavaScript 编辑
The JavaScript exception "missing } after function body" occurs when there is a syntax mistake when creating a function somewhere. Check if any closing curly brackets or parenthesis are in the correct order.
Message
SyntaxError: Expected '}' (Edge) SyntaxError: missing } after function body (Firefox)
Error type
What went wrong?
There is a syntax mistake when creating a function somewhere. Also check if any closing curly brackets or parenthesis are in the correct order. Indenting or formatting the code a bit nicer might also help you to see through the jungle.
Examples
Forgotten closing curly bracket
Oftentimes, there is a missing curly bracket in your function code:
var charge = function() {
if (sunny) {
useSolarCells();
} else {
promptBikeRide();
};
Correct would be:
var charge = function() {
if (sunny) {
useSolarCells();
} else {
promptBikeRide();
}
};
It can be more obscure when using IIFE, Closures, or other constructs that use a lot of different parenthesis and curly brackets, for example.
(function() { if (true) { return false; } );
Oftentimes, indenting differently or double checking indentation helps to spot these errors.
(function() {
if (true) {
return false;
}
});
See also
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论