实现一个监听器

66 阅读1分钟
function dispacher(){
    this.callbacks = {};
}
dispacher.prototype.on = function(type,call){
    if (typeof call != 'function') return;
    this.callbacks[type] = call;
}
dispacher.prototype.emit = function(type){
    this.callbacks[type]();
}

var d = new dispacher();
var callback = function () {
    console.log("触发click方法");
}

 d.on("click",callback);
 d.emit("click");