Five notes during JavaScript development _ javascript skills

Source: Internet
Author: User
Five precautions during JavaScript development 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 ('');

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.