some naming conventions or JS book naming conventions, like to mix naming conventions with syntax such as:
1. Use "Var" to define, initialize variables to prevent global variables, a multi-variable definition using "," (itself this way is very controversial).
2. The end must be added ";" Prevent JS execution effect is not the same as imagined.
3. Judge two number equal use "= = =" Do not use "= =". (In fact, the "=" problem rarely occurs, and JS's syntax section will also refer to the proposed use of "= = =")
And so the specification is too lengthy, in my opinion, the naming convention should not be too many, but the most commonly used in the field code list can be.
Have studied some of the framework of low-level code, read the "Write maintainable code" to sort out the JS coding specifications that feel appropriate for themselves:
1. Naming.
The text is too cumbersome, the following examples directly illustrate:
Constant Age my_age General variable age my_name my_firstname private Property _age _my_name _my_firstname function init () ChangeName () Changmyname () Private Method _init () _changename () _changemyname () class Sprite jumpsprite
2. Methods and functions suggested by verbs:
Sprite.gettag (); Sprite.isload ();
3. Interval, indent.
Interval: the operator is separated by a space interval (except for the special minus sign "-" and self-decrement).
for 0, len = items.length; i < Len; i++) { dosomething (items[i]);}
Indent: Although some articles think that 2 squares of space indentation is easy to read and code transfer, but actually 2 line indentation is difficult to operate on most Ides
Because the default indentation for the mainstream IDE is 4, for problems caused by using "tab", use the IDE, Web or local format software
"Format code" can be resolved.
4. Line width:
A line of code more than 80 lines, different IDE has different hints: such as editplus in the lower right corner of the current number of rows, the number of columns prompt
The Webstorm directly has an obvious line-dividing hint.
5. Array, object definition:
Define an array without the new array (), but instead with "[]", define the object without the new object and use "{}" instead.
Off Topic:
The variables and attributes in JS are different, for example, properties can be deleted using "delete", and variables defined by "Var" cannot.
The same method is different from the function, a method of a class can be accessed externally and a function in a class cannot.
var TestClass = function () { // externally accessible this . GetName = function () {} // external inaccessible function Getage () {}}; var New TestClass (); var // Normal var // Error
Attach the JS specification of a top 1500 company:
1. Prohibit enumeration of arrays using For-in statements. (because for-in traversal, the redundant content is traversed, such as the extended method).
2. Prohibit the use of with statements. (Error-prone, security-risk, and alternative code implementations).
3. The business code prohibits the use of eval. (Ibid., mainly to prevent XSS attacks)
Disable the function constructor, which is another form of eval.
It is forbidden to pass string type arguments to settimeout or setinterval, otherwise the behavior of these two methods is similar to eval.
4. Prohibit the use of new Object (), must be replaced with {}. (Assigning a large number of new variable addresses in JavaScript is a slow operation and you should always use object notation for efficiency.) )
5. Prohibit the use of the new Array () and must be substituted with []. Ibid.
JavaScript coding specification [original]