[20141011] 68 effective ways to write high-quality JS code (II)
No.6, understanding the limitations of semicolon insertion
Tips:
- The semicolon is deduced only before the "}" tag, at the end of a line, and at the end of the program
- The semicolon is deduced only when the immediately preceding token cannot be parsed.
- Never omit a semicolon before a statement that begins with (, [, +,-, or/characters)
- When the script is connected, insert the semicolon explicitly between the scripts
- Must not be wrapped before return, throw, break, continue, + +, or--arguments
- Semicolons cannot be pushed out as delimiters for the header and empty statements of a For loop
- Personal Summary: Try not to omit the semicolon, do not let JS automatically deduced
The semicolon is inserted only before the} tag, after one or more line breaks, and at the end of the program input, looking at the code:
// 能自动推导分号function square(x){ var n = +x return n*n}// Error,不能自动推导function square(x){var n = +x return n*n}
Semicolons are inserted only when subsequent input tags cannot be parsed, see the code:
a=b(f());此时,代码等价于 ab(f());但是:a=bf()则会被解析为a=b f();
You must never omit a semicolon before a statement that begins with (, [, +,-, or/characters], see the code:
a=b[‘a‘,‘b‘,‘c‘].forEach(function(key){ console.log(key)})等价于a=b[‘a‘,‘b‘,‘c‘].forEach(function(key){ console.log(key);});a=1/Error/i.test(‘test‘)等价于a=1/Error/i.test(‘test‘);
You cannot omit semicolons when merging scripts to see the code:
//file1.js(function(){console.log(‘file1‘)})()//file2.js(function(){console.log(‘file2‘)})()//合并后 --输出file1,然后报错(function(){console.log(‘file1‘)})()(function(){console.log(‘file2‘)})()
To prevent your own library from interfering with other code within the merge, the following code is commonly written:
;(function(){ /*Code*/ })();
You can never wrap a line before return, throw, break, continue, + +, or--See the code:
a++b等价于:a;++b;
Do not omit semicolons in the For loop
//Parse Errorvar total=0for(var i=0,total=1 i<n i++){ total*=i}
In conclusion, once again, do not add a semicolon looks code light, but a little attention will cause a lot of bugs, so, suggestions are added semicolon, do not let the JS environment self-deduction
No.7, a sequence of code elements with a 16-bit view string
待定...
No.8, minimize the use of global objects
Tips:
- Avoid declaring global variables
- Try to assert local variables
- Avoid adding properties to global objects
- Using global objects for Platform feature detection
Defining a global variable pollutes the shared public namespace and can lead to unexpected naming conflicts. Global variables are not conducive to modularity because they cause unnecessary coupling between independent components in the program.
No.9, always declare local variables
Tips:
- Always declare a new local variable with VAR
- Consider using the Lint tool to help check for unbound variables
If there is anything more troublesome than a global variable, it is an unexpected global variable. Since variables declared by VAR are not applicable, all are global variables, so be sure to use Var to define variables to prevent variable contamination.
function test(){ test=‘test‘;}test();window.test;// ‘test‘
No.10, avoid using with
Tips:
- Avoid using the WITH statement
- Use short variable names instead of repeatedly accessed objects
- Explicitly bind local variables to object properties instead of using the WITH statement to bind them implicitly
待定...
68 effective ways to write high-quality JS code (II)