I'm working with Javascript "classes", I have a parent "class" with a variable and a function, something like this
function WS() {
this.auth = { client: '', password: '' };
this.Execute = function(url, callback) {
soap.createClient(url, function(err, client) {
if (err) return callback(err);
return callback(null, client);
});
}
}
The "subclass" uses this function and variable, like this
function Stats() {
this.url = 'http://';
this.emailsByDate = function(params, callback) {
this.Execute(this.url, function(err, client) {
if (err) return callback(err);
client.Get(this.auth.concat(params), function(err, results) {
if (err) return callback(err);
return callback(results);
});
});
}
}
Stats.prototype = new WS;
I'm getting through this.Execute() function, but this.auth variable is undefined, why is that
The context in which you are accessing this.auth isn't your Stats function, but instead your anonymous callback function.
You could either save the value of this.auth outside the callback function:
function Stats() {
var auth = this.auth
this.url = 'http://';
this.emailsByDate = function(params, callback) {
this.Execute(this.url, function(err, client) {
if (err) return callback(err);
client.Get(auth.concat(params), function(err, results) {
if (err) return callback(err);
return callback(results);
});
});
}
}
Or you can bind the context of your callback function to that of your Stats function:
function Stats() {
this.url = 'http://';
this.emailsByDate = function(params, callback) {
this.Execute(this.url, function(err, client) {
if (err) return callback(err);
client.Get(this.auth.concat(params), function(err, results) {
if (err) return callback(err);
return callback(results);
});
}.bind(this));
}
}