The 5 points below are really good, improving performance and readability. You can choose to use them as needed. Five quick tips:
Only
Use the submit event on the element
If you want to bind an event handler to formInstead of binding a click event to the submit button.
March: This method is good, but when the company uses Web Flow for development, a page is a large form, and there may be several submit buttons in it, therefore, some event handlers have to be bound to the click Event of the submit button.
Click links.
Do not bind a click event to any element except the anchor element. This is important for keyboard users because they encounter difficulties in getting element focus only through the keyboard.
March: However, I personally think that the anchor element should be used only as a link, and some functional operations (such as Google Reader's Mark all as new) should be used to Mark, you can solve the accessibility problem by using shortcuts and other methods. In this way, the semantics of HTML elements can be better restored.
Simple for loop Optimization
When you write a for loop, there is a simple technique that can improve performance.
The Code is as follows:
For (var I = 0; I <elements. length; ++ I)
Replace the preceding statement with the following statement:
The Code is as follows:
For (var I = 0, j = elements. length; I <j; ++ I)
In this way, you can store the number of elements (the value of elements. length) in a variable j, so that you do not have to calculate the number of elements each time in a loop.
Use anonymous functions as event handlers
Especially for short functions, creating an anonymous function is more readable than referencing a naming function.
The Code is as follows:
Anchor. onclick = function () {map. goToPosition (home); return false ;}
March: it is more efficient to use naming functions in complex JavaScript development.
Use Array. join instead of concatenating strings)
When many strings and variables are connected to a long string, all strings and variables are put into an array and then they are formed into a long string using the join method, in this way, both code readability and performance are better than string connections.
The Code is as follows:
Var text = 'there are '+ elements. length + 'members in the elements array .';
Var text = ['there are ', elements. length, 'Members in the elements array.']. join ('');