我使用纯JavaScript创建此类:

var SelectFeature = /*@__PURE__*/(function (Select) {
    function SelectFeature() {
        Select.call(this, {
            condition: ol.events.condition.click
        });
    }

    this.on('select', function (e) {
        //some logic
    });

    if (Select) SelectFeature.__proto__ = Select;
    SelectFeature.prototype = Object.create(Select && Select.prototype);
    SelectFeature.prototype.constructor = Select;

    return SelectFeature;
}(ol.interaction.Select));


如您所见,我将ol.interaction.Select作为参数传递给类,并使用SelectFeature中的方法Select.call()作为构造函数。

这是ol.interaction.Select类的描述。

ol.interaction.Select类具有一个称为on()的成员。我在上面的示例中尝试访问此方法:

this.on('select', function (e) {
        //some logic
})


但是我得到这个错误:

Uncaught TypeError: this.on is not a function


我的问题是如何访问ol.interaction.Select类的成员?

最佳答案

thisSelectFeature函数之外没有定义。
因此,您需要在this.on函数内调用SelectFeature
为此,您需要在on函数内设置SelectFeature函数:

var SelectFeature = /*@__PURE__*/(function (Select) {
    function SelectFeature() {
        Select.call(this, {
            condition: ol.events.condition.click
        });
        this.on = Select.prototype.on; // This gets Select's on method and gives it to `this`
        this.on('select', function (e) {
           //some logic
         });
    }

    if (Select) SelectFeature.__proto__ = Select;
    SelectFeature.prototype = Object.create(Select && Select.prototype);
    SelectFeature.prototype.constructor = Select;

    return SelectFeature;
}(ol.interaction.Select));

09-11 07:02