Tips for writing widgets using jquery

Source: Internet
Author: User

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. CopyCode The Code is as follows: Switch ()
Case "AA ":
This. _ set_aa ();
Case "BB ":
This. _ set_bb ();
Case "cc"
This. _ set_cc ();

The above code can be replaced with the following codeCopy codeThe 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:Copy codeThe 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.

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: 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.