4

Let's say I have a basic dumb javascript class :

var FunctionX = function(configs) {
this.funcConfigs = configs;
}

FunctionX.prototype.getData = function() {
  return $.get('/url');
}

FunctionX.prototype.show = function(promise) {
  console.log(this.funcConfigs); // <-- this here is the promise itself, I'm looking to get the instance's configs
}

FunctionX.prototype.setup = function() {
  this.GetData().then(show);
}

var f = new FunctionX({ "a": "b" });
f.setup();

Now I'm trying here in the show function to access the instance variable "funcConfig". "This" is the promise, and "funcConfigs" directly returns undefined.

I tried to resolve this issue with a .resolveWith(this) but it does not solve this issue.

How can I access the instances variables in this scope context?

4
  • Nothing to do with promises, only this in functions invoked with different contexts. Commented Dec 3, 2013 at 22:19
  • I put back in the jquery-deferred tag for use of resolveWith, which tries to approach the problem from the opposite way (e.g. specifies a context, doesn't bind to an existing context). If that "does not solve the issue" then this is already wrong in the context resolveWith is used. Commented Dec 3, 2013 at 22:26
  • Does that code actually run? I could understand this.getData().then(this.show); (though you may still have the same issue). Commented Dec 4, 2013 at 5:36

1 Answer 1

7

In agreement with user2864740, the issue is most likely caused because this is not what you expect it to be when show is invoked as a callback. To make this work properly, you need to capture the proper this in a closure (e.g. var that = this;), and invoke it explicitly.

In other words...

FunctionX.prototype.setup = function() {
   var that = this;

   this.getData().then(function () {
      that.show();
   });
}

EDIT: For a slightly cleaner syntax (using underscore.js):

FunctionX.prototype.setup = function() {
   var that = this;

   this.getData().then(_.bind(this.show, this));
}
Sign up to request clarification or add additional context in comments.

6 Comments

Yep but then it defeats completely the goal of the use of promises which is to chain to make the code cleaner.
Hmm... I'm not sure I agree with you about defeating the purpose of promises. We're still using the callback nature of promises to chain functions together, we're just chaining a new function that wraps the existing function. If it's just a matter of cleaner syntax, see my edit.
Where did underscore suddenly come from?
@Beetroot-Beetroot it comes from underscorejs (underscorejs.org/#bind). The idea is nice but I can't use UnderscoreJS. Fortunately enough, the native js function bind was enough to solve the problem via this.getData().then(this.show.bind(this));.
@Beetroot-Beetroot Fair question, I prefer the original syntax, it's not worth pulling in underscore.js just for this purpose, but useful if you're already using it for other things.
|

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.