zoukankan      html  css  js  c++  java
  • 4.js模式-发布-订阅模式

    1. 发布-订阅模式

    var observe = (function(){

          var events = {},

                 listen,

                 trigger,

                 remmove;

         

          listen = function(key,fn){

                 if(!events[key]){

                        events[key] = [];

                 }

                

                 events[key].push(fn);

          };

         

          trigger = function(){

                 var key = Array.prototype.shift.call(arguments);

                 var fns = events[key];

                 if(!fns || fns.length === 0){

                        return false;

                 }

                

                 for(var i=0,fn;fn=fns[i++];){

                        fn.apply(this,arguments);

                 }

          };

         

          remove = function(key,fn){

                 var fns = events[key];

                 if(!fns){

                        return false;

                 }

                 if(!fn){

                        fns && (fns.length=0);

                 }else{

                        for(var l =fns.length -1;l>=0;l--){

                               var _fn = fns[l];

                               if(_fn===fn){

                                      fns.splice(l,1);

                               }

                        }

                 }

          };

         

          return {

                 listen:listen,

                 trigger:trigger,

                 remove:remove

          }

    })();

    • observe.listen('xx',function(price){

          console.log(price);

    });

    observe.trigger('xx',2000);

  • 相关阅读:
    IEEE 802.11p (WAVE,Wireless Access in the Vehicular Environment)
    齐夫定律, Zipf's law,Zipfian distribution
    信息中心网络 ,Information-centric networking, ICN
    Ubuntu 16.04安装QQ国际版图文详细教程
    IP多媒体子系统(IP Multimedia Subsystem,IMS)
    遗传算法
    再见, 软交换!又一个通信时代的落幕
    矩阵的核、特征向量、值域
    IPv4组播通信原理
    APIPA(Automatic Private IP Addressing,自动专用IP寻址)
  • 原文地址:https://www.cnblogs.com/SLchuck/p/4869713.html
Copyright © 2011-2022 走看看