How programming languages in JavaScript encode specifications

Source: Internet
Author: User

For engineers who are familiar with C + + or the Java language, JavaScript is flexible, easy to understand, and relatively loosely formatted for code. It's easy to learn and apply to your own code. Also because of this, JavaScript coding norms are often despised, the development process of tinkering, and eventually evolved into a follow-up maintenance personnel nightmare. The long-term value of software is directly proportional to the quality of the code. Coding specifications can help us reduce unnecessary trouble in programming. and JavaScript code is sent directly to the client browser, directly with the customer to meet, the quality of the coding should be more attention.

This paper discusses the problem of coding standard in JavaScript programming, and analyzes the reasons. Expect more WEB developers to focus on JavaScript coding specifications and focus on software PRODUCT quality issues.

Objective

It is believed that many engineers are not unfamiliar with the reference to C + + and Java coding specifications. But when it comes to coding specifications for JavaScript languages, you might be laughing. Isn't JavaScript a very flexible syntax? Variables can be declared at any time, the statement terminator can be not, strings and numbers can be added, and a few more arguments will not error. Yes, when you turn to JavaScript language from the strict syntax of C + + and Java, you will feel free and relaxed a lot. Loose grammar is an important feature of JavaScript. It is flexible and easy to understand, bringing a lot of convenience to developers, but if the writing process is not noticed, the debugging and maintenance costs of the code will increase invisibly.

JavaScript encoding should be sent directly to the client's browser, code specifications are not only the guarantee of code quality, but also affect the long-term reputation of the product. I hope that the specification of JavaScript programming language can also arouse the attention of more friends.

JavaScript Coding Specification Recommendations

In this paper, in terms of typesetting, naming, declaration, scope, and the use of some special symbols in JavaScript coding process, some suggestions are given according to the summary of individual learning work, and the reasons are analyzed.

JavaScript file references

JavaScript programs should be kept in the. js file, and it needs to be called in HTML to

As you can see from the output in Listing 4, the InF () function takes effect only within the OUTF () function, and the local variable Innera the scope of the internal function. This encoding allows the scope of variables and functions to become clearer.

Statement

For simple statements, it is still necessary to mention the semicolon necessity, while the line has at most one statement. If an assignment statement uses functions and objects to assign values, you may need to span multiple lines, and always remember to add a semicolon at the end of the assignment statement.

This is because in JavaScript, all expressions can be used as statements, and when line breaks are resolved to the end of an expression, a new error may be introduced when irregular wrapping and semicolons are lost.

For compound statements, if, for, while, do, switch, try ... catch and other code bodies, function-defined function bodies, object definitions, etc., need to be placed inside the curly braces ' {} '.

' {' should be at the end of the line, marking the beginning of the code block.

'} ' should be at the beginning of a line, marking the end of a block of code and needing to align with the beginning of the line ' {' to indicate a complete compound statement segment. This can greatly improve the readability of the code, control logic can be clearly demonstrated.

The included code snippet should be indented 4 more spaces.

Even if the included code snippet is only one sentence, it should be included with the curly brace ' {} '. It's not wrong to use curly braces, but if you need to add statements, it's easier to compile errors or logic errors due to missing curly braces.

The return statement is also prudent when used, and if the expression is executed as the returned value, put the expression and the returns in the same row so that the line break is incorrectly resolved to the end of the statement and cause a return error. Returns undefined if no expression is returned after the return keyword. The default return value for the constructor is this.

Listing 5. return expression

In Listing 5, you need to pay attention to the return error caused by the return expression not being placed on the same line as the returned keyword.

Special symbols

Blank character

The right blank line can greatly improve the readability of the code and make the code logic clearer and easier to understand. At the same time, the appropriate left blank in the expression, will also bring convenience to the code reading.

After the keyword is followed by parentheses, it is best to leave a blank between the keyword and the opening parenthesis ' (', for example, if, while, and so on. There is no space between the function name and the parentheses, but in the case of anonymous functions, you must leave blank between the function and the left parenthesis ' (', otherwise, the editor will mistakenly assume that it is called a functions.

In an expression, the two-dollar operator (except opening parenthesis ' (', left bracket ' [', Scope Point '. ') and two operands is best left blank. A unary operator (other than the word typeof) and its operands should not be left blank.

Comma ', ' after the need to leave blank to show clear parameter intervals, variable intervals, and so on.

Semicolon '; ' This usually indicates the end of the expression statement and should be blank. In the for conditional statement, the semicolon should be left blank.

{} and []

In JavaScript, if you want to define empty objects and empty arrays, it is often natural to think of the new object () and the new Array () method. In fact, the curly braces ' {} ' and the square brackets ' [] ' can be used directly to define an empty object and an empty array. This writing method makes the code look 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.