Home > Article > Web Front-end > JavaScript writes maintainable code
//原始代码function getData() { return { title:"Maintainable JavaScript", author:"Nicholas C. Zakas" } //分析器会它理解function getData
4个空格缩进,最好统一tab为4个字符。
结尾分号
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 } }
80个字符
通常我们会在运算符后换行,下一行会增加两个层级的缩进(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;
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
4个空格缩进,最好统一tab为4个字符。
结尾分号
//原始代码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 } }
80个字符
通常我们会在运算符后换行,下一行会增加两个层级的缩进(8个字符)
var result = something + antherThing + yetAnotherThing + somethingElse + anotherSomethingElse;
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!