JavaScript Inserts semicolons automatically

Source: Internet
Author: User
Tags foreach error handling expression insert log parse error string window

Automatic semicolon Insertion
Although JavaScript has the code style of C, it does not force you to use semicolons in your code, and you can actually omit them.
JavaScript is not a language without semicolons, on the contrary it requires a semicolon to parse the source code.
Therefore, the JavaScript parser automatically inserts a semicolon in the source code when it encounters a parse error caused by a missing semicolon.
var foo = function () {
}//Parse error, semicolon missing
Test ()
The semicolon is inserted automatically, and the parser is resolved again.
var foo = function () {
}; No errors, parsing continues
Test ()
Automatic semicolon insertion is considered one of the biggest design flaws in the JavaScript language because it changes the behavior of the code.
Working principle
The following code does not have a semicolon, so the parser needs to decide where to insert the semicolon.

(Function (window, undefined) {
function test (options) {
Log (' testing! ')
(Options.list []). ForEach (function (i) {
})
Options.value.test (
' Long string to pass ',
' and another long string to pass '
)
Return
{
Foo:function () {}
}
}
Window.test = Test
}) (window)
(function (window) {
Window.somelibrary = {}
}) (window)
The following is the result of the parser "guessing".
(Function (window, undefined) {
function test (options) {

No semicolon inserted, two rows merged into one line
Log (' testing! ') (Options.list []). ForEach (function (i) {

}); <-Insert Semicolon

Options.value.test (
' Long string to pass ',
' and another long string to pass '
); <-Insert Semicolon

Return <-inserts a semicolon, changing the behavior of the return expression
{//is handled as a code snippet
Foo:function () {}
}; <-Insert Semicolon
}
Window.test = test; <-Insert Semicolon
Two lines are merged again.
}) (window) (function (window) {
Window.somelibrary = {}; <-Insert Semicolon
}) (window); <-Insert Semicolon
Note: JavaScript does not correctly handle the return expression immediately following the line break.
Although this is not an automatic semicolon insertion error, this is indeed an undesirable side effect.
The parser dramatically changes the behavior of the code above and, in other cases, makes the wrong deal.
Front Bracket
In the case of a front bracket, the parser does not automatically insert a semicolon.
Log (' testing! ')
(Options.list []). ForEach (function (i) {})
The above code is converted to a row by the parser.
Log (' testing! ') (Options.list []). ForEach (function (i) {})
The result of the log function is extremely likely not to be a function; In this case, a typeerror error can occur, and the verbose error message may be the undefined is not a functions.
Conclusion
It is recommended that you never omit semicolons, and that you also promote the use of curly braces and corresponding expressions on one line,
You should not omit curly braces for an if or else expression that has only one line of code.
These good programming habits not only refer to the consistency of the code, but also prevent the parser from altering the error handling of the code's behavior.



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.