Netease mail frontend Javascript code specification: Basic specification

Source: Internet
Author: User
Tags coding standards
During the many years of mailbox webmail development, the Netease mail front-end team has accumulated many experiences. We have developed many basic js libraries and implemented a large number of front-end effect components, we have developed a mature opoa framework and api components to share with you. Today I want to talk to you about javascript... syntaxHighlighter. all (); during years of mailbox webmail development, the front-end team of Netease mail has accumulated many experiences. We have developed many basic js libraries and implemented a large number of front-end performance components, we have developed a mature opoa framework and api components to share with you. Today, I want to talk to you about the javascript coding standards.
In general, javascript is a language with extremely flexible syntax. Javascript was designed to add dynamic effects to HTML. Due to its dynamic, weak type, and compatibility issues with different browsers, the development cost is much higher than that of java and other languages. Because it is too flexible, we have developed javascript code specifications for Netease mail, as much as possible to reduce the problems caused by flexible syntax. The following is a detailed introduction:
1. variable naming rules
Variable names include global variables, local variables, class variables, and function parameters.
Basic specifications
Variable names are composed of prefix types and meaningful words, and the first letter of a word must be capitalized. For example, sUserName and nCount.
Prefix Specification
Each local variable must have a type prefix, which can be divided:
S: string. Example: sName, sHtml;
N: Number. Example: nPage, nTotal;
B: indicates the logic. Example: bChecked, bHasLogin;
A: array. Example: aList, aGroup;
R: indicates a regular expression. Example: rDomain, rEmail;
F: indicates a function. Example: fGetHtml, fInit;
O: other objects not involved above, such as oButton and oDate;
Exceptions:
1: temporary variables with little scope can be abbreviated as str, num, bol, obj, fun, and arr.
2: cyclic variables can be abbreviated as I, j, and k.
Why do we need to force a variable prefix like this? This is because javascript is a weak language. When defining a large number of variables, we need to clearly understand the attributes of the current variable. It is difficult to distinguish only common words.
For example:
Var group = [];
Group. name = 'mygroup ';
/****
Some code
***/
// At this time, can you see at a glance what the group is?
For example:
Var checked = false;
Var check = function (){
Return true;
}
/**
Some code
**/
If (check) {// The checked may be written as check. Because check cannot be quickly found to be a function, it may cause a logic error.
// Do some thing
}
If we write:
Var bChecked = false;
Var fCheck = function (){
Return true;
}
/**
Some code
**/
If (bChecked ){
// Do some thing
}
If (fCheck ()){
// Do other thing
}
It is clear.
Global variables and constant specifications
Netease mail front-end is based on the concept of "class" to develop javascript (will be introduced later), each class definition is in a closure function, in addition to class definitions in windows, only two types of variables can be defined globally, that is, global variables and constants.
The global variable uses g as the prefix and is defined under the window. For example, gUserName and gLoginTime.
Some variables that cannot be modified are considered constants, and all letters are capitalized. For example, COPYRIGHT and PI. Constants can exist in functions or globally.
Let's take a look at the example to figure out why the definition is as follows:
Var userName = "dongua ";
Function checkName (userName ){
// The userName parameter and the global variable userName exist. To compare the two values, you must enter
Return window. userName = userName
}
If the prefix of global variables is used, it is very clear.
2. Function naming rules
The verb or verb [+ noun] form is used in a unified manner, for example, fGetVersion (), fSubmitForm (), fInit (); functions that involve returning logical values can use is, has and other words that represent logic replace verbs.
If an internal function exists, use the _ f + Verb [+ noun] form. The internal function must be defined at the end of the function. For example:
Function fGetNumber (nTotal ){
If (nTotal <100 ){
NTotal = 100;
}
Return _ fAdd (nTotal );
 
Function _ fAdd (nNumber ){
NNumber ++;
Return nNumber;
}
}
Alert (fGetNumber (30); // alert 101
Object method implementation
Object method naming uses the form of f + object class name + Verb [+ noun]; for example, fAddressGetEmail
Event Response Function
An event response function is named by the trigger event object name + event name or module name + trigger event object name + event name, for example, fDivClick (), fAddressSubmitButtonClick ()
3. Other considerations
1: All names are best represented in English.
2: all variable names should be clear and necessary, and try to avoid unnecessary abbreviations that are easy to confuse.
3: netease. events. mouse. Handler, instead of netease. events. mouse. MouseEventHandler.
4: The corresponding method should use the corresponding verb, such as get/set, add/remove, create/destroy, start/stop, insert/delete, begin/end.
5: Double Negative variables, such as bIsNotError and bIsNotFound, should be avoided.
6: variables should be defined within the minimum range and the minimum active time should be kept as much as possible.
7: it is best to define the loop variable in the loop. For example, for (var I = 0, m = 10; I 8: avoid complex condition statements as much as possible. Use a temporary boolean variable instead.
9: do not execute the statement in the condition. For example, if (I = 3)> 2) {} is not advisable.
10: Do not repeatedly use numbers of the same meaning in the code, instead of a variable, such as nTotal = 100; num = total.
 
 


From Netease mail front-end technology center

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.