写一个订阅发布模式
class EventEmeitter{
constructor(){
this._events = this._events || new Map();
}
}
EventEmeitter.protype.emit = function (type, ...args) {
write your code
// this._events.get(type)(...args);
}
EventEmeitter.protype.addListener = function (type, fn) {
write your code
// this.events.set(type, fn);
}
EventEmeitter.protype.removeListener = function (type, fn) {
write your code
// this.events.delete(type);
}
const emitter = new EventEmeitter();
emitter.addListener('jswp', name => {
console.log(`wp: ${name}`);
});
emitter.addListener('jswp', name => {
console.log(`wp1: ${name}`);
});
emitter.emit('jswp', 'jswp');
打印结果要求: wp: jswp wp1: jswp
网友评论