1 and strict mode

can use

"use strict" in the scope of a scope (including function scope and global scope),

to open the mode.

2, indent

, use Tab key to indent code to save code size, and use the width of 4 spaces to indent (JSLint recommendation).

3,

1) symbol curly

and statements on the same line, the back; only one line statement, also use braces:

 if (true) {//true} else {//false}
 while (true) {//alert (1); pre> 

2

} operator after the
space in braces began to match
and else, between the end of match while or catch
in various parts of the for
in

 var brace: a = [1, 2, 3]; VAR obj = {name:'name', value:'value'}; for (VaR I = 0; I 10; < i++) {} function func (a, B, c) {} C = a + B (a & if; & B || C) {//if} else {//else} try {//try} catch {//catch (ERR)}

3) all The statement after use;

4, named

, the end of the

object: the use of hump type, such as: MyClass
method, variable: the use of hybrid, such as: (getName), myName
constant: uppercase underlined, such as: MY_NAME

5, VAR

single mode using only a var variable declaration, in the top of the function role as follows:

1) provides a single address to find all local variables
function requires 2) to prevent the emergence of variables in the definition before the logical error
is used 3) to help keep in mind to declare a variable, and less use of global variables
4 as much as possible) less

 function func encoding 

(VaR) {a = 1, B = 2, sum = a + B, obj = {name:'name'}, value:'value', $btn = $('#btn') The body of the function

6,}

; / /

1) for cycle

cycle

 var I, arr = []; for (I = arr.length; i--; //arr[i]) {}; 

 for (Note: VAR I = 0; I < document.getElementsByName (.Length); i++) {//document.getElementsByName ([0]);}

this way every time the I length of document on will query using comparison, and usually the DOM operation is very time-consuming.

2

 var ARR) while = [], I = arr.length; while (i--)}

3) {/ / for-in

 var hasOwn cycle i = Object.prototype.hasOwnProperty; for (I in man) {if (hasOwn.call (man, I) console.log (I) {/ / filter, ':'. Man[i]}

7, switch);}

 switch (Num) case 0: //do something {break case 1:; //do something break; default: //do default}

...

 var suggested use: Obj = {'0': function (//do) {somethins},'1': function (do) {/ / somethis}. If...} (obj.hasOwnProperty (Num)) {(obj[num])} else {//do default;}

8, parseInt (

1)

) numerical agreement each specific to

 var month band parameters: 

='09', day ='08'; month = parseInt (month, 10); / / not will be converted to octal day = parseInt (day, 10) - parameter;

2) other commonly used to convert a string of numerical methods:

 Number ('08'+'08'; 

;

9), literal mode

does not recommend the use of constructor definition:

 / / built in constructors (avoid) var o Object (VaR) = new; a = new; re = var (Array) new RegExp ('[a-z]','g'); VAR s = new; VAR = n (String) new (Number); VAR B = new Boolean (throw); new Error ('message'); 

recommends the use of literal pattern better:

 / / literals and primitives (prefer) var o = {}; var a = []; VAR re = /[a-z]/g; VAR = s var n =' '; 0 VaR; b = FALS E; throw message:'message'{name:'Error',}

10,

1) in other variables - the word if at the beginning is all lowercase: xmlDocument, if not at the beginning, all uppercase: loadXML
2) variables must be meaningful English, ban

This paper fixed link:http://www.script-home.com/javascript-coding-conventions-encoding-specifications.html | Script Home | +Copy Link

Article reprint please specify:Javascript coding conventions (encoding specifications) | Script Home

You may also be interested in these articles!