Front-end Coding specification--javascript

Source: Internet
Author: User
Tags deprecated hasownproperty

1.tab key (must) use four spaces instead of

2. After each line of code (must) add ";"

3. The names of variables, constants, and classes are executed according to (must) the following rules:

1) Variable: must be named with the Camel Peak and the first letter lowercase

// 正确的命名  var isHotel,      isHotelBeijing,      isHotelBeijingHandian;  // 不推荐的命名  var is_Hotel,      ishotelbeijing,      IsHotelBeiJing;

2) constants: All uppercase names must be used, and words are separated by _, constants are usually used for Ajax request URLs, and some data that does not change

// 正确的命名  var HOTEL_GET_URL = ‘http://map.baidu.com/detail‘,      PLACE_TYPE = ‘hotel‘;

3) Class: Must use the name of Camel Peak and capitalize the first letter, such as:

// 正确的写法  var FooAndToo = function(name) { this.name = name; 

4. Use of spaces
1) If the space in the first example

//正确的写法  if (isOk) {      console.log("ok");  }  //不推荐的写法 if(isOk){ console.log("ok");

() The judging conditions before and after all (must) plus a space
() before and after the Judgment (forbidden) plus a space, such as: the correct wording: if (isOk); deprecated wording: if (isOk)

2) The space in switch, first the example

The correct wordingSwitch (name) {Case"Hotel": Console.log (name);BreakCase"Moive": Console.log (name); Break ; default: //code} //deprecated switch (name) { //switch should not have a space after the correct wording: switch (name) {//Code case "Hotel": Console. log (name); Break ; //break; should be aligned with Console.log case "movie": a newline console is required before each case.  log (name); Break ; //break; should be aligned with Console.log default: //code          

3) for the space in the first example

//correct wording var names = [ "Hotel",  "movie"], I, len; for (I=0, len=names.length; i < len; i++) {//Code} //deprecated notation var names = [ "Hotel",  "movie"], I, Span class= "hljs-built_in" >len; for (i = 0, len = Names.length;i < len;i++) {//for should have a space after each '; ' number, there should be a space, the assignment of the variable should not have a space //code 

For after (must) plus space
Each, after (must) add a space
() is forbidden to declare variables with VAR; and assignment of variables = before and after (forbidden) plus space

4) space in function, first example

// 正确的写法  function call(name) { } var cell = function () { }; // 不推荐的写法 var call = function(name){ // cod

Arguments after the parentheses (must) plus a space
After function (must) plus space

5) var hollow lattice and definition, first example

// 一个推荐的var写法组  function(res) {      var code = 1 + 1, json =www.meiwanyule.cn  JSON.parse(res), type, html;

Declare variable = before and after (must) add a space
The assignment declaration for each variable is declared with the next variable assignment after the end (must) wrapping
(recommended) Place the last row of all variable declarations that do not need to be assigned, and no line breaks between variables
(recommended) When a set of variable declarations is complete, the remaining code is written after an empty line

5. Within the same function, the declaration of a local variable must be placed at the top

Because even in the middle, the JS parser will be promoted to the top (hosting)

 The right writingvar clear= function (EL) {var ID= El. ID, Name= El. getattribute ("Data-name"); ... ... ... return true;} //non-recommended writing var clear = function (EL) { var id = El. ID; ... ... var name = Elgetattribute ("Data-name"); ... ... ... return true;

6. Intra-block functions must be declared with local variables

The wrong wordingvar call =function (name) {if (name = =  "Hotel") {function foo () {Console.log ( "Hotel foo");}} Foo && foo (); } //recommended notation var call = functionvar foo; if (name =  "Hotel") {foo = function (www.233077.cn) {console.log ( " Hotel Foo "); }} foo && foo (); 
/span>

7. (recommended) Add line breaks before a code snippet that needs to be closed with {}, for example: for if

 No line break, small code snippet cannot be distinguishedif (WL && wl.length) {for (i =0, L = www.huachengj1980.com wl.length; I < L; ++i) {p = wl[i];Type = Y.lang.Type (r[p]);if (S.hasownproperty (p) www.chaoyueyule.cn) {if (merge &&type = =  ' object ') {Y.mix (r[p], s[p]);} else if (ov |www.tkcyl1.com|!) P in R)) {R[p] = s[p];}} }} //has a line break, logic is much clearer if (WL && wl.length) {for (i = 0, L = wl.length; i < L; ++i) {p = wl[i]; type (R[p]); if (S.hasownproperty (www.chaoyueyule.com p)) {//handles merge logic Span class= "Hljs-keyword" >if (merge && type = =  ' object ' ) {Y.mix (r[p], s[p]);} else if (ov | |! ( P in R) {R[p] = s[p];              

Front-end encoding specification--javascript

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.