평론-구독 게시 모드

3841 단어
function Wechat(){
        this.state = [];
        
        //     
        this.subscribe = function(obj){
            this.state.push(obj)
        };
        
        //     
        this.publish = function(){
            this.state.forEach(function(v){
                v.natice();
            })
        };
        
        //     
        this.unsubcribe = function(obj){
            this.state.forEach((items,index)=>{
                if(items === obj){
                    this.state.splice(index,1)
                }
            })
        };
    }
    
    var zhangsan = {
        'name':' ',
        natice:function(){
            console.log(this.name+' ')
        }
    }
    
    var lisi = {
        'name':' ',
        natice:function(){
            console.log(this.name+' ')
        }
    }
    
    var WechatPublish = new Wechat();
    //     
    WechatPublish.subscribe(zhangsan)
    WechatPublish.subscribe(lisi)
    
    //     2     
    setInterval(function(){
        WechatPublish.publish()
        console.log(WechatPublish.state)
    },2000)
    
    //    6 , 
    setTimeout(function(){
        WechatPublish.unsubcribe(zhangsan)
        console.log(WechatPublish.state)
    },6000)

 
다음으로 전송:https://www.cnblogs.com/web-zqk/p/8654205.html

좋은 웹페이지 즐겨찾기