I can get access to object's properties if method is called from anonymous function, but I can't do

Posted by Kirzilla on Stack Overflow See other posts from Stack Overflow or by Kirzilla
Published on 2010-05-12T19:05:27Z Indexed on 2010/05/12 19:14 UTC
Read the original article Hit count: 156

Filed under:
|

Hello,

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

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

  var self = this;
  $.post(
    self.url,
    {
      'somedata' : self.somedata
    },
    function(data) {          //using anonymous function to call object's method
      self.callback(data);
    } 
  );

}

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

This code works great when I'm calling $.Comment.send(), but this code won't work...

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

  var self = this;
  $.post(
    self.url,
    {
      'somedata' : self.somedata
    },
    self.callback          //using direct access to method
  );

}

Please, could you explain me why?

Thank you

© Stack Overflow or respective owner

Related posts about JavaScript

Related posts about jQuery