Five practical tips _javascript skills in JavaScript programming development

Source: Internet
Author: User
It's really five quick tips:

use submit events only on <form> elements
If you want to bind an event handler in a form, you should only bind the submit event on the <form> element, rather than the Submit button for the Click event.
March: That's a good way to do it, but when the company develops using Web flow, one page is a big form, and there may be several submit buttons inside, you have to bind some of the event handlers to the click event of the Submit button.

clickable should be the link
Do not bind the Click event to elements other than the anchor element (<a>). This is important for keyboard users because they are having difficulty getting element focus only through the keyboard.
March: But personally feel the anchor element should be used only as a link, and some functional operations (such as Google Reader Mark all as new), preferably with <span> to mark, Accessibility problems can be solved by shortcut keys, and so on. Doing so can better restore the semantics of HTML elements.

Simple for loop optimization
When you write a for loop, there is a simple technique to improve performance.
Copy Code code as follows:
for (var i = 0; i < elements.length; ++i)

Use the following statement instead of the above:
Copy Code code as follows:
for (var i = 0, j = elements.length I < J; ++i)

This allows you to store the number of elements (Elements.length values) in a variable J so that you do not have to compute the number of elements in each loop.

use anonymous functions as event handlers
Especially for short functions, creating an anonymous function is more readable than a reference to using a named function.
Copy Code code as follows:
Anchor.onclick = function () {map.gotoposition (home); return false;}

March: It is more efficient to use named functions in more complex javascript development.

use Array.join instead of string concatenation (concatenating strings)
When you concatenate many strings, variables, and so on into a long string, put all the strings and variables into an array, and then use the Join method to make them into a long string, which is better than the string connection in terms of both readability and performance of the code.
Copy Code code 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.