Home > Web Front-end > JS Tutorial > JavaScript Coding Standards Recommended_Basic Knowledge

JavaScript Coding Standards Recommended_Basic Knowledge

WBOY
Release: 2016-05-16 17:53:13
Original
1207 people have browsed it
1. Naming
1. Give variables and functions a meaningful name, do not name them randomly.
2. Non-constructor functions use camel case naming, and try to use a verb-object structure to distinguish them from variable names, such as getName or IsFull. The first letter of the constructor (that is, a custom type) name is capitalized to distinguish it from non-constructor functions, such as Person.
3. Variables use camel case naming. Since JavaScript is a weakly typed language, it is recommended to prefix the variable name: integer (i), floating point number (f), boolean (b), string (s), array (a). But it is not mandatory to do this. You can choose according to your personal preferences. Once you have made the choice, do not mix the two methods of adding prefixes and not adding prefixes.

2. Layout
1. Space.
a) Leave a space between var and the variable name, a space between the variable name and the equal sign, a space between the equal sign and the initial value, and no space between the initial value and the semicolon. For example: var i = 10;
b) When using literals to declare reference type variables, leave no spaces between each attribute and the colon, and leave a space between the colon and the initial value. For example:
Copy code The code is as follows:

var Person = {
age: 16 ,
name: "Sam"
};

c) Leave a space between function and function name, no space between function name and (), () and { Leave a space between them.
d) Leave a space between each parameter of the function.
e) Leave a space between if, while, for and the left bracket to emphasize the keyword; leave no space between switch, with and the left bracket.
f) Leave a space between the binary operator and the left and right operands. When a line of code is long, no spaces can be left.
2. Line break.
a) Each statement occupies one line. Do not use multiple statements on one line.
b) The braces { after block-level scopes such as if, while, for, etc. do not start on a new line, just put them on the same line as the keywords.
3. Indent.
a) Use 4 spaces for indentation and do not use tabs.
b) When the scopes are different, they should be indented to show their hierarchical relationship.

3. Comments
1. Add comments appropriately. Comments cannot be completely absent, nor are the more the better. Just add comments to important methods, variables, and algorithms (or other issues that need attention).
2. When modifying the source code, you need to modify the comments simultaneously to keep them consistent.
3. Do not use html comments in the code.

4. Specifications
1. The var keyword must be added when declaring variables. Although JavaScript allows the var keyword to be omitted and become a global variable, this is a source of problems.
2. When declaring a variable, it must be initialized at the same time. It is best not to change the data type of the variable later.
3. If a semicolon can be added at the end of the statement, it must be added.
4. If, while, for, etc. have only one statement, they also need to be placed within braces.
5. Do not use global variables arbitrarily. If you have to use them, it is best to use only one global variable.
6. JavaScript should be loosely coupled with html and css. HTML is the data layer, CSS is the presentation layer, and JavaScript is the behavior layer. Tight coupling between the three should be avoided, otherwise it will be difficult to maintain later. There should be no specific JavaScript code in html, and all external files should be included; in JavaScript, try not to use innerHTML to insert a large number of html elements. You should consider placing elements in html, but just hide them initially; do not directly use JavaScript To modify specific properties in css, you should modify them indirectly through className.
7. Do not modify objects that are not owned by you, do not add properties or methods to their instances or prototypes, and do not repeatedly define their existing methods. Otherwise, potentially subtle problems could result when a new version of the object adds a property or method with the same name. There are two solutions: one is inheritance and the other is inclusion.
8. Use namespaces to prevent conflicts between multiple libraries. Please refer to the organization of the YUI library.
9. For literals that appear in the code, they should be placed in the attribute of a variable, with the first letter of the attribute name or all letters capitalized (simulating define or enum in other languages). For example:
Copy code The code is as follows:

var Color = {
RED: 1 ,
BLUE: 2,
GREEN: 3
};

10. 함수에 전달된 매개변수를 확인하세요. 기본 유형인 경우 typeof를 사용하고, 참조 유형인 경우 objectof를 사용하고, 객체에 메소드가 포함되어 있는지 확인하려면 해당 메소드에 typeof 연산자를 사용하고 이를 "undefine" 문자열과 비교하세요.

5. 성능
1. 글로벌 검색을 피하세요. 전역 변수 및 함수를 사용하는 것은 지역 변수 및 함수를 사용하는 것보다 비용이 더 많이 듭니다. 전역 변수 및 함수에는 범위 체인 조회가 포함되기 때문입니다. 따라서 함수 내에서 전역 변수를 여러 번 사용하는 경우에는 여러 번 범위 체인 검색을 수행하여 이러한 문제를 방지하기 위해 여러 번 사용된 전역 변수를 지역 변수에 할당하고 해당 지역 변수를 사용하게 됩니다. 미래에.
2. 마녀 표현을 사용하지 마세요. with 문은 자체 범위를 생성하므로 추가 오버헤드가 발생합니다.
3. 속성 조회를 피하세요. 속성 조회는 O(n) 작업이며 객체에 대한 모든 속성 조회는 변수 및 배열에 액세스하는 것보다 시간이 더 걸립니다(변수 및 배열에 액세스하는 것은 O(1) 작업입니다). 따라서 동일한 속성을 여러 번 사용하는 경우에는 로컬 변수에 저장해야 합니다. 예:
코드 복사 코드는 다음과 같습니다.

var sUrl = window.location .href;
var sData = sUrl.substring(sUrl.indexOf("?"))
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