Prevent JavaScript from automatically inserting semicolons

Source: Internet
Author: User

The JavaScript language has a mechanism: when parsing, you can automatically insert a semicolon after a sentence to modify the semicolon delimiter that is omitted at the end of the statement.

However, since this automatically inserted semicolon conflicts with another mechanism of the JavaScript language, that is, all whitespace is ignored, so the program can format the code with whitespace.

The conflict between these two mechanisms can easily obscure the more serious parsing errors. Sometimes it is inappropriate to insert semicolons.

For example, the automatic insertion of a semicolon in a return statement can result in the consequences:

If the return statement is to return a value, the starting part of the expression for that value must be on the same line as return, for example:

    var f = function () {    return    {    status:true    };    }

It looks like this is going to return an object that contains the status member element.

Unfortunately, JavaScript automatically inserts a semicolon so that it returns undefined, causing the object below to be actually returned to be ignored.

When the automatic insertion of a semicolon causes the program to be misunderstood, there are no warning reminders.

If you put {on the tail of the previous line instead of the head of the next line, you can avoid the problem, for example:

    var f = function () {    return {    status:true    };    }

In order to avoid omitting the errors caused by semicolons, it is advisable to develop a good habit, regardless of whether the statement is complete, as long as the complete statement must increase the semicolon to indicate the end of the sentence.

To facilitate reading, when a long sentence needs to be displayed in a branch, you should ensure that a line does not form a complete logical semantics.

For example, the following code is a continuous assignment statement that allows you to view their relationships more clearly through a branch display.

This branch shows that because there is no independent logical semantics within a line, JavaScript does not treat each line as a separate sentence, thus creating no ambiguity.

    var a =    b =    c =  4;

The above statement appears in one line as follows: var a = b = c = 4;

For the following statement, it is easy to create ambiguity if the line is not displayed correctly.

The meaning of the sentence: Define a variable i, and then assign a value, if the variable A is true, the assignment is 1, otherwise the variable B, if B is true, the assignment is 2, otherwise the variable C, if C is true, the assignment value is 3, otherwise the assignment is 4.

var i = a? 1:b? 2:c? 3:4;

The following branch display is wrong because the expression a? 1:b can form independent logical semantics, so JavaScript automatically adds a semicolon after it to represent a separate sentence.

    var i = a? 1:b    2:c    ? 3:4;

The safe approach should be shown in the following branches so that each line cannot form a separate semantics.

  var i = a? 1    : B? 2    : C? 3    : 4;

In short, when writing code, you should develop a good habit of ending sentences with semicolons, and all sentences should be separated by semicolons.

The sentences displayed by the branch should ensure that the single line is not easy to form independent legal logical semantics.

Prevent JavaScript from automatically inserting semicolons

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.