Sinsing analysis of programming specifications in JavaScript

Source: Internet
Author: User
Tags naming convention

This is what goes on in the previous blog post, first of all, to talk about the importance of programming specifications, which can keep our code consistent, predictable, and easier to read and understand. A new developer can get started faster by reading the code and understanding the codes written by other team members.

The 1th is indentation.

A bad thing is an inconsistent indentation because it looks like it follows the norm, but it may be accompanied by confusion and surprise along the way, and it is important to use indentation for the specification.

Some developers prefer to use tab tabs to indent, because anyone can adjust their editor to display tabs in the number of spaces they like. Some people like to use a space, usually four spaces. Generally this does not matter, as long as the team everyone follows the same specification is good.

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 () { C11/>return {                r:c + D            };}        ;    }    return inner;}

The 2nd is the curly braces .

Curly braces, also become curly braces. Technically, in or for if the statement has only one, then curly braces are not needed, but we should always use them, which makes the code more persistent and easy to update.

For the position of the left brace, someone likes to put it on the same line, and someone likes to put it on the next line, which is generally not important. However, in some cases, different brackets will behave differently. This is because of the semicolon insertion mechanism (semicolon insertion mechanism), because JavaScript will automatically fill up the line when we choose not to apply the semicolon, which can lead to trouble.

For example, the following code:

function func () {

Return

The following code does not perform

{

Name: "Xin"

}

}

If we want the function to return an object with the name attribute, we will find that because the semicolon is hidden, the function returns undefined, and the preceding code is equivalent to the following operation:

function func () {

return undefined;

The following code does not perform

{

Name: "Xin"

}

}

In short, just use curly braces, and always put the same line in the previous statement:

function func () {

return {

Name: "Xin"

};

}

The 3rd is the space

the use of spaces also helps to improve the readability and consistency of your code. Where spaces are suitable for use include:

(1) Part after the for loop semicolon is separated. For example, for (var i = 0; I < + + + + 1) {...}

(2) The multivariable that is initialized in the for loop. For example, for (var i = 0, max=; i < max; i + = 1) {...}

(3) after separating the comma of the array. such as var a = [1, 2, 3];

(4) After the comma of the object property, followed by a colon separating the property name and the property value. such as var o = {A:1,b:2};

(5) Defining function parameters. such as MyFunc (A, B, c)

(6) The anonymous function is followed by an expression. such as Var myFunc =function () {...}

(7) Before the curly brace of the function declaration. such as function MyFunc () {...}

(8) Use spaces to separate all operators and operands. such as +,-, *, = before and after the use of space.

The 4th is the naming convention .

we usually need to name our variables and functions in the same way, here are some rules:

(1) Write the constructor in uppercase letters. Although JavaScript does not have a class, there is a constructor for the new call,

For example, var adam = new person (), so that we can just look at the function name to know if the function is a normal function.

(2) Divide the word. When our variables or function names have multiple words, the separation of the best words follows a uniform norm.

A common practice is "camel nomenclature", which is the lowercase words, capitalized in the first letter of each word.

For constructors, you can use the big Hump naming method (upper camel case), such as Myconstructor ()

For function and method names, you can use the small hump naming method (lower camel case), such as MyFunction ().

For variables, developers usually use the small hump nomenclature, and use all the words in lowercase, using an underscore link,

For example, variables can be used first_name,old_company_name, etc.

(3) There is no solid-defined method in JavaScript,

Developers use all capitalized words to name variables that will not change in the program's declaration period.

For example var PI = 3.14, max_width = 900;

(4) Developers can only use an underscore prefix to represent a private property or a private method, such as

var person = {

Getname:function () {

return This._getfirst () + "+ this._getlast ();

},

_getfirst:function () {

...

},

_getlast:function () {

...

}

};

In the example above, GetName () represents a public method, a partially stable API. _getfirst () and _getlast () indicate the private. It is still a normal public method, but using an underscore prefix to warn the consumer of a person object these methods are not guaranteed to work in the next version and are not directly available.

For the common _private specification:

(1) Use the trailing underline to indicate the private, such as Name_ and Getelements_ ()

(2) Use an underscore prefix _ to denote the protected attribute, and two underscore prefix __ for the private property.


Sinsing analysis of programming specifications in 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.