如何修复未定义的二传手?

jauntyCoder

我正在尝试创建一个地理定位类,以便可以在需要的时间和位置调用该类,但是遇到了问题,并不断出错。 Uncaught TypeError: Cannot read property 'setLatitude' of undefined at setCurrentPosition (geolocation.js:13)

任何指导都会得到赞赏吗?

 class Geolocation {
    constructor() {
        this.latitude = 0;
        this.longitude = 0;
    }

    getGeoLocation() {
        if ('geolocation' in navigator) {
            navigator.geolocation.getCurrentPosition(this.setCurrentPosition);
        }
    }
    setCurrentPosition(position) {
        this.setLatitude(position.coords.latitude);
        this.setLongitude(position.coords.longitude);
    }
    setLatitude(latitude) {
        this.latitude = latitude;
    }
    setLongitude(longitude) {
        this.longitude = longitude;
    }

    getLatitude() {
        return this.latitude;
    }

    getLongitude() {
        return this.longitude;
    }
}
阿努拉格·斯里瓦斯塔瓦

您需要绑定到正确的this

navigator.geolocation.getCurrentPosition(this.setCurrentPosition.bind(this))

本文收集自互联网,转载请注明来源。

如有侵权,请联系[email protected] 删除。

编辑于
0

我来说两句

0条评论
登录后参与评论

相关文章