You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
functionObserver(){this.handlers={};}Observer.prototype={constructor: Observer,listen: function(event,handler){if(typeofthis.handlers[event]=="undefined"||!this.handlers[event]){console.log("Add new handler type: "+event);this.handlers[event]=[];}this.handlers[event].push(handler);},trigger: function(event,data=null){if(typeofthis.handlers[event]=="undefined"||!this.handlers[event]){console.log("Observer has no such event: "+event);return;}else{for(vari=0;i<this.handlers[event].length;i++){this.handlers[event][i](data);}}},remove: function(event,handler){varhandlers=this.handlers[event];if(Object.prototype.toString.call(handlers)=="[object Array]"){for(vari=0;i<handlers.length;i++){if(handlers[i]===handler){break;}}handlers.splice(i,1);}}};// TestvarMessage=newObserver();Message.listen("ready",function(){console.log("Hello World!");});Message.listen("ready",function(data){if(!data){console.log("Good Night!");}else{console.log("Good Night! "+data);}});Message.trigger("not_ready");Message.trigger("ready","XiaoMing");
一. 单例模式
所谓单例模式,就是为一个类创建唯一的一个实例。
但JS是一种无类语言,只能说创建一个唯一的对象{},这个对象具有独一无二的功能。
比如页面上唯一一个遮罩层mask,就可以这样实现:
闭包实现
桥接模式实现(函数可以作为参数传入)
二. 观察者模式
又称作发布-订阅模式,发布者发布信息事件,而订阅者主动监听信息事件,并执行后续的函数。一般情况下,
发布者和订阅者为同一对象。
观察者模式可以很好的实现模块间的解耦。
代码如下:
参考
常用的Javascript设计模式
The text was updated successfully, but these errors were encountered: