首页 > 解决方案 > 如何将函数添加到对象内的现有函数列表中

问题描述

我正在使用外部库,尤其是LittlePlanet.js

function LittlePlanet (type = 'image', source) {
    this.dragging = false;
    ...
}

LittlePlanet.prototype = Object.assign(Object.create(ImagePanorama.prototype), {
    constructor: LittlePlanet,
    registerMouseEvents: function () {
        this.container.addEventListener('mousedown', this.onMouseDown.bind(this), {passive: true});
        this.container.addEventListener('mousemove', this.onMouseMove.bind(this), {passive: true});
        this.container.addEventListener('mouseup', this.onMouseUp.bind(this), { passive: true});
        ...
    }
});

如何在registerMouseEvents不覆盖预先存在的事件侦听器的情况下向属性添加另一个事件侦听器?

这是我尝试过的:

PANOLENS.LittlePlanet.prototype.registerMouseEvents = function () {
    zoomOut.addEventListener("mousedown", this.onZoom.bind(this), false);
    ...
};

但这会覆盖预先存在的registerMouseEvent事件侦听器。

标签: javascript

解决方案


推荐阅读