68 effective ways to write high-quality JS code (II)

Source: Internet
Author: User

[20141011] 68 effective ways to write high-quality JS code (II)

No.6, understanding the limitations of semicolon insertion

Tips:

    1. The semicolon is deduced only before the "}" tag, at the end of a line, and at the end of the program
    2. The semicolon is deduced only when the immediately preceding token cannot be parsed.
    3. Never omit a semicolon before a statement that begins with (, [, +,-, or/characters)
    4. When the script is connected, insert the semicolon explicitly between the scripts
    5. Must not be wrapped before return, throw, break, continue, + +, or--arguments
    6. Semicolons cannot be pushed out as delimiters for the header and empty statements of a For loop
    7. 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:

    1. Avoid declaring global variables
    2. Try to assert local variables
    3. Avoid adding properties to global objects
    4. 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:

    1. Always declare a new local variable with VAR
    2. 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:

    1. Avoid using the WITH statement
    2. Use short variable names instead of repeatedly accessed objects
    3. 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)

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.