函数声明后我们需要一个分号吗? [英] Do we need a semicolon after function declaration?

查看:686
本文介绍了函数声明后我们需要一个分号吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述


可能重复:

JavaScript:花括号后我应该何时使用分号?

有人在函数声明后添加了分号,但有人没有。这是在函数声明后添加分号的好习惯吗?

Someone added semicolon after function declaration, but someone not. Is this a good practice to add semicolon after function declaration?

function test(o) {
}

function test(o) {
};


推荐答案

函数声明不需要(也不应该有)分号后面的分号:

A function declaration does not need (and should not have) a semicolon following it:

function test(o) {
}

但是,如果将函数编写为语句,就像下面的变量初始化程序一样,然后语句应该用分号终止,就像任何其他语句一样:

However, if you write a function as a statement, like the variable initializer below, then the statement should be terminated with a semicolon, just like any other statement would be:

var a = function test(o) {
};

这篇关于函数声明后我们需要一个分号吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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