发布订阅

核心类

var Center=function(){

this.beNoticeList=[];

}

Center.prototype.add=function(obj){

this.beNoticeList.push(obj);

}

Center.prototype.notice=function(){

for(var item in this.beNoticeList)
{

this.beNoticeList[item].update();

}

}

var user1={update:function(){

console.log("this is user1")

}}

var user2={update:function(){

console.log("this is user2")

}}

var  dep=new Center();

dep.add(user1);

dep.add(user2);

dep.update();

c

onsole.log("this is user1")

原文地址:https://www.cnblogs.com/gaojian0927/p/9552895.html