JS do not repeat detection browser to improve efficiency _ application skills

Source: Internet
Author: User
Here to take an example of adding events
Copy Code code as follows:

Mode 1
function Addevent (EL, type, fn) {
if (El.addeventlistener) {
El.addeventlistener (Type, FN, false);
}else{
El.attachevent (' On ' +type, FN)
}
}

Mode 2
var addevent = Document.addeventlistener?
Function (el, type, fn) {El.addeventlistener (type, FN, false);}:
Function (el, type, fn) {el.attachevent (' on ' +type, FN)};


Mode 1 in the function to make judgments, every time to add events to the element need to be judged once, the efficiency is relatively low.
JQuery 1.6.1/prototype 1.7/mootools 1.3/tangram 1.3.6/reg.js/right.js are all used in this way.

Mode 2 uses two anonymous functions, which are only judged once during initialization, and are not required to be judged every time it is invoked. Efficiency is relatively high.
Ext/kissy/qwrap uses this branch of writing.

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.