JavaScript's team programming specification

Source: Internet
Author: User

This specification is designed for JavaScript functional programming styles and the fact that the company relies heavily on jquery for coding.

Eval,with and caller (use strict requirements for ecma262 V5) are prohibited. Eval only allows machine generation at encryption time.

The declaration variable must be prefixed with the VAR keyword, except in the for (;;) A continuous declaration is not allowed in principle in statements such as loops. Because the efficiency of the continuous declaration is not as high as the separate declaration, and it is easy for the hand to be exposed to the global scope.

Constant, all uppercase. Xingcheng Fayon Gift

Variable name, can not use pinyin, English words in the hump style organized together.

semicolons, which can be added (basically except for, function, if, switch, try, and while), prevent the compression failure due to this problem.

Custom classes can only be used for the building of the UI library, and business code is not allowed to define classes privately.

In principle, a pseudo-object (String, Number,boolean) is not allowed, directly using its literal.

In principle, array and object are not allowed, directly using their literal [], {}.

This is handled with caution, preventing the binding from failing, pointing to window, and recommending that reference to it.

To invoke the function itself, prohibit the use of a named function expression, in the first line of the target function to write the following code implementation, in detail please google "named function expression Disclosure":

var self = Arguments.callee;

It is forbidden to extend the prototype of native objects, especially object.prototype.

Prevents binding an event on a page to an element, that is, the following code style:

<a href= "www.nowamagic.net" jump= "BBB" onclick= "Goto (this.jump)" > Concise Modern Magic </a>

Prohibit the use of IE conditional comments, a compression will not be.

var f = function () {/    * @cc_on if (@_jscript) {return @*/  3;/*@} @*/};

Block function declaration is forbidden, in detail please google "named function expression Disclosure".

if (x) {//ng  function foo () {}} if (x) {  var foo = function () {}}

For-in loops can only be used with object.

It is forbidden to use multi-line strings, because at compile time, whitespace characters at the start of a line cannot be ignored; "\" After the white space character will produce a strange error; While most scripting engines support this notation, it is not a standard specification for ECMAScript.

var myString = ' A rather long string of Chinese text, an error message                 actually this 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.  A nice day. ';

In principle, strings are not allowed to be spliced with HTML code, please use foreground or background templates.

string literal, using ' better than '.

Note, use JSDoc.

Each line should not be too long, after writing a piece of code, please use the IDE to format it.

Add custom variables for the elements, uniformly using the "data-" prefix, and the HTML5 with the "data-*" mechanism.

It is very difficult to read if jquery is forbidden to use more than one line of chained operations.

Element selection, multi-use ID selector, class selector, tag Selector, prudent use of child element structure pseudo-class and position Pseudo-class (NTH-CHILD,:FIRST,:EQ,:GT)

Searching for nearby element nodes in existing jquery objects is not recommended by using the multi-level find search, but using the related traversal functions.

JavaScript programs should be placed in an external JS file in order to compress and cache.

Standard features are better than nonstandard features (if the class library is provided, the functions in the class library are preferred).

$ ("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 with the following event change resize MouseEnter MouseLeave MouseWheel, in general, can not use the event proxy, like the MouseWheel event only with the help of plug-ins.

Do not leave the JS file in the future to determine the code snippet no longer used.

Any ID or class name referenced by jquery should be prefixed with js_, alerting others to mistakenly deleting it when adjusting the style.

The JS code must be placed within the $$ namespace object, and all functions are executed starting with the main function.

     ;;; $ (function () {//... Other variables used in var $$ = window.$$ = {//This page is a private auxiliary function 1 _assist1:function () {},//This page is privately supported          Helper function 2 _assist2:function () {},///This page is a private auxiliary function 3 _assist3:function () {}, This page is private auxiliary function 4 _assist4:function () {},///This page private auxiliary function 5 _assist5:function () {} ,//.... More private functions//Functions 1 Feature1:function () {},//function 2 feature2:function () {}          ,//Function 3 Feature3:function () {},//function 4 feature4:function () {}, Function 5 feature5:function () {},//The JSON data obtained from the background is uniformly put into this object so that other functions call jsons:{},//....            More requirements, one requirement corresponds to a function main:function () {$$.feature1 ();            $$.feature2 ();            $$.feature3 (); $$.feature4 ();//....          They are called in the main primary function.      }} $$.main (); });

Get JSON data from the background in a uniform format

$.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");

In order to achieve the separation between the request and the execution callback, we use the Dependby function, which effectively avoids multi-layered nested callbacks, which greatly improves the understanding of the code.

$.dependby (["List_configs"],$$.jsons,function () {     var json = $$.jsons.ist_configs;     

JavaScript's team programming specification

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.