zoukankan      html  css  js  c++  java
  • 向JS对象添加和删除事件

    this.removeEventListener = function (obj, ename, func) {
        var store = obj[this.addEventListener.pre + ename];
        if (!store) {
            return;
        }
        var i = store.indexOf(func);
        if (i < 0) {
            return;
        }
        obj[this.addEventListener.pre + ename].splice(i, 1);
    };
    this.addEventListener = function (obj, enames) {
        if (arguments.length < 2) {
            return;
        }
        if (!this.addEventListener.pre) {
            this.addEventListener.pre = "god_" + Math.random() * Number.MAX_VALUE;
        }
        if (typeof arguments[1] == "string") {
            var ename = arguments[1];
            var fullname = "on" + ename;
            if (!(fullname in obj)) {
                var store = this.addEventListener.pre + ename;
                if (!obj[store]) {
                    obj[store] = [];
                }
                Object.defineProperty(obj, fullname, {
                    set: function (v) {
                        obj[store].push(v);
                    }
                });
                var notice = ename === "notice" ? "notice" : "notice" + ename;
                obj[notice] = function () {
                    for (var i = 0; i < obj[store].length; i++) {
                        try {
                            obj[store][i].apply(obj, arguments);
                        } catch (e) {
                            console.log(e.message);
                        }
                    }
                };
            }
        }
        if (typeof arguments[2] === "string") {
            var narguments = [obj];
            for (var i = 2; i < arguments.length; i++) {
                narguments.push(arguments[i]);
            }
            this.addEventListener.apply(this, narguments);
        }
    };
  • 相关阅读:
    code review
    设计原则
    知识点介绍
    REST API
    第四章 模块化React和Redux应用
    第3章 从Flux到Redux
    第二章 设计高质量的React组件
    React和Jquery比较
    第一章 React新的前端思维方式
    封装一个获取module.exports内容的方法
  • 原文地址:https://www.cnblogs.com/ly45/p/6016164.html
Copyright © 2011-2022 走看看