Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/413.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
JavaScript OOP';这';范围不起作用_Javascript_Oop - Fatal编程技术网

JavaScript OOP';这';范围不起作用

JavaScript OOP';这';范围不起作用,javascript,oop,Javascript,Oop,地图类别: function Map() { this.map; this.userLatitude = 0; this.userLongitude = 0; this.startPositionReporting(); } Map.prototype.getUserPosition = function () { alert(this.userLatitude); return { latitude: this.userL

地图类别:

function Map() {
    this.map;
    this.userLatitude = 0;
    this.userLongitude = 0;

    this.startPositionReporting();
}

Map.prototype.getUserPosition = function () {
    alert(this.userLatitude);

    return { 
        latitude: this.userLatitude,
        longitude: this.userLongitude
    };
};

Map.prototype.startPositionReporting = function () {
    var geolocationOptions = {
        enableHighAccuracy : true
    };

    function showError(error) {
        switch(error.code) {
            case error.PERMISSION_DENIED:
                alert("User denied the request for Geolocation.");
                break;
            case error.POSITION_UNAVAILABLE:
                alert("Location information is unavailable.");
                break;
            case error.TIMEOUT:
                alert("The request to get user location timed out.");
                break;
            case error.UNKNOWN_ERROR:
                alert("An unknown error occurred.");
                break;
        }
    }

    if (navigator.geolocation) {
        navigator.geolocation.getCurrentPosition(function(position) {
            this.userLatitude = position.coords.latitude;
            this.userLongitude = position.coords.longitude;
        }, null, geolocationOptions);
    }
};
调用对象:

var mapObject = new Map();
console.log(mapObject.getUserPosition()); // Why does this return 0???

我不明白为什么
mapObject.getUserPosition()
返回0。我已经在
startPositionReporting
方法中检查了
this.userLatitude
this.userLatitude
,它们工作正常。这一定与范围有关。。。有什么想法吗?

navigator.geolocation.getCurrentPosition
是一个异步函数。您需要等待它返回,然后这些字段将不是0。

navigator.geolocation.getCurrentPosition
是一个异步函数。您需要等待它返回,然后这些字段将不是0。

是的,这是范围问题

  var that = this;

  if (navigator.geolocation) {
        navigator.geolocation.getCurrentPosition(function(position) {
            that.userLatitude = position.coords.latitude;
            that.userLongitude = position.coords.longitude;
        }, null, geolocationOptions);
    }

在getCurrentPosition中传递的函数将在不同的上下文中调用。这就是为什么需要保存对调用该函数的上下文的引用。

是的,这是范围问题

  var that = this;

  if (navigator.geolocation) {
        navigator.geolocation.getCurrentPosition(function(position) {
            that.userLatitude = position.coords.latitude;
            that.userLongitude = position.coords.longitude;
        }, null, geolocationOptions);
    }
在getCurrentPosition中传递的函数将在不同的上下文中调用。这就是为什么您需要保存对调用该函数的上下文的引用。

您的问题很简单

if (navigator.geolocation) {
    navigator.geolocation.getCurrentPosition(function(position) {
        this.userLatitude = position.coords.latitude;
        this.userLongitude = position.coords.longitude;
    }, null, geolocationOptions);
}
startPositionReporting
方法的这一部分中,您假设在匿名函数中,
this
变量仍然引用映射对象。嗯,没有。您需要显式存储要在变量中使用的范围,然后在匿名函数闭包中使用它,如下所示:

var self = this;

if (navigator.geolocation) {
    navigator.geolocation.getCurrentPosition(function(position) {
        self.userLatitude = position.coords.latitude;
        self.userLongitude = position.coords.longitude;
    }, null, geolocationOptions);
}
希望这有帮助

你的问题很简单

if (navigator.geolocation) {
    navigator.geolocation.getCurrentPosition(function(position) {
        this.userLatitude = position.coords.latitude;
        this.userLongitude = position.coords.longitude;
    }, null, geolocationOptions);
}
startPositionReporting
方法的这一部分中,您假设在匿名函数中,
this
变量仍然引用映射对象。嗯,没有。您需要显式存储要在变量中使用的范围,然后在匿名函数闭包中使用它,如下所示:

var self = this;

if (navigator.geolocation) {
    navigator.geolocation.getCurrentPosition(function(position) {
        self.userLatitude = position.coords.latitude;
        self.userLongitude = position.coords.longitude;
    }, null, geolocationOptions);
}

希望这有帮助

好几个。问题是,
这个
的行为不像你想象的那样

绑定到函数的作用域,并动态解析(在调用函数时)

您的问题是,有一个回调被馈送到
navigator.geolocation.getCurrentPosition
,在该函数中,您在
this
上设置属性,期望
this
成为您的实例

不是

在您的情况下,此
很可能是
窗口

两种解决方案是:

Map.prototype.startPositionReporting = function () {
    var map = this; // instance of Map

    /* ...  */
    navigator.geolocation.getCurrentPosition(function (position) {
        map.userLatitude = position.coords.latitude;
        // ...
    });


好几个。问题是,
这个
的行为不像你想象的那样

绑定到函数的作用域,并动态解析(在调用函数时)

您的问题是,有一个回调被馈送到
navigator.geolocation.getCurrentPosition
,在该函数中,您在
this
上设置属性,期望
this
成为您的实例

不是

在您的情况下,此
很可能是
窗口

两种解决方案是:

Map.prototype.startPositionReporting = function () {
    var map = this; // instance of Map

    /* ...  */
    navigator.geolocation.getCurrentPosition(function (position) {
        map.userLatitude = position.coords.latitude;
        // ...
    });


我确实在等待,我确保我在分配位置后调用了函数。你能发布一个JSFIDLE来演示你的问题吗?我确实在等待,我确保我在分配位置后调用了函数。你能发布一个JSFIDLE来演示你的问题吗?快速思考。。。这看起来像是一个范围问题。放置
var\u this=this
更改为
\u此
。快速思考。。。这看起来像是一个范围问题。放置
var\u this=this在导航器编码之前,并在返回函数中将
更改为
\u此
。干杯,伙计!干得很好,干杯,伙计!工作得很好。