第一種方式,直接將自定義事件存放在__onfireEvents中var __onfireEvents = {};function _bind(eventName, callback, is_one, context) {
if (typeof eventName !== string_str || typeof callback !== function_str) {
throw new Error('args: '+string_str+', '+function_str+'');
} if (! hasOwnKey(__onfireEvents, eventName)) {
__onfireEvents[eventName] = {};
}
__onfireEvents[eventName][++__cnt] = [callback, is_one, context]; return [eventName, __cnt];
}function on(eventName, callback, context) { return _bind(eventName, callback, 0, context);
}第二種方式,同樣是將自定義事件存儲起來,不同之處是綁定在元素上,是否有必要?這里有什么優勢嗎?$customSubMap = {};
subscribeEvent = function ( $collection, event_name, fn ) {
$collection.on( event_name, fn );
if ( ! $customSubMap[ event_name ] ) {
$customSubMap[ event_name ] = $collection;
} else {
$customSubMap[ event_name ]
= $customSubMap[ event_name ].add( $collection );
}
};
這兩種自定義事件的綁定方式有什么區別?哪種好?
汪汪一只貓
2018-09-07 14:09:43