美文网首页
interview question

interview question

作者: 一土二月鸟 | 来源:发表于2020-06-11 16:48 被阅读0次

    写一个订阅发布模式

    • 按照输出结果补全代码
    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
    

    相关文章

      网友评论

          本文标题:interview question

          本文链接:https://www.haomeiwen.com/subject/uinbtktx.html