1

$.Comment = function() { this.alertme = "Alert!"; }

$.Comment.prototype.send = function() {

  var self = this;
  $.post(
    self.url,
    {
      'somedata' : self.somedata
    },
    function(data, self) {
      self.callback(data);
    } 
  );

}

$.Comment.prototype.callback = function(data) {
  alert(this.alertme);
}

When I'm calling $.Comment.send() debugger is saying to me that self.callback(data) is not a function

What am I doing wrong?

Thank you

2 Answers 2

2

You don't want to declare self as an argument to the success function. If you remove that declaration, you'll pick up the local variable, which is what you want. E.g.:

$.Comment = function() {
    this.alertme = "Alert!";
}

$.Comment.prototype.send = function() {

    var self = this;
    $.post(
        self.url,
        {
            'somedata' : self.somedata
        },
        function(data) {         // <== removed `self` argument
            self.callback(data); // <== now sees `self` local var
        }
    );

}

$.Comment.prototype.callback = function(data) {
    alert(this.alertme);
}
Sign up to request clarification or add additional context in comments.

Comments

0

You should not declare self as a parameter:

function(data) {
  self.callback(data);
} 

That way self will be a closure and reference the correct object within the function.

Comments

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.