5

This is my code:

function User(){
   this.nickname='nickname';
} 
User.prototype.save=function(){
   dosomething();
};
User.prototype.add=function(){
   navigator.geolocation.getCurrentPosition(function(positon){
        this.save();
   });
};

but this.save() is wrong, I want to call save() in another callback. I find this isn't pointing to the User, how can I call save() correctly?

JJJ
  • 32,902
  • 20
  • 89
  • 102
Arnold
  • 210
  • 1
  • 6
  • 13
  • http://stackoverflow.com/questions/2148451/help-this-is-confusing-me-in-javascript try this tread to understand context of this. It was really helpful. – Anil Namde Dec 07 '13 at 20:01

1 Answers1

5

this inside getCurrentLocation probably isn't what you think it is. Try:

User.prototype.add=function(){
  var self = this;
   navigator.geolocation.getCurrentPosition(function(positon){
        self.save();
   });
};
Bill Criswell
  • 32,161
  • 7
  • 75
  • 66