初始化后如何访问class的成员?

How to access the member of the class after initialization?

我创建这个 class 使用纯 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 作为参数传递给 class 并使用 Select.call() SelectFeature 中的方法作为构造函数。

这里是ol.interaction.Selectclass的描述。

ol.interaction.Select class 有一个叫 on() 的成员。我尝试在上面的示例中像这样访问此方法:

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

但是我得到这个错误:

Uncaught TypeError: this.on is not a function

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

thisSelectFeature 函数之外没有定义。 所以你需要在SelectFeature函数里面调用this.on。 为此,您需要在 SelectFeature 函数 中设置 on 函数

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));