javascript - What is the difference between these two ways of binding custom events? Which one is better?

WBOY
Release: 2016-09-15 11:30:57
Original
1321 people have browsed it

The first way is to store custom events directly in __onfireEvents

<code>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);
}
</code>
Copy after login
Copy after login

The second way is to store custom events, but the difference is that they are bound to elements. Is it necessary? Are there any advantages here?

<code>$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 );
    }
};</code>
Copy after login
Copy after login

Reply content:

The first way is to store custom events directly in __onfireEvents

<code>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);
}
</code>
Copy after login
Copy after login

The second way is to store the custom event, but the difference is that it is bound to the element. Is it necessary? Are there any advantages here?

<code>$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 );
    }
};</code>
Copy after login
Copy after login

It seems that the second one is more readable.

Actually, I think there is essentially no difference between the two, just like the relationship between products and categories. You can say {Product1:[Category1, Category2]} or {Category1: [Product1, Product2]}

My personal feeling is second

Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!