Skip to content

Qbian61/Event

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

2 Commits
 
 
 
 
 
 

Repository files navigation

Event

观察者模式

不超过 25 行的观察者模式

window.Event = (function() {
    function Event() {
        this._events = {};
    }
    Event.prototype = {
        emit: function(name, obj) {
            if(name in this._events) {
                for(var i = 0, len = this._events[name].length; i < len; ++ i) {
                    this._events[name][i](obj);
                }
            }
        },
        on: function(name, callback) {
            if(!(name in this._events)) this._events[name] = [];
            this._events[name].push(callback);
        },
        remove: function(name, callback) {
            this._events[name] ? this._events[name].splice(this._events[name].indexOf(callback), 1) : void 0;
        },
        removeAll: function(name) {
            this._events[name] = [];
        }
    };
    return Event;
})();

About

观察者模式

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published