The value of this
is determined by how you call the function that contains it. You have no control over how the setup
function is called, since it is called inside the q.await
function.
When you do q.await(this.setup)
, you pass a reference to the function setup, but "lose" any connection to your namespace. You need to bind the function to the namespace for it to have the proper this
value:
q.await(this.setup.bind(this));
Even better, since all your functions work only if this
is the namespace, you should bind them to the namespace like this:
this.start = function() {
// body
}.bind(this);
// usage:
q.await(this.setup);
And now wherever you pass them, they will have the correct this
inside.
Note: in this situation, bind
works something like this:
this.start = function() {
var namespace = this;
q.await(function(){ namespace.setup(); });
}