Polyfill用于attachEvent删除dhtmlx attachEvent函数

时间:2019-09-23 04:49:14

标签: javascript internet-explorer dhtml

我正在为我们的旧产品添加chrome支持(即8支持), 问题是我为attachEvent添加了polyfill

if (!isIE() && Object.attachEvent == null) {
    Object.defineProperty(Object.prototype, 'attachEvent', {
        value: function(event, func) {
            if ('string' !== typeof event || event.indexOf('on') !== 0) {
                return;
            }
                this.addEventListener(event.substring(2), func, false);
        },
        enumerable: false
    });
}

但我们使用的是dhtmlx 3rd party库,该库通过attachEvent函数管理事件, 因此,我的polyfill会覆盖此功能,这使dhtmlx缺少功能。

任何同志如何解决此问题? 我想polyfill attachEvent,但不重写dhtmlx的attachEvent 谢谢!

1 个答案:

答案 0 :(得分:0)

无需检查IsIE() ...,因为只有IE仍会使用非标准的attachEvent

对于其他浏览器,您可以填充EventTarget.prototype.attachEvent-因为无论如何这都是addEventListener的定义

if (EventTarget.prototype.attachEvent == null) {
    Object.defineProperty(EventTarget.prototype, 'attachEvent', {
        value: function(event, func) {
            if ('string' !== typeof event || event.indexOf('on') !== 0) {
                return;
            }
            this.addEventListener(event.substring(2), func, false);
        },
        enumerable: false
    });
}