Home  >  Article  >  Web Front-end  >  JavaScript writes maintainable code

JavaScript writes maintainable code

小云云
小云云Original
2017-12-06 15:31:051770browse

The basic formatting indentation level is 4 spaces indentation, and it is best to unify tabs to 4 characters. An example of an error caused by a trailing semicolon at the end of a statement.

//原始代码function getData() {    return     {        title:"Maintainable JavaScript",        author:"Nicholas C. Zakas"    }
//分析器会它理解function getData

Basic formatting

Indentation level

4个空格缩进,最好统一tab为4个字符。

End of statement

结尾分号

Example of error

//原始代码function getData() {
    return 
    {
        title:"Maintainable JavaScript",
        author:"Nicholas C. Zakas"
    }//分析器会它理解function getData() {
    return;
    {
        title:"Maintainable JavaScript",
        author:"Nicholas C. Zakas"
    };
}

This problem can be fixed by moving the opening curly brace to the same line as return.

//这段代码工作正常,尽管没有用分号function getData() {
    return {
        title:"Maintainable JavaScript",
        author:"Nicholas C. Zakas
    }
}

The length of the line

80个字符
Line break
通常我们会在运算符后换行,下一行会增加两个层级的缩进(8个字符)

Exception: When assigning a value to a variable, the position of the second line Should be aligned with the position of the assignment operator, for example

var result = something + antherThing + yetAnotherThing + somethingElse + 
             anotherSomethingElse;

empty line

  • between methods

  • Between local parts of a method and statements

  • Before multi-line or single-line comments

  • Between logical fragments within a method Insert blank lines between spaces to improve readability

##Basic formatting

Indentation level
4个空格缩进,最好统一tab为4个字符。

End of statement
结尾分号

Example of error

//原始代码function getData() {
    return 
    {
        title:"Maintainable JavaScript",
        author:"Nicholas C. Zakas"
    }//分析器会它理解function getData() {
    return;
    {
        title:"Maintainable JavaScript",
        author:"Nicholas C. Zakas"
    };
}

This problem can be fixed by moving the opening curly brace to the same line as return.

//这段代码工作正常,尽管没有用分号function getData() {
    return {
        title:"Maintainable JavaScript",
        author:"Nicholas C. Zakas
    }
}

The length of the line
80个字符

Line break
通常我们会在运算符后换行,下一行会增加两个层级的缩进(8个字符)
Exception: When assigning a value to a variable, the position of the second line Should be aligned with the position of the assignment operator, for example

var result = something + antherThing + yetAnotherThing + somethingElse + 
             anotherSomethingElse;

empty line

  • between methods

  • Between local parts of a method and statements

  • Before multi-line or single-line comments

  • Between logical fragments within a method Insert blank lines in between to improve readability

The above content is maintainable code written in JavaScript. I hope it can help everyone.

Related recommendations:

Tutorial on writing a simple AJAX method library in JavaScript

Common JavaScript memory leaks

Introduction to the use of split function in JavaScript from shallow to deep

The above is the detailed content of JavaScript writes maintainable code. For more information, please follow other related articles on the PHP Chinese website!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn