Home  >  Article  >  Web Front-end  >  A brief discussion on the use of semicolons in javascript_javascript skills

A brief discussion on the use of semicolons in javascript_javascript skills

WBOY
WBOYOriginal
2016-05-16 15:59:461242browse

Is it necessary to add a semicolon at the beginning of function in JS? Should I add a semicolon after the js statement? Should semicolons be used after JavaScript braces? What does the exclamation mark and semicolon at the beginning of function in JS mean?

After integrating multiple JS files into one file, to avoid syntax errors when compressing the code, you can handle it as follows

1. Add a semicolon before js

For example:;(function($){...code here...})();
In Javascript, a semicolon indicates the end of a statement. It is added at the beginning to separate it from other methods during compression and indicates the beginning of a new statement

2. Add a semicolon after the js function

For example

// 模块1
// 前面有若干代码
var Manager = {
 prop: '',
 method: function () {

 }
}
// 模块2,开头是个立即执行函数
(function () {
 // 代码
})()

After compression, it becomes: }}(function where it will be executed as a function, so the overall analysis will be wrong

Copy code The code is as follows:

var Manager = {prop: '',method: function (){}}(function () {})()

Solution: Add a semicolon after the Manager function

The above is the entire content of this article, I hope you all like it.

Statement:
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