我使用纯 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 类的成员?
最佳答案
this
在 SelectFeature
函数之外没有定义。
因此,您需要在 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));
关于javascript - 初始化后如何访问类的成员?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/60893894/