function Observer(){
this.fns=[];
}
Observer.prototype={
subscribe:function(){
this.fns.push(
},
unsubscrible:function(fn){
this.fns=this.fns.filter(
function(e1){
if(e1!==fn){
return e1;
}
}
);
},
update:function(o,thisObj){
var scope=thisObj||window;
this.fns.forEach(
function (e1){
e1.call(scope,o);
}
);
}
};
var o=new Observer;
var f1=function(data){
console.log('Robbin:'+data+',赶紧干活了');
};
var f2=function(data){
console.log('Randall'+data+',找他加点工资去!');
};
o.subscribe(f1);
o.subscribe(f2);
o.update('Tom回来了');
javascript观察者模式
最新推荐文章于 2022-12-09 18:29:45 发布