Q:

JavaScript OOP 'this' scope not working

Map class:
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);
    }
};

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

I can't figure out why mapObject.getUserPosition() is returning 0. I have checked this.userLatitude and this.userLongitude inside the startPositionReporting method and they work fine. This must be something to do with scope... any ideas?

A:

Several. The problem is that this doesn't behave the way you think it does.  
this is bound to a function's scope, and is resolved dynamically (at the time the function is called).
Your problem is that you've got a callback fed to navigator.geolocation.getCurrentPosition, where, in that function, you're setting properties on this, expecting this to be your instance.
It's not.
this is most-likely window, in your case.
Two solutions are:
Map.prototype.startPositionReporting = function () {
    var map = this; // instance of Map

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

or
var updateMapCoords = function (position) {
        this.userLatitude = position.coords.latitude;
        // ...
};

Map.prototype.startPositionReporting = function () {
    var map = this; // instance of Map
    var updateMap = updateMapCoords.bind(map);
    // returns a copy of the function with `this` set to the value passed to `bind`
    // ...

    navigator.geolocation.getCurrentPosition(updateMap);
    // ...

