Many documents on the Internet, especially the Google specifications, have been referenced for the JavaScript programming specifications drafted by the company. Now, I hope you can give me some valuable comments.
This specification is developed based on the reality that JavaScript functional programming styles and companies rely heavily on jquery for coding.
Disable the use of Eval, with, and caller (use strict requirements of ecma262 V5 ). Eval can only be generated by machines during encryption.
The VaR keyword must be added to declared variables. In principle, continuous declarations are not allowed except in statements such as for (;) loops. Because of Connection
The efficiency of resuming declarations is not as high as that of separate declarations, and it is easy to expose them to the global scope by mistake.
Constant, all uppercase.
Variable name, which cannot use Pinyin. English words are organized in the camper style.
Semicolons (;) are added to all places that can be added (except for, function, if, switch, try, while) to prevent compression failure due to this problem.
Custom class, which can only be used for building the UI library, businessCodeClasses are not allowed to be defined without authorization.
In principle, pseudo objects (string, number, and Boolean) cannot be used directly.
In principle, array and object are not allowed, and they are directly [], {}.
Handle this with caution to prevent binding from being invalid and point to the window. We recommend that you use that to reference it.
If you want to call the function itself, do not use the name function expression. Write the following code in the first line of the target function.
Secrets of naming function expressions:
VaR self = arguments. callee;
Do not extend native object prototypes, especially object. prototype.
Do not bind events to an element on the page, that is, the following code style:
<A href = "aaaa" Jump = "BBB" onclick = "Goto (this. Jump)"> jump to a page </a>
Disable the use of IE conditional comments, and no compression is allowed.
VaR F = function () {/* @ cc_on if (@ _ JScript) {return 2 * @ */3 ;/*@}@*/};
Block Function Declaration prohibited. For details, please google name function expression secrets.
If (x) {// ng function Foo () {}} if (x) {var Foo = function (){}}
The for-in loop can only be used for objects.
Multi-line string is prohibited, because the blank characters at the starting position of the line cannot be ignored during compilation; the blank characters after "\" will generate an odd
Strange error. Although most script engines support this method, it is not the standard specification of ecmascript.
VaR mystring = 'a rather long string of English text, an error message \ Actually that just keeps going and going -- an error \ message to make the Energizer Bunny blush (right through \ those Schwarzenegger shades )! Where was I? Oh yes, \ you \ 've got an error and all the extraneous whitespace is \ just gravy. Have a nice day .';
In principle, String concatenation of HTML code is not allowed. Use the foreground template or background template.
String literal, use 'better ".
Note: Use jsdoc.
Each line should not be too long. After writing a piece of code, use ide to format it.
Add custom variables to the element and use the "data-" prefix in a uniform manner, so that the prefix is connected to the "data-*" mechanism of html5.
Prohibiting jquery from using more than one row of chain operations is very difficult to read.
Select an element. Use the ID selector, class selector, and label selector. Use the sub-element structure pseudo class and position pseudo class with caution (Nth-
Child,: first,: eq,: GT)
Search for nearby element nodes on an existing jquery object. We do not recommend that you use multi-level find instead of related traversal functions.
JavascriptProgramTry to put it in an external JS file for compression and caching.
Standard features are better than non-standard features (if a class library is provided, use the functions in the class library first ).
$ ("XXXX "). find ("YYYY "). find ("ZZZZ"); // ng $ ("XXXX "). next () // or nextuntil, nextall, Prev, prevall, prevuntl, children, closest ,. siblings
When adding events to an element, the order of consideration is delegate> live> bind.
Jquery has a problem in the following events: Change resize mouseenter mouseleave mousewheel. Generally, event proxies are not used. For example, mousewheel events can only be implemented by plug-ins.
Leave no future code snippets in the JS File
Any ID or class name referenced by jquery should use JS _ as the prefix to warn others to accidentally delete it when adjusting the style.
JavaScript code must be placed in the $ namespace object. The execution of all functions starts from the main function.
; $ (Function (){//... other variables used: var $ = Window. $ = {// helper function 1 _ Functions 1: function () {}, // helper function 2 _ functions 2: function (){}, // third-party auxiliary function 3 _ Functions 3: function () {}, // The third-party auxiliary function 4 _ functions 4: function (){}, // helper function 5 _ functions 5: function (){},//.... more private functions // function 1 feature1: function () {}, // function 2 feature2: function () {}, // function 3 feature3: function () {}, // function 4 feature4: function () {}, // function 5 feature5: function (){ }, // Put the JSON data retrieved from the background to this object, so that other functions call jsons :{},//.... for more requirements, a requirement corresponds to a function main: function () {$ $. feature1 (); $. feature2 (); $. feature3 (); $. feature4 ();//.... call them in the main function. }}$ $. Main ();});
The format for retrieving JSON data from the background is
$. Getjson (URL, Params, function (JSON) {If (JSON & JSON. status = "1") {$. flash (JSON. MSG); $. jsons ["XXXX"] = JSON; // save JSON} else {$. flash (JSON. MSG, "error") }}); $. post (URL, Params, function (JSON) {If (JSON & JSON. status = "1") {$. flash (JSON. MSG); $. jsons ["XXXX"] = JSON;} else {$. flash (JSON. MSG, "error") }}, "JSON ");
To separate requests from execution callbacks, we use the dependby function, which effectively avoids multi-layer nested callbacks and greatly improves code readability.
$. Dependby (["list_configs"], $. jsons, function () {var JSON =$ $. jsons. ist_configs ;//...... other code });
If you have any good suggestions for the time being, let us know.