You can use call()
or apply()
like this:
this.getContactName(id, (error, contactName) => {
if (error) return callback.call(this, error);
// want to access "this" here e.g.,
// this.indexScore = 1
return callback.call(this, contactName);
});
Or with apply()
this.getContactName(id, (error, contactName) => {
if (error) return callback.apply(this, [ error ]);
// want to access "this" here e.g.,
// this.indexScore = 1
return callback.apply(this, [ contactName ]);
});
Both methods bind the first argument as this
value. The difference is, that apply()
has an array of function arguments as a second parameter whereas call()
just has one more argument than the initial function call (the first one is the functions's this
value). See this answer for more information.