JavaScript team programming specifications

Source: Internet
Author: User

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 the efficiency of the continuous declaration is not as high as that of the separate declaration, and it is easy to mistakenly expose it to the global scope.

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 classes can only be used to build the UI library, and the Business Code is not allowed to define classes without permission.

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, you are not allowed to use the name function expression. Write the following code in the first line of the target function. For details, refer to google 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 = "www.bkjia.com" jump = "bbb" onclick = "goto (this. jump)"> helping customers </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 "\" may produce odd errors; 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 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 existing jQuery objects. We do not recommend that you use multi-level find instead of related traversal functions.

JavaScript programs should be stored in external JS files as much as possible 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.

Do not leave a code snippet In the JS file that will not be used in the future.

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.

Related Article

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.