JavaScript Authoring Style Guide

Source: Internet
Author: User

Refer to "Writing maintainable JavaScript"

One: Indent
The level of the first row consists of 4 spaces to avoid indentation with TAB tab

Good wording.
if (true) {
DoSomething ();
}

Two: The length of the line
The length of each line should not exceed 80 characters. If a row is for 80 characters, it should be in an operator (comma, plus sign)
After a line break. The next line should increase the level two indent

Good wording.
DoSomething (argument1, Argument2, Argument3, Argument4,
ARGUMENT5);

Three: Original value
Strings should use double quotation marks (avoid using single quotes) and keep one line. Avoid using slashes in strings for another line

Good wording.
var name = "M";

Bad wording.
var name = ' M ';

Bad writing: Wrapping before the end of a string
var longstring = "Here's the sorry, of a man \
Named Brady. ";

Numbers should use decimal integers, scientific notation for integers, hexadecimal integers, or decimal floating-point decimals,
You should keep at least one digit before and after the decimal point. Avoid using octal direct volume

Good wording.
var count = 10;
var price = 10.00;
var price = 10.0;
var num = 0x23;

Bad wording.
var price = 10.;
var price =. 1;
var price = 010;

Special value NULL should be avoided except in the following cases
1. Used to initialize a variable, this variable may be assigned to an object
2. To compare with an already initialized variable, this variable can or may not be an object
3. When the function's argument is expected to be an object, it is used as a parameter passed in
4. When the return value of a function is expected to be an object, it is used as the return value outgoing

Example:

Good wording.
var person = null;

Good wording.
function Getperson () {
if (condition) {
return new Person ("M");
} else {
return null;
}
}

Good wording.
var person = Getperson ();
if (person!== null) {
DoSomething ();
}

Bad notation: compare to an uninitialized variable
var person;
if (person! = null) {
DoSomething ();
}

Bad writing: Test to determine whether a parameter is passed
function dosomething (arg1, arg2, Arg3, Arg4) {
if (Arg4!== null) {
DoSomething ();
}
}

Avoid using special values undefined determine if a variable is defined should use the TypeOf operator

Good wording.
if (typeof variable = = "undefined") {
Do something;
}

Bad writing: The use of undefined direct volume
if (variable = = undefined) {
Do something;
}

Four: operator spacing


A space must be used before and after the ternary operator to keep the expression neat.
Operators include assignment operators and logical operators

Good wording.
var found = (Value[i] = = = False);

Good wording.
if (found && (Count > 10)) {
DoSomething ();
}

Good wording.
for (i = 0; i < count; i++) {
Process (i);
}

Bad wording: Lost space
var found = (varlues[i]===false);

if (found&& (count>10)) {
DoSomething ();
}

for (i=0; i<count; i++) {
Process (i);
}

JavaScript Authoring Style Guide

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.