How to code standards for programming languages in javascript

Source: Internet
Author: User
Tags coding standards

For Engineers familiar with the C/C ++ or Java language, javascript is flexible, easy to understand, and requires relatively loose code formats. It is easy to learn and use it in your own code. Because of this, javascript coding standards are often despised. Fixing and complementing during the development process will eventually become a nightmare for subsequent maintenance personnel. The long-term value of software is directly proportional to the coding quality. Coding specifications can help us reduce unnecessary troubles in programming. While javascript code is directly sent to the client's browser and directly met with the customer, the quality of code should be paid more attention.

This article describes the coding standards in javascript programming and analyzes the reasons. It is expected that more Web developers will pay attention to javascript coding standards and software product quality issues.

Preface

I believe many engineers are not unfamiliar with the C/C ++ and Java coding specifications. However, when it comes to the javascript language encoding specifications, you may not be able to help. Isn't javascript syntax flexible? Variables can be declared at any time; statement Terminator can be avoided; string and number can also be added; one or more parameters will not report an error. Yes, when you switch from the strict C/C ++ and Java syntax rules to the javascript language, you will feel a lot of freedom and much easier. Loose syntax is an important feature of javascript. It is flexible and easy to understand, and brings a lot of convenience to developers. However, if you do not pay attention to the coding process, the debugging and maintenance costs of the code will be invisible.

Javascript code will be directly sent to the browser on the client as needed. The code specification not only guarantees the code quality, but also affects the long-term reputation of the product. We hope that the standardization of the javascript programming language will also attract more attention from friends.

Javascript coding specifications

This article provides some suggestions for the formatting, naming, declaration, scope, and use of some special symbols involved in the javascript coding process, and analyze the reason for reference.

Javascript file reference

Javascript programs should be stored in. js files as much as possible.

From the output in Listing 4, we can see that the inF () function takes effect only within the outF () function, and the innerA local variable takes effect for the scope of the internal function. This encoding method makes the scope of variables and functions clearer.

Statement

For a simple statement, we need to mention the necessity of a semicolon. At the same time, a maximum of one statement can be entered in a row. If a value assignment statement uses functions and objects to assign values, it may need to be performed across multiple rows. Be sure to add a semicolon at the end of the value assignment statement.

This is because in javascript, all expressions can be used as statements. In case of line breaks, it will be resolved to the end of the expression. In this case, nonstandard line breaks and the loss of semicolons may lead to new errors.

For compound statements, if, for, while, do, switch, try... Catch and other code bodies, function-defined function bodies, and object definitions must be placed in curly brackets.

'{' Indicates the beginning of the code block at the end of the line.

'}' Should start with a line, marking the end of the code block. It must be aligned with the start of the row where '{' is located to indicate a complete compound statement segment. This greatly improves the readability of the code, and the control logic can be clearly displayed.

The included code segment should be indented with four spaces.

Even if the included code segment contains only one sentence, it should be included in braces. Although no curly braces are required, compilation errors or logical errors may be caused by the omission of curly braces if you need to add statements.

The return statement must also be used with caution. If the expression execution is used as the return value, put the expression and return in the same line to avoid the return error caused by the end of the statement. If no expression is returned after the return keyword, undefined is returned. The default return value of the constructor is this.

Listing 5. return expression

Listing 5 shows the returned error because the return expression is not in the same line as the return keyword.

Special symbols

Blank Space Character

Appropriate blank lines can greatly improve the readability of the Code and make the code logic clearer and easier to understand. At the same time, leaving the appropriate blank in the expression will also facilitate the reading of the Code.

If there are Parentheses behind the keyword, it is best to leave a blank between the keyword and the left brace '(', such as for, if, while, etc. The function name and brackets should not be left blank, but if it is an anonymous function, it must be left blank between the function and left brace ('). Otherwise, the editor will mistakenly think that the function name is function.

In an expression, it is best to leave a blank space between the binary operator (except the left brace '(', left brace '[', scope point '.') and the two operands. Do not leave a blank space between the unary operator (if not the word typeof) and Its operands.

The comma ',' must be left blank to display the specified parameter interval and variable interval.

The semicolon ';' usually indicates the end of the expression statement, but should be empty. In the for Condition Statement, leave blank after the semicolon.

{} And []

In javascript, if empty objects and empty arrays need to be defined, the new Object () and new Array () methods are naturally used. In fact, braces '{}' and square brackets '[]' can be directly used to define an empty object and an empty array. This writing method makes the code easy to understand.

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.