//实现事件的发布-订阅
function Events() {
this.events = {};
}
Events.prototype = {
constructor: Events,
on: function(eventType, callback) {
if (!this.events[eventType]) {
this.events[eventType] = [];
}
this.events[eventType].push(callback);
return this;
},
remove: function(eventType) {
for (var key in this.events) {
if (this.events.hasOwnProperty(key) && key === eventType) {
delete this.events[eventType];
}
}
},
emit: function(eventType) {
if (!this.events[eventType]) {
return this;
}
var args = Array.prototype.slice.call(arguments, 1);
for (var i = 0; i < this.events[eventType].length; i++) {
this.events[eventType][i].apply(this, args);
}
return this;
}
}
console