Document the more useful coding specifications (the guidelines are based on the Java language encoding specification and JAVASCRIPT programming specifications, combined with the personal experience and preferences of the author Nicholos Zakas).
Some specifications for formatting (including indentation, line length, operator spacing, bracket spacing, object Direct volume, comments, single-line comments, multiline annotations, and so on) are not recorded here.
A.3 Original Value
var name = "Nicholos"//var name = ' Nicholos ' // var longstring = "Here's the story, of a man named Brady.";
Special value NULL should be avoided in addition to the following conditions.
① used to initialize a variable
② is used to compare to an already initialized variable, which can or may not be an object
③ when a function's argument is expected to be an object, it is used as a parameter passed in
④ when the return value of a function is expected to be an object, it is used as the return value outgoing
For example:
//good wording.varperson =NULL;//good wording.functiongetperosn () {if(condition) {return NewPerson ("Nicholas"); } Else { return NULL; } }//good wording.varperson =getperosn ();if(Person!==NULL) {dosomething (); }//bad notation: compare to an uninitialized variablevarPerson ;if(Person! =NULL) {dosomething (); }//bad writing: test to determine whether a parameter is passedfunctiondosomething (arg1, arg2, Arg3, Arg4) {if(Arg4! =NULL) {dosomethingelse (); }}
Avoid using special value undefined. Determine if a variable should be defined using the TypeOf operator.
// good wording. if (typeof variable = = "undefined") {//dosomething}// Bad wording: Using the undefined direct volume if (variable = = undefined) { //dosomething }
Writing maintainable JavaScript reading notes (appendix A): JavaScript Coding style Guide (1) original value