Javascript assigning the return value of a Callback function to global variable

后端 未结 5 1354
北海茫月
北海茫月 2020-12-28 19:01

My question is about Javascript. I have a Callback function which receives a Position object on a successful callback.

The problem is that when I try to set the prop

5条回答
  •  旧巷少年郎
    2020-12-28 19:28

    You can implement a helper method like flowing:

    var LocationHelper = function() {};
    
    LocationHelper.prototype.getLocation = function(callback) {
    
        navigator.geolocation.getCurrentPosition(onSuccess, onError);
    
        function onSuccess(pos) {
            callback({
                pos: pos
            });
        }
    
        function onError(message) {
            callback({
                message: message
            });
        }
    
      };
    

提交回复
热议问题