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?

1

1 Answer 1

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();
   });
};
Sign up to request clarification or add additional context in comments.

2 Comments

I shouldn't feel like this is an accomplishment @p.s.w.g. ... but I'm interpreting it as one.
self is keyword in javascript and refers to window object.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.