Analysis on programming specifications in JavaScript and Xinxing javascript

Source: Internet
Author: User
Tags define function

Analysis on programming specifications in JavaScript and Xinxing javascript

This is written in the previous blog post. First, let's talk about the importance of programming specifications. It keeps our code consistent, predictable, and easier to read and understand. A new developer can read the code written by other team members to get started faster.

The first point is indent.

One bad thing is inconsistent indentation, because it looks like it complies with specifications, but it may be accompanied by chaos and surprises along the way. What's important is the standard indent.

Some developers prefer to use tab indentation, because anyone can adjust their editor to display the tab with the number of spaces they like. Some people also like to use spaces, usually four spaces. Generally, this doesn't matter, as long as everyone in the team follows the same rule.

Below 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;}

The second is curly brackets.

Curly braces are also braces. Technically, if there is only one statement in or for, curly brackets are not required, but we should always use them, this makes the code more persistent and easy to update.

For the left braces, some people like to put them in the same line, while others like to put them in the next line, which is generally not important. However, in some cases, different brackets may behave differently. This is because of the semi-colon insertion mechanism (semicolon insertion mechanic), because JavaScript will help us automatically complete the code when we choose not to apply the semi-colon to end a line of code, which may cause trouble.

For example, the following code:

Function func (){

Return

// The following code is not executed

{

Name: "xin"

}

}

If we want the function to return an object containing the name attribute, we will find that the function will return undefined because the semicolon is hidden. The previous code is equivalent to the following operation:

Function func (){

Return undefined;

// The following code is not executed

{

Name: "xin"

}

}

In short, curly braces are used and always put in the same line as the previous statement:

Function func (){

Return {

Name: "xin"

};

}

The third point is space.

The use of spaces also helps improve code readability and consistency. Spaces are applicable:

(1) parts separated by semicolons in the for loop. For example, for (var I = 0; I <10; I + = 1 ){...}

(2) multi-variables initialized in the for loop. For example, for (var I = 0, max = 10; I <max; I + = 1 ){...}

(3) Separate the comma (,) of the array. For example, var a = [1, 2, 3];

(4) The end of the object attribute comma and the end of the colon that separates the attribute name and attribute value. For example, var o = {a: 1, B: 2 };

(5) define function parameters. For example, myFunc (a, B, c)

(6) The end of the anonymous function expression function. For example, var myFunc = function (){...}

(7) the front of the curly braces in the function declaration. For example, function myFunc (){...}

(8) use spaces to separate all operators and operation objects. For example, +,-, *, =, and so on.

The fourth point is the naming convention.

We usually need to name our variables and functions in the same form. Below are some rules:

(1) Write constructors with uppercase letters. Although JavaScript does not have classes, there are constructors called by new,

For example, var adam = new Person (); then we can check the function name to see whether the function is a normal function.

(2) separate words. When our variables or function names have multiple words, it is best to keep a unified rule for word separation.

A common practice is "camel naming method", which means lowercase words, and the first letter of each word is capitalized.

For constructors, you can use the upper camel case method, such as MyConstructor ()

For functions and method names, you can use the lower camel case method, such as myFunction ().

For variables, developers usually use the small camper method, and all words can be used in lower case with underline links,

For example, you can use first_name and old_company_name for variables.

(3) there is no definition of the always-bright method in JavaScript,

Developers use uppercase letters to name variables that will not change during the program declaration period.

For example, var PI = 3.14, MAX_WIDTH = 900;

(4) developers can use only one underline prefix to indicate a private attribute or method, such

Var person = {

GetName: function (){

Return this. _ getFirst () + ''+ this. _ getLast ();

},

_ GetFirst: function (){

...

},

_ GetLast: function (){

...

}

};

In the preceding example, getName () indicates a public method and some stable APIs. While _ getFirst () and _ getLast () indicate private. It is still a normal public method, but it uses the underline prefix to warn the user of the person object that these methods cannot work in the next version and cannot be used directly.

For common _ private specifications:

(1) Use the trailing underline to indicate private, such as name _ and getElements _()

(2) Use an underscore prefix _ to indicate the protected attribute, and the underscore prefix _ to indicate the private attribute.


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.