• 观察者模式



    <!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <title>观察模式例子</title> </head> <body> </body> <script type="text/javascript"> var Events = function() { var obj = {}; obj.stack=[]; var listen = function( key, eventfn ) { //事件消息队列 var stack = obj[key] ? obj[key] : (obj[ key ] = []);//1.在obj对象上添加盒子属性;2.用三目运算符解决拓展问题 return stack.push(eventfn); }; var one = function( key, eventfn ) {//添加一个唯一的订阅 remove( key ); return listen( key, eventfn ); }; var remove = function( key ) { return obj[key] ? obj[key].length = 0 : void 0; //取消订阅 }; var trigger = function() { //面试官打电话通知面试者 var fn, stack, key; key = Array.prototype.shift.call( arguments );//获取arguments参数数组的第一个值 stack = obj[key] ? obj[key] : obj[key] = []; for ( var i = 0,len = stack.length; i < len; i++ ) { fn = stack[i]; if (fn) { fn.apply(this,arguments); } } } return { listen: listen, one: one, remove: remove, trigger: trigger } } //订阅者 var adultTv = Events(); adultTv.listen( 'play',(data)=> {return alert("今天是谁的电影:" + data.name)}) //发布者 adultTv.trigger('play',{'name': "刘德华"}); </script> </html>  
  • 相关阅读:
    S02_CH15_ AXI_OLED 实验
    S02_CH14_ EMIO_OLED 实验
    S02_CH13_ AXI_PWM 实验
    S02_CH12_ AXI_Lite 总线详解
    【第二季】CH11_ ZYNQ软硬调试高级技巧
    bzoj3028 食物
    Hdu3579 Hello Kiki
    bzoj4872 [Shoi2017]分手是祝愿
    bzoj4873 [Shoi2017]寿司餐厅
    省选模拟赛 LYK loves rabbits(rabbits)
  • 原文地址:https://www.cnblogs.com/maoBable/p/9721120.html
Copyright © 2020-2023  润新知