【发布时间】:2021-05-31 23:00:07
【问题描述】:
所以我试图在我之外调用一个函数
window.addEventListener('deviceorientation', function (event) {
console.log(event.alpha + ' : ' + event.beta + ' : ' + event.gamma);
this.adjustHeading(event.alpha);
})
我试图调用的函数:
adjustHeading: function (heading) {
this.map.getModel().setViewRotation(heading, false);
}
整个js:
(function ($) {
'use strict';
$.widget("symfony.GpsPosition", {
//lots of code//
listenForDeviceOrientation: function() {
window.addEventListener('deviceorientation', function (event) {
console.log(event.alpha + ' : ' + event.beta + ' : ' + event.gamma);
this.adjustHeading(event.alpha);
})},
adjustHeading: function (heading) {
this.map.getModel().setViewRotation(heading, false);
}
});
}(jQuery));
我的问题是,来自 window.eventListener 的 this.adjustHeading(event.alpha); 调用不起作用,因为在 windows 范围内adjustHeading() 不可用。
有什么方法可以绕过它并访问同一文件中的 JS 属性?
我正在为地图视图使用 smyfony 和 openlayers,如果这有帮助的话。
【问题讨论】:
-
使用arrow function 作为事件监听器。
标签: javascript jquery symfony openlayers device-orientation