Home > Web Front-end > JS Tutorial > body text

JavaScript writes maintainable code

小云云
Release: 2017-12-06 15:31:05
Original
1776 people have browsed it

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
Copy after login

Basic formatting

Indentation level

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

End of statement

结尾分号
Copy after login
Copy after login

Example of error

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

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
    }
}
Copy after login
Copy after login

The length of the line

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

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;
Copy after login
Copy after login

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个字符。
Copy after login
Copy after login

End of statement
结尾分号
Copy after login
Copy after login

Example of error

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

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
    }
}
Copy after login
Copy after login

The length of the line
80个字符
Copy after login
Copy after login

Line break
通常我们会在运算符后换行,下一行会增加两个层级的缩进(8个字符)
Copy after login
Copy after login
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;
Copy after login
Copy after login

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!

Related labels:
source:php.cn
Statement of this Website
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
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!