FED20. 观察者模式
描述
请补全JavaScript代码,完成"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":[]}