zoukankan      html  css  js  c++  java
  • 监听自定义事件

    window.CusEventTarget = function () {
        this._listener = {};
    };
    
    CusEventTarget.prototype = {
        constructor: this,
        addEvent: function (type, fn) {
            if (typeof type === "string" && typeof fn === "function") {
                if (typeof this._listener[type] === "undefined") {
                    this._listener[type] = [fn];
                } else {
                    this._listener[type].push(fn);
                }
            }
            return this;
        },
        addEvents: function (obj) {
            obj = typeof obj === "object" ? obj : {};
            var type;
            for (type in obj) {
                if (type && typeof obj[type] === "function") {
                    this.addEvent(type, obj[type]);
                }
            }
            return this;
        },
        addMutipleEvents: function (types, fn) {
            types = types instanceof Array ? types : [];
            var type;
            for (var i in types) {
                type = types[i]
                this.addEvent(type, fn);
            }
            return this;
        },
        fireEvent: function (type,data) {
            if (type && this._listener[type]) {
                var events = {
                    type: type,
                    target: this
                };
    
                for (var length = this._listener[type].length, start = 0; start < length; start += 1) {
                    this._listener[type][start].call(this,data, events);
                }
            }
            return this;
        },
        fireEvents: function (array) {
            if (array instanceof Array) {
                for (var i = 0, length = array.length; i < length; i += 1) {
                    this.fireEvent(array[i]);
                }
            }
            return this;
        },
        removeEvent: function (type, key) {
            var listeners = this._listener[type];
            if (listeners instanceof Array) {
                if (typeof key === "function") {
                    for (var i = 0, length = listeners.length; i < length; i += 1) {
                        if (listeners[i] === listener) {
                            listeners.splice(i, 1);
                            break;
                        }
                    }
                } else if (key instanceof Array) {
                    for (var lis = 0, lenkey = key.length; lis < lenkey; lis += 1) {
                        this.removeEvent(type, key[lenkey]);
                    }
                } else {
                    delete this._listener[type];
                }
            }
            return this;
        },
        removeEvents: function (params) {
            if (params instanceof Array) {
                for (var i = 0, length = params.length; i < length; i += 1) {
                    this.removeEvent(params[i]);
                }
            } else if (typeof params === "object") {
                for (var type in params) {
                    this.removeEvent(type, params[type]);
                }
            }
            return this;
        }
    };
    

      

  • 相关阅读:
    Android实战经验之图像处理及特效处理的集锦(总结版)
    Android类似于滚动的通知栏实现
    Python概览
    高效程序员的45个习惯读书笔记
    Web前台传对象字符串到后台并让后台反序列化对象字符串的方法(ASP.NET)
    发布订阅者模式之C#委托实现
    表数据复制(迁移)
    Code Smell
    Python学习过程遇到的Bug不断更新
    Resharper 7小技巧系列:导航、书签、和最近编辑
  • 原文地址:https://www.cnblogs.com/holy-amy/p/9304976.html
Copyright © 2011-2022 走看看