列表

详情


FED20. 观察者模式

描述

请补全JavaScript代码,完成"Observer"、"Observerd"类实现观察者模式。要求如下:
1. 被观察者构造函数需要包含"name"属性和"state"属性且"state"初始值为"走路"
2. 被观察者创建"setObserver"函数用于保存观察者们
3. 被观察者创建"setState"函数用于设置该观察者"state"并且通知所有观察者
4. 观察者创建"update"函数用于被观察者进行消息通知,该函数需要打印(console.log)数据,数据格式为:小明正在走路。其中"小明"为被观察者的"name"属性,"走路"为被观察者的"state"属性
注意:
1. "Observer"为观察者,"Observerd"为被观察者

原站题解

HTML/CSS/JavaScript 解法, 执行用时: 1685ms, 内存消耗: 77816KB, 提交时间: 2022-02-08

{"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            // 补全代码\n            class Observerd {\n                constructor(name) {\n                    this.name = name\n                    this.state = '走路'\n                    this.observers = []\n                }\n                setObserver(observer) {\n                    this.observers.push(observer)\n                }\n                setState(state) {\n                    this.state = state\n                    this.observers.forEach(observer => {\n                        observer.update(this)\n                    })\n                }\n            }\n\n            class Observer {\n                update(observerd) {\n                   console.log(observerd.name+'正在'+observerd.state) \n                }\n            }\n        </script>\n    </body>\n</html>","libs":[]}

HTML/CSS/JavaScript 解法, 执行用时: 1739ms, 内存消耗: 77772KB, 提交时间: 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            // 补全代码\n            class Observerd {\n                constructor(name) {\n                    this.name = name\n                    this.state = '走路'\n                    this.observers = []\n                }\n                setObserver(observer) {\n                    this.observers.push(observer)\n                }\n                setState(state) {\n                    this.state = state\n                    this.observers.forEach(item => item.update(this))\n                }\n            }\n\n            class Observer {\n                constructor() {\n\n                }\n                update(observerd) {\n                    console.log(observerd.name + '正在' + observerd.state)\n                }\n            }\n        </script>\n    </body>\n</html>","libs":[]}

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

{"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            // 补全代码\n            class Observerd {\n                constructor(name) {\n                    this.name = name\n                    this.state = '走路'\n                    this.observers = []\n                }\n                setObserver(observer) {\n                    this.observers.push(observer)\n                }\n                setState(state) {\n                    this.state = state\n                    this.observers.forEach(observer => observer.update(this))\n                }\n            }\n\n            class Observer {\n                constructor() {\n         \n                }\n                update(observerd) {\n                    console.log(observerd.name + '正在' + observerd.state)\n                }\n            }\n        </script>\n    </body>\n</html>","libs":[]}

HTML/CSS/JavaScript 解法, 执行用时: 1744ms, 内存消耗: 77800KB, 提交时间: 2021-12-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            // 补全代码\n            class Observerd {\n                constructor(name) {\n                    this.name = name\n                    this.state = '走路'\n                    this.observers = []\n                }\n                setObserver(observer) {\n                    this.observers.push(observer)\n                }\n                setState(state) {\n                    this.state = state\n                    this.observers.forEach(observer => observer.update(this))\n                }\n            }\n\n            class Observer {\n                constructor() {\n         \n                }\n                update(observerd) {\n                    console.info(observerd,89)\n                    console.log(observerd.name + '正在' + observerd.state)\n                }\n            }\n        </script>\n    </body>\n</html>","libs":[]}

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

{"css":"","js":"","html":"<!DOCTYPE html>\n<html>\n    <head>\n        <meta charset=utf-8>\n    </head>\n    <body>\n        <script type=\"text/javascript\">\n            // 补全代码\n            class Observerd {\n                constructor(name){\n                    this.name = name\n                    this.state = '走路'\n                    this.observer = []\n                }\n                \n                setState(newState){\n                    this.state = newState\n                    this.observer.forEach(item=>item.update(this))\n                }\n                \n                setObserver(newObserver){\n                    this.observer.push(newObserver)\n                }\n            }\n\n            class Observer {\n                \n                update(observerd){\n                    console.log(observerd.name+'正在'+observerd.state)\n                }\n                \n            }\n        </script>\n    </body>\n</html>","libs":[]}

上一题