jquery writes some tips for widgets to share _jquery

Source: Internet
Author: User
1. When writing widgets, we usually need to bind some events, preferably the binding events of these widgets plus the namespace of the current widget. If the same jquery object uses two widgets, and two widgets are bound to the same event name, there may be a problem. When destroying widgets, it is also convenient to remove the binding event, just Unbind (". Namespace") is all you need.
2, when writing jquery, because the jquery object is to support ligatures. For example: $ (obj). css ("height", "20px"). attr ("title", "ABC") ....
3. When writing, you can use JavaScript's native method to avoid switch.
Copy Code code as follows:

Switch (a)
{
Case "AA":
This._set_aa ();
Break
Case "BB":
THIS._SET_BB ();
Break
Case "CC"
THIS._SET_CC ();
Break
}

The code above can be replaced with the following code
Copy Code code as follows:

this["_set_" +a] ();

4, as far as possible to cache jquery objects, and various variables. This will improve the performance of the script
5, the use of variable caching this pointer, in the minimization of code when this can be minimized.
6, it is best to take the CSS class name consistent name, and then define the variable to save. In the use of the direct variable, so even if the CSS name adjustment, only need to change the value of the variable cache. At the same time, the code can reduce the volume when it is minimized.
7, when setoption, if an option is a complex object, rather than a simple value object, it is best not to simply this.options[key]= Value. And before that, you need to make a extend of value and previous option values, and then assign values so that you can keep a partial value of the original complex object. Like what:
Copy Code code as follows:

var a = {width:120,height:200};
var c={width:200};
A=c;
A=$.extend (A,C);

The result is that the first a will be {width:20}, the height:200 will be lost, and the one below will continue to retain the original height:200.

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.