zoukankan      html  css  js  c++  java
  • js事件模型与自定义事件

    JavaScript 一个最简单的事件模型,需要有事件绑定与触发,还有事件删除。

     1 var eventModel = {
     2   list: {},
     3 
     4   bind: function () {
     5     var args = [].slice.call(arguments),
     6     type = args[0],
     7     handlers = args.slice(1);
     8 
     9     if (typeof type === 'string' && handlers.length > 0) {
    10       for (var i = 0; i < handlers.length; i++) {
    11         if (typeof handlers[i] === 'function') {
    12           if (!this.list[type]) {
    13             this.list[type] = [];
    14           }
    15           this.list[type].push(handlers[i]);
    16         }
    17       }
    18     }
    19   },
    20 
    21   unbind: function () {
    22     var type = arguments[0],
    23     handlers = Array.prototype.slice.call(arguments, 1);
    24 
    25     if (typeof type === 'string') {
    26       if (handlers.length === 0) {
    27         this.list[type] = [];
    28       } else {
    29         for (var i = 0; i < handlers.length; i++) {
    30           if (typeof handlers[i] === 'function' && handlers[i] === this.list[type][i]) {
    31             this.list[type].splice(i, 1);
    32           }
    33         }
    34       }
    35     }
    36   },
    37 
    38   trigger: function () {
    39     var arguments = [].slice.call(arguments),
    40     type = arguments[0],
    41     args = arguments[1] instanceof Array && !arguments[2] ? arguments[1] : arguments.slice(1),
    42     handlers = this.list[type];
    43 
    44     for (var i = 0; i < handlers.length; i++) {
    45       handlers[i].apply(this, args.splice(0, handlers[i].length));
    46     }
    47   }
    48 };

    其中主要实现了bind(绑定事件)、unbind(删除事件)与 trigger (触发事件)。对同一事件名称,可以绑定多个事件处理函数;并按照绑定的顺序依次触发。

    args.splice(0, handlers[i].length) 触发时的传参

    事件绑定与触发:

    eventModel.bind('myevent1', function (a) {
      console.log(a);  // 1
    }, function(b) {
      console.log(b);  // 2
    }, function(c, d) {
      console.log(c + ' + ' + d);  // a + b
    });
    
    eventModel.bind('myevent1', function (e) {
      console.log(e);  // 50
    });
    
    eventModel.trigger('myevent1', 1,2,'a','b', 50);

    事件删除:

    <button id="bind">bind</button>
    <button id="unbind">unbind</button>
    var fnX = function () {
      console.log('fnX');
    }
    
    var fnY = function () {
      console.log('fnY');
    }
    
    eventModel.bind('myevent2', fnX, fnY);
    
    document.getElementById('unbind').onclick = function () {
      eventModel.unbind('myevent2', fnX); //删除 fnX 后,只剩下 fnY
    };
    
    document.getElementById('bind').onclick = function () {
      eventModel.trigger('myevent2'); //输出 fnX fnY
      //在点击unbind后,只输出 fnY
    };
  • 相关阅读:
    python!让人惊讶的python
    由测试中的版本同步联想到敏捷开发中的两个实践
    python中比较两个文件是否相同
    python中根据类名生成类的实例
    使用sqlServer开发应用程序时要注意的10件事
    python版的Hello World
    其他语言的.net实现列表
    Bug管理的流程和几个重点
    IL Reference
    Delphi CreateProcess函数调用示例
  • 原文地址:https://www.cnblogs.com/caihg/p/5227139.html
Copyright © 2011-2022 走看看