Deep understanding of javascript (1) Writing high-quality code _ javascript skills

Source: Internet
Author: User
Write some elements of high-quality JavaScript, such as avoiding global variables, using a single variable declaration, pre-caching length (length) in a loop, following code reading, and more 1. Variables

• Global Variables
The two features of JavaScript make it unexpected to unconsciously create global variables. First, you can use variables without even declaring them. Second, JavaScript has an implicit global concept, this means that any variable you do not declare will become a global object attribute (not a global variable in the true sense, you can delete it using delete)

The Code is as follows:


Function sum (x, y ){
// The result is not declared. It is an implicit global variable.
Result = x + y;
Return result;
}

Function foo (){
// Use the task chain for partial var declaration. B is an implicit global variable.
Var a = B = 1;
}


Suggestion:

The Code is as follows:


Function (x, y ){
Var a, B;
A = B = 1; // a, B is the local variable
}


• Var Function
Global variables created through var (created in programs other than functions) cannot be deleted. Implicit global variables created without var (ignore whether or not they are created in the function) can be deleted.

The Code is as follows:


// Define three global variables
Var global_var = 1;
Global_novar = 2; // textbook
(Function (){
Global_fromfunc = 3; // negative textbook
}());

// Try to delete
Delete global_var; // false
Delete global_novar; // true
Delete global_fromfunc; // true

// Test the deletion.
Typeof global_var; // "number"
Typeof global_novar; // "undefined"
Typeof global_fromfunc; // "undefined"


• Declare variables in the form of single var
Using a single var statement at the top of a function is a useful form. The initial values of all uninitialized but declared variables are undefined.

The Code is as follows:


Function func (){
Var a = 1,
B = 2,
Sum = a + B,
Myobject = {},
I,
J;
// Function body...
}


• Var Distribution

The Code is as follows:


// Counterexample
Myname = "global"; // global variable
Function func (){
Alert (myname); // "undefined" var myname = "local"; alert (myname); // "local"} func (); equivalent:


Myname = "global"; // global variable
Function func (){
Var myname; // equivalent to-> var myname = undefined;
Alert (myname); // "undefined"
Myname = "local ";
Alert (myname); // "local "}
Func ();


2. for Loop

• Recommended

The Code is as follows:


Function logoff (){
Var I = 0,
Max,
Myarray = [];
//...
For (I = 0, max = myarray. length; I <max; I ++ ){
// Use myarray [I] to do something
}
}


Use the following expression to replace I ++

The Code is as follows:


I = I + 1
I + = 1 the following two cycles are faster


// The first form of change:

Var I, myarray = [];
For (I = myarray. length; I --;){
// Use myarray [I] to do something
}

// The second method uses the while loop:

Var myarray = [],
I = myarray. length;
While (I --){
// Use myarray [I] to do something
}


• For-in Loop
Apply to non-array object traversal. The array uses a normal for loop, and the object uses a for-in loop. The hasOwnProperty () method filters out attributes from the prototype chain when traversing object attributes.

Iii. Avoid implicit type conversion

• Always use = and! =

4. Avoid using eval, and avoid passing strings to setInterval (), setTimeout (), and Function () constructor, instead of using functions.

Five parseInt () numeric Conversion

We recommend that you assign a value to the base parameter,

The Code is as follows:


Var month = "06 ",
Year = "09 ";
Month = parseInt (month, 10); // a string starting with 0 is treated as an octal string.
Year = parseInt (year, 10 );


Six programming specifications

Name of the constructor: MyConstructor ();

General function name: myFunction ();

Variable name: firstName;

Private property or method: _ secondeName,

Constant: PI, MAX;
Related Article

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.