Two anonymous functions are used, which are only judged once during initialization, and no judgment is required for each subsequent call. The efficiency is relatively high. Here is an example of adding an event
Copy the code code as follows:
// Method 1
function addEvent(el, type, fn){
if(el.addEventListener){
el.addEventListener(type, fn, false);
}else{
el.attachEvent('on'+type, fn)
}
}
// Method 2
var addEvent = document.addEventListener?
function(el, type, fn) {el.addEventListener(type, fn, false);}:
function(el, type, fn) {el.attachEvent('on'+type, fn)};
Method 1 makes the judgment within the function. Every time an event is added to an element, it needs to be judged once, which is relatively inefficient.
jQuery 1.6.1/Prototype 1.7/Mootools 1.3/tangram 1.3.6/reg.js/right.js all use this branching method.
Method 2 uses two anonymous functions, which are only judged once during initialization, and no judgment is required for each subsequent call. The efficiency is relatively high.
Ext/kissy/qwrap uses this branch writing method.