基于单例模式构建发布订阅库

基于单例模式
在这里插入图片描述
设计模式是一种设计思想:用来有效管理代码的思想。经典设计模式:发布订阅

    <style>
        #box{
            position:relative;
            left:0;
            top:0;
            width:100px;
            height:100px;
            background-color: hotpink;
        }
    </style>
</head>
<body>
    <div id="box"></div>
    <script>
        //subscribe->sub
        let sub=(function (){
            let pond={};
            const on=function(type,func){
                //验证当前事件池中是否存在这个自定义事件:不存在则新增一个
                !pond.hasOwnProperty(type)?pond[type]=[]:null;
                let arr=pond[type];
                //重复验证 或者使用 if(arr.includes(func)) return;
                for(let i=0;i<arr.length;i++){
                    let item=arr[i];
                    if(item===func){
                        return;
                    }
                }
                arr.push(func);
            };
            const off=function(type,func){
                let arr=pond[type] || [];
                for(let i=0;i<arr.length;i++){
                    if(arr[i]===func){
                        //想要移除 arr.splice(i,1);
                        //防止数组塌陷,实现当前项的假删除
                        arr[i]=null;
                        break;
                    }
                }
                //或者是 arr.filter(item => item!==func);
            };
            const fire=function(type,...params){
                let arr=pond[type]||[];
                for(let i=0;i<arr.length;i++){
                    let item=arr[i];
                    if(typeof item==="function"){
                        item(...params);
                        continue;
                    }
                    //如果当前项是null把他删除
                    arr.splice(i,1);
                    i--;
                }
                //或者是 arr.forEach(item=>item(...params));
            };
            //暴露API
            return{
            on,
            off,
            fire
        }
        })();
        let box=document.querySelector('#box');
        box.onclick=function(ev){
            sub.fire('@A',10,20,ev);
        }
       function fn1(x,y,ev){
           console.log(1,x,y,ev);
       }
       sub.on('@A',fn1);
       function fn2(){
           console.log(2);
           sub.off('@A',fn1);
           sub.off('@A',fn2);
       }
       sub.on('@A',fn2);
       function fn3(){
           console.log(3);
       }
       sub.on('@A',fn3);
       function fn4(){
           console.log(4);
       }
       sub.on('@A',fn4);
       function fn5(){
           console.log(5);
       }
       sub.on('@A',fn5);
    </script>
</body>
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值