列表

详情


FED19. 发布订阅模式

描述

请补全JavaScript代码,完成"EventEmitter"类实现发布订阅模式。
注意:
1. 同一名称事件可能有多个不同的执行函数
2. 通过"on"函数添加事件
3. 通过"emit"函数触发事件

原站题解

HTML/CSS/JavaScript 解法, 执行用时: 1741ms, 内存消耗: 77808KB, 提交时间: 2021-12-14

{"css":"","js":"","html":"<!DOCTYPE html>\n<html>\n    <head>\n        <meta charset=utf-8>\n    </head>\n    <body>\n    \t\n        <script type=\"text/javascript\">\n            class EventEmitter {\n                // 补全代码\n                constructor () {\n                    this.handler = {};\n                }\n                on(type, fn) {\n                    const arr = this.handler[type];\n                    if (arr) {\n                        arr.push(fn);\n                    } else {\n                        this.handler[type] = [fn];\n                    }\n                }\n                \n                emit(type, ...args) {\n                    this.handler[type] && this.handler[type].forEach(callback => callback(...args))\n                }\n            }\n        </script>\n    </body>\n</html>","libs":[]}

HTML/CSS/JavaScript 解法, 执行用时: 1744ms, 内存消耗: 77772KB, 提交时间: 2022-01-25

{"css":"","js":"","html":"<!DOCTYPE html>\n<html>\n    <head>\n        <meta charset=utf-8>\n    </head>\n    <body>\n    \t\n        <script type=\"text/javascript\">\n            class EventEmitter {\n                // 补全代码\n                constructor() {\n                    this.events = {}\n                }\n                on(event, fn) {\n                    if(!this.events[event]) {\n                        this.events[event] = [fn]\n                    } else {\n                        this.events[event].push(fn)\n                    }\n                }\n                emit(event){\n                    this.events[event].forEach(cb => {\n                        cb();\n                    })\n                }\n            }\n        </script>\n    </body>\n</html>","libs":[]}

HTML/CSS/JavaScript 解法, 执行用时: 1746ms, 内存消耗: 77820KB, 提交时间: 2021-12-14

{"css":"","js":"","html":"<!DOCTYPE html>\n<html>\n    <head>\n        <meta charset=utf-8>\n    </head>\n    <body>\n    \t\n        <script type=\"text/javascript\">\n            class EventEmitter {\n                // 补全代码\n                constructor() {\n                    this.events = {}\n                }\n                on(event, fn) {\n                    if(!this.events[event]) {\n                        this.events[event] = [fn]\n                    } else {\n                        this.events[event].push(fn)\n                    }\n                }\n                emit(event) {\n                    if(this.events[event]) {\n                        this.events[event].forEach(item => item())\n                    }\n                }\n            }\n        </script>\n    </body>\n</html>","libs":[]}

HTML/CSS/JavaScript 解法, 执行用时: 1747ms, 内存消耗: 77824KB, 提交时间: 2022-01-05

{"css":"","js":"","html":"<!DOCTYPE html>\n<html>\n    <head>\n        <meta charset=utf-8>\n    </head>\n    <body>\n    \t\n        <script type=\"text/javascript\">\n            class EventEmitter {\n                // 补全代码\n                constructor(){\n                    this.events={}\n                }\n                on(event,fn){\n                    if(!this.events[event])\n                        this.events[event]=[fn]\n                    else\n                        this.events[event].push(fn)\n                }\n                emit(event){\n                    if(this.events[event])\n                        this.events[event].forEach(callback=>callback())\n                }\n            }\n        </script>\n    </body>\n</html>","libs":[]}

HTML/CSS/JavaScript 解法, 执行用时: 1749ms, 内存消耗: 77832KB, 提交时间: 2022-01-25

{"css":"","js":"","html":"<!DOCTYPE html>\n<html>\n    <head>\n        <meta charset=utf-8>\n    </head>\n    <body>\n    \t\n        <script type=\"text/javascript\">\n            class EventEmitter {\n                // 补全代码\n                constructor(){\n                    this.event={}\n                }\n                \n                on(type, fn){\n                    if(!this.event[type]){\n                        this.event[type]=[]\n                    }\n                    this.event[type].push(fn)\n                }\n                \n                off(type, fn){\n                    let index = this.event[type].findIndex(fn)\n                    if(index!== -1){\n                        this.event[type].splice(index, 1)\n                    }\n                    if(!this.event[type].length){\n                        delete this.event[type]\n                    }\n                    \n                }\n                \n                emit(type){\n                    this.event[type].forEach(fn=>fn())\n                }\n                \n            }\n        </script>\n    </body>\n</html>","libs":[]}

上一题