Common coding conventions for JavaScript base JavaScript (007)

Source: Internet
Author: User
Tags array definition

Using certain coding conventions, you can make the code in your project refer to high consistency, readability, and predictability.

1. Indent
Indentation can improve the readability of your code. However, incorrect indentation can also cause code to be misread. Some people think that indentation should use tab, while others advocate the use of 4 spaces. Indentation of 4 spaces is currently the mainstream approach. Simply put, 4 spaces should be indented for all the code in the curly braces (including do, while, for, for-in, if, switch, and object properties in the object declaration). Here is an example of indentation:

function outer (A, b) {
var c = 1, d = 2, inner; if (a > B) { inner = function () { return { r:c-D }; }; } else { inner = function () { return { r:c + D };} ; } return inner;}

2. Curly Braces
The curly braces can be omitted for the contents of the loop body and if statement only one line, but a good habit should always be a curly brace:

  Bad practice for (var i = 0; i <; i + = 1)    alert (i);    Alert (i + "is" + (i% 2? ") Odd ":" even "));

So we recommend always adding curly braces:

for (var i = 0; i <; i + = 1) {    alert (i);}

This is true for the IF statement:

Badif (True)    alert (1), Else    alert (2),//Betterif (TRUE) {    alert (1);} else {    alert (2);}

3. Position of the left curly brace
Someone likes to put the left curly braces on the top line, and someone likes to open another line:

if (true) {    alert ("It ' s true!");} Or:if (True) {    alert ("It ' s true!");}

There is no problem with the code above, and the two forms can be any one of the first. However, if the return value of the function is an object, care must be taken://warning:unexpected return value

function func () {    return    {        name: "Batman"    };}

If you expect the above code to return an object, the result might be unexpected. Because the function above returns the undefined. Because the return without the semicolon looks like this for the JavaScript interpreter://warning:unexpected return value

function func () {      return undefined;      Unreachable code follows      ... {          name: "Batman"      };}

Therefore, we should always place the first curly brace at the end of the previous line:

function func () {    return {        name: "Batman"    };}

4. Spaces
Spaces should make the code easier to read, so there are a lot of places where spaces should be placed:
In the FOR Loop statement, after the semicolon: for (var i = 0; i <; i + = 1) {...}
The For statement is also the same when initializing multiple variables: for (var i = 0, max = 10;i < max; i + = 1) {...}
The array definition is followed by a comma: var a = [1, 2, 3];
Define the properties of the object after the colon and after the comma: var o = {a:1, b:2};
After the comma in the function argument list: MyFunc (A, B, c)
The curly braces in the function's fame are preceded by: function MyFunc () {}
When the anonymous function is known, the functions keyword follows: var myFunc = function () {};

In short, the space should allow the code to breathe:

Generous and consistent spacing//makes the code easier to read//allowing it to "breathe" var d = 0,    a = B + 1;if ( A && b && c) {    d = a% C;    A + = D;} antipattern//missing or inconsistent spaces//make the code confusingvar d= 0,    a =b+1;if (a&& b&&c ) {d=a%c;    a+= D;}

5. The function name of the constructor to capitalize the first letter
JavaScript does not have a "class" concept, but it can be known as a constructor called with new:

var adam = new Person ();

This constructor is not like Java or C, and is placed in the definition of the class, so it looks the same as the other functions. To differentiate a constructor, you can capitalize the first letter of the function name: Functions Myconstructor () {...}

function myFunction () {...}

We'll also show you how to make a function look like a constructor (rather than a general function) when used. But they can be effectively distinguished by the initial letter.

6. Distinguishing words in identifiers

For a word in a function name, it should be in the case of a camel-cased. such as: MyFunction (), CalculateArea () and Getfirstname ().
For variables, or properties of an object, it should be underlined: first_name, Favorite_bands, Old_company_name.
For constants, it should be in the form of all capitals:

Precious constants, please don ' t touchvar PI = 3.14,    max_width = 800;

For private properties and methods in an object, precede them with an underscore:

var person = {    getname:function () {        return This._getfirst () + ' + this._getlast ();    },    _getfirst:funct Ion () {        //...    },    _getlast:function () {        //...    }

In the above code, GetName is not underlined, meaning the method of public, while underlined _getfirst and _getlast are private methods.

Common coding conventions for JavaScript base JavaScript (007)

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.