var Subscriber = function(){ var subName = arguments[0]; return function(){ console.log("我是"+subName,"我收到了"+arguments[0]); } }; var XM = Subscriber("小明"); var XA = Subscriber("小暗"); var pub_sub = function(){ var subList = []; var pub = { add:function(){ subList.push(arguments[0]); }, remove:function(){ for(var i=0;i<subList.length;i++){ if(arguments[0] == subList[i]){ subList.splice(i,1); i--; } } }, send:function(){ for(var i=0;i<subList.length;i++){ (subList[i])(arguments[0]); } if(i == 0){console.log("我是小明和小暗他爹,他们都失联了")} console.log("--------------------------") } }; return pub; }; var Publisher = pub_sub(); Publisher.add(XM); Publisher.add(XA); Publisher.send("第一条消息"); Publisher.remove(XM); Publisher.send("第二条消息"); Publisher.add(XM); Publisher.send("第三条消息"); Publisher.remove(XA); Publisher.send("第四条消息"); Publisher.add(XM); Publisher.send("第五条消息"); Publisher.remove(XM); Publisher.send("第六条消息");
时间: 2024-10-11 23:04:43