Tips for writing widgets using jQuery _ jquery

Source: Internet
Author: User
When writing widgets, we usually need to bind some events. It is best to add the binding events of these widgets to the namespace of the current widget. 1. When writing widgets, we usually need to bind some events. It is best to add the binding events of these widgets to the namespace of the current widget. If two widgets are used for the same jQuery object, and both widgets are bound with the same event name, the problem may occur. It is also convenient to remove binding events when destroying Widgets. You only need unbind (". namespace.
2. When writing jQuery, because jQuery objects support continuous writing. For example, watermark (obj).css ("height", "20px"). attr ("title", "abc ")....
3. You can use the native method of javascript to avoid switch during writing.

The Code is as follows:


Switch ()
{
Case "aa ":
This. _ set_aa ();
Break;
Case "bb ":
This. _ set_bb ();
Break;
Case "cc"
This. _ set_cc ();
Break;
}


The above code can be replaced with the following code

The Code is as follows:


This ["_ set _" + a] ();


4. cache jQuery objects and various variables as much as possible. This improves the script performance.
5. cache the this pointer using variables. this can be minimized when code is minimized.
6. It is best to take the css class name with the same name and save the definition variable. When using the variable directly, even if the css name is adjusted, you only need to change the value of the variable cache. At the same time, the Code can also reduce the size when minimized.
7. During setOption, if an option is a complex object instead of a simple value object, it is best not to simply this. options [key] = value. before that, you need to make an extend between the value and the previous option value, and then assign a value to keep the original part of the complex object. For example:

The Code is 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}, and the height: 200 will be lost. The following one will keep the original height: 200.
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.