Joe
Joe

Reputation: 23

call methods in callback

I have this Try class:

function Try () {
  console.log('Start')
  this.Something( function() {
    console.log('asd')
    this.Some()
  })
}
Try.prototype.Something = function (callback) {
  console.log('hi all')
  callback()
}
Try.prototype.Some = function () {
  console.log('dsa')
}

But when I try to call the Some method in the callback part, it gives me an error, which says this.Some is not a function. What is the problem? How can i fix this?

Upvotes: 1

Views: 46

Answers (1)

gurvinder372
gurvinder372

Reputation: 68373

scope of this is different inside a different function, even if it is inner function

you need to preserve this of outer function in self and make it

function Try () {
  console.log('Start')
  var self = this;
  self.Something( function() {
    console.log('asd')
    self.Some();
  })
}

Upvotes: 1

Related Questions