Home > Web Front-end > JS Tutorial > Detailed introduction to the semicolon insertion mechanism in JavaScript_Basic knowledge

Detailed introduction to the semicolon insertion mechanism in JavaScript_Basic knowledge

WBOY
Release: 2016-05-16 16:14:35
Original
1214 people have browsed it

is inserted

only before }, after one or more newlines and at the end of program input

This means that you can only omit the semicolon at the end of a line, a code block, and a program.

That is to say, you can write the following code

Copy code The code is as follows:

function square(x) {
var n = x
Return n * n
}

But you cannot write the code like the following, otherwise an error will be reported
Copy code The code is as follows:

function area(r) { r = r return Math.PI*r*r }//error

Insert

only if subsequent input tags cannot be parsed

In other words, semicolon insertion is an error correction mechanism. Look at the code and talk

Copy code The code is as follows:

a = b
(f())
//Can be correctly parsed into a single statement. The unit price is the following statement
a = b(f())

a = b
f()
//Parsed into two independent statements
a = bf();//Error in analysis

So you have to pay attention to the beginning of the next statement to determine whether you can legally omit the semicolon.

(, [, , -, and / These five characters start the statement, then it is best not to omit the semicolon in front.

Give me an example

Copy code The code is as follows:

a = b
['r', 'g', 'b'].forEach(function (key) {
console.log(key);
});

Originally you thought there was no error, but the parser parsed it into the following statement
Copy code The code is as follows:

a = b['r', 'g', 'b'].forEach(function (key) {
console.log(key);
});

Because the second statement starts with [, the parser will not automatically insert a semicolon after the first statement, so it is parsed as shown above. The above formula is parsed as b['b'].forEach Isn't it wrong?

So for sentences starting with these five characters (, [, , -, and / , it is best not to omit the semicolon in front.

If you want to omit the semicolon, experienced programmers will follow this statement with a declaration statement to ensure that the parser parses it correctly. As shown below

Copy code The code is as follows:

a = b
var x//A declaration statement is specially added here to ensure that a = b will not be parsed together with (f())
(f())

So if you need to omit the semicolon, you must check whether the start mark of the next line is the above five characters, which will cause the parser to disable automatic insertion of semicolons, or you can also use the five characters (, [, , -, and / Precede with a semicolon

Omitting semicolons causes script connection problems

Copy code The code is as follows:

//file1.js
(function () {
//......
})()

//file2.js
(function () {
//......
})()

When the above two files are connected, they will be parsed as follows

Copy code The code is as follows:

(function () {
//......
})()(function () {
//......
})()

So omitting the semicolon requires not only watching out for the next token in the current file, but also for any token that may appear after the statement after the script is connected.

To avoid parser parsing errors, you can prefix each file with an extra semicolon to protect the script from careless concatenation. If the first statement in the file opens with the five vulnerable characters mentioned above, you should add an additional semicolon prefix.

JavaScript syntax restricts production

JavaScript syntax restricts production: no line breaks are allowed between two characters.

Example:

Copy code The code is as follows:

return
{};

The above code is parsed into
Copy code The code is as follows:

return;
{}
;

Semicolon insertion rules for auto-increment and auto-decrement operations

Copy code The code is as follows:

a

b

Think about it, what will the above code be parsed into? Tell me the answer, because the increment operator can be used as both a prefix operator and a postfix operator, but the postfix operator cannot appear before a newline, so the above code is parsed into
Copy code The code is as follows:

a;
b;

The semicolon will not be automatically inserted as a separator at the head of an empty for loop statement

Copy code The code is as follows:

for (var i = 0,total=1
i < length
i ) {
total*=i;
}

Code like the above will cause parsing errors.

The while of the empty loop body also needs to display a semicolon, otherwise it will also cause parsing errors

Copy code The code is as follows:

function mytest() {
While (true)
}

It must be written as follows to avoid errors
Copy code The code is as follows:

function mytest() {
While (true) ;
}

To summarize

1. Semicolons are deduced only before the } mark, at the end of a line and at the end of a program
2. Semicolon
is deduced only if the following tag cannot be parsed 3. The semicolon
must not be omitted before statements starting with (, [, , -, and / characters 4. When linking scripts, explicitly insert semicolons
between scripts 5. Never break a line before the parameters of return, throw, break, continue, or --
6. A semicolon cannot be deduced as the head of a for loop or the delimiter of an empty statement

Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template