javascript是否需要';'在一行代码的末尾? [英] does javascript require ';' at the end of a line of code?

查看:70
本文介绍了javascript是否需要';'在一行代码的末尾?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经看过并使用了javascript代码,它们都有,并且在语句末尾省略了';'。它们在某些情况下是否需要而在其他情况下不需要如果是这样,你能给出一些例子,如果不是一般标准,那么使用';'或者不......那就是问题??

I have seen and used javascript code that both has and omits the ';' from the end of statements. Are they required in some cases and not in others? If so can you give some examples, and if not what is the general standard, to use the ';' or to not...that..is the question??

推荐答案

JavaScript使用一种名为Semicolon Insertion的(通常不喜欢)方法,它允许你省略分号。

JavaScript uses a (generally disliked) method called Semicolon Insertion, where it will allow you to omit semicolons.

规则你可以和不能插入分号的地方很广泛,而且这里很好。

The rules for where you can and cannot insert semicolons is extensive, and covered fairly well here.

一般情况下,不要依赖分号插入。这是一个经过深思熟虑的功能,会在它帮助你之前伤害你。

In general, do not rely on semicolon insertion. It is a poorly thought out feature and will hurt you before it helps you.

你不仅应该总是使用分号,而且你也应该养成放置你的分号的习惯。 {括号在同一行,因为分号插入实际上可能会误解你的意思。

Not only should you always use semicolons, but you should also get in the habit of putting your { braces on the same line, as semicolon insertion can actually misinterpret what you meant.

例如,说我我试图从函数返回一个javascript对象:

For example, say I'm trying to return a javascript object from a function:

function f() {
    return { "a":"b", "c":"d" }    // works fine
}

function g() {
    return                         // wrong!!!
    {
        "a":"b",
        "c":"d"
    }
}

如果我将 {放在新行上(就像我在中所做的那样) g ),return语句后面会插入一个分号,破坏你所说的含义。

If I put the { on a new line (as I did in g), the return statement will actually have a semicolon inserted after it, destroying the meaning of what you were saying.

整体,分号插入是一个糟糕的功能,你永远不应该依赖它。

Overall, semicolon insertion is a poor feature and you should never rely on it.

这篇关于javascript是否需要';'在一行代码的末尾?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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