1. When writing widgets, we generally need to bind some events. It is best to add these widget binding events to the current widget namespace. If the same jQuery object is used, two widgets are used, and both widgets are bound to the same event name, problems may occur. When destroying widgets, it is also very convenient to remove binding events, just unbind (".namespace") is enough.
2. When writing jQuery, because jQuery objects support sync writing. For example: $(obj).css("height","20px").attr("title","abc")...
3. When writing, you can use the native JavaScript method to avoid switch.
switch(a)
{
case "aa":
this._set_aa();
break;
case "bb":
this._set_bb();
break;
case "cc"
this._set_cc();
break;
}
The above code can be replaced by the following code
this["_set_"+a]();
4. Try to cache jQuery objects and various variables. This can improve script performance
5. Use variables to cache this pointer, and this can be minimized when minimizing the code.
6. It is best to give the css class name a consistent name and then define the variable to save it. When using it, you can use variables 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 volume when it is minimized.
7. When settingOption, if an option is a complex object, not a simple value object, it is best not to simply [key]=value. Before this, you need to extend the value and the previous option value, and then assign the value, so that the original part of the complex object can be retained. for example:
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}, which will lose height:200; while the one below will continue to retain the original height:200.
2. When writing jQuery, because jQuery objects support sync writing. For example: $(obj).css("height","20px").attr("title","abc")...
3. When writing, you can use the native JavaScript method to avoid switch.
Copy the codeThe code is as follows:
switch(a)
{
case "aa":
this._set_aa();
break;
case "bb":
this._set_bb();
break;
case "cc"
this._set_cc();
break;
}
The above code can be replaced by the following code
Copy the codeThe code is as follows:
this["_set_"+a]();
4. Try to cache jQuery objects and various variables. This can improve script performance
5. Use variables to cache this pointer, and this can be minimized when minimizing the code.
6. It is best to give the css class name a consistent name and then define the variable to save it. When using it, you can use variables 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 volume when it is minimized.
7. When settingOption, if an option is a complex object, not a simple value object, it is best not to simply [key]=value. Before this, you need to extend the value and the previous option value, and then assign the value, so that the original part of the complex object can be retained. for example:
Copy the 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}, which will lose height:200; while the one below will continue to retain the original height:200.