Reprint-Read online documentation-notes-JS Programming Best Practices (practice)

Source: Internet
Author: User

Original address:

1). JavaScript Best practices:http://www.w3schools.com/js/js_function_closures.asp

2). JavaScript Closures http://www.w3schools.com/js/js_function_closures.asp

3). JavaScript Strict Mode http://blog.csdn.net/supersky07/article/details/14129179

1. About Global variables

    • Minimize the use of global variables (to represent data types, objects, and functions), and global variables or functions may be overridden by other JS scripts that are referenced.
    • In the Web page, the global variable belongs to the Winow object
    • When you define a variable without using the keyword VAR, the variable is defined as global, even if it is created inside a function

2. Closures

var add = (function () {    var counter = 0;    return function () {return counter + = 1;}}) (); Add (), add (); Add ();//The counter is now 3

The initial value of the variable add is the return value of a self-invoking function, and this self-invoking function executes only once. Using the code above, we succeeded in turning add into a function, and it could also access the variable of the parent scipe of that function-counter.

This example is called a JavaScript closure, making it possible to implement "private" variables in JS. The counter in the example above is protected by the scope of the anonymous function and can only be accessed through the Add function.

3. Strict mode

    • Strict mode, variables declared without (using VAR) are not allowed
    • If an internal method definition is inside an external method that declares "use strict," It also enables strict mode.
    • In strict mode, the value of this is not automatically changed to an object. Most notably, when call or apply's first argument is null or undefined, the value of this in the referenced method will not be converted to a global object. (Translator note: Non-strict mode will be converted to window, strict mode is null)

4. Place the variable declaration at the top of the function or script and initialize the variable when declared.

5. Do not use the new Object ()

    • Use {} instead of new Object ()
    • Use "" Instead of new String ()
    • Use 0 instead of new number ()
    • Use false instead of new Boolean ()
    • Use [] instead of new Array ()
    • Use /()/ instead of new RegExp ()
    • Use function () {} instead of new function ()

6. Note Type Auto-conversion

var x = "Hello";     // typeof X is a stringx = 5;               // changes typeof X to a number

JS can turn numbers into strings when doing mathematical operations

varx = 5 + 7;//x.valueof () is the TypeOf X is a number var x = 5 + "7"; // x.valueof () is the typeof x is a string varx = "5" + 7;//x.valueof () is the typeof x is a stringvarx = 5-7;//x.valueof () is-2, typeof X is a numbervarx = 5-"7";//x.valueof () is-2, typeof X is a numbervarx = "5"-7;//x.valueof () is-2, typeof X is a numbervarx = 5-"x";//x.valueof () is NaN, typeof X is a number

Two string subtraction, no error but return NaN (not a number)

"Hello"-"Dolly"    //  returns NaN

7. Three equals sign

= = Two equal signs do the type conversion when the operands are compared;

= = = Three equals equal to the value of the operand type is forced to compare

0 = = "";        // true1 = = "1";       // true true;      // true0 = = = "";       // false1 = = = "1";      // false true;     // false

8. When a function call does not pass a parameter, the parameter value is set to undefined

9. Avoid using eval ()-eval () to execute text as code

Reprint-Read online documentation-notes-JS Programming Best Practices (practice)

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.