时间:2021-07-01 10:21:17 帮助过:20人阅读
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 );
}
};
第一种方式,直接将自定义事件存放在__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 );
}
};
看起来第二种可读性更好一点。
其实我觉得两者本质上没区别,就像产品与分类的关系,可以说 {Product1:[Category1, Category2]}也可以说{Category1: [Product1, Product2]}
个人感觉第二