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

标签 javascript typescript ionic-framework leaflet ionic4

如何调用此事件内部的函数?

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

这应该有效。

注意:您遇到的问题是scope 。我建议阅读一些内容,这是一个值得理解的概念。

关于javascript - 如何使用 "this"调用事件内的函数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/61013732/

相关文章:

javascript - Vanilla JavaScript + 多重选择 - 在多项选择中获得相同的值?

php - 如果我的表单使用的是 ajax 提交方法(无需重新加载),我该如何阻止垃圾邮件?

javascript - 如何通过用户输入更改按钮值

css - 如何关闭 Angular 2 中一个属性的 View 封装?

android - ionic 和 cordova - css active 在 Android 设备上不起作用

javascript - Firebase 存在 : onDisconnect make a transaction

typescript - PrimeNG 下拉选择项数据绑定(bind)

reactjs - React Prop 中的索引签名

css - 在 Ionic 框架中右对齐列表项内容

angular - 使用可点击图标切换输入框