如何使用"this"调用事件内部的函数



如何在该事件内部调用函数?

myfunction() {
alert('bla bla!');
}
this.map.on('moveend', function() {
console.log('moveend :: ' + this.getCenter());
this.myfunction(); // => ERROR TypeError: this.myfunction is not a function
});

尝试以下

this.map.on('moveend', () => {
console.log('moveend :: ' + this.getCenter());
this.myfunction();
});

这应该行得通。

注意:您在这里面临的问题是范围。我建议你读一读,这是一个很有价值的概念。

最新更新