I have this code:
function A() {
this.name = 'John';
}
A.prototype.getName = function() {
return this.name;
}
function B() {
}
B.prototype.callApi = function(cb) {
var context = arguments[0];
setTimeout(function() {
console.log('server data combine with ' + cb());
}, 1000);
}
var a = new A();
console.log(a.getName());
var b = new B();
b.callApi(a.getName);
I want when getName is executed, the this
variable points to a
object.
The problem is that when the cb
is executed, the this
is not of the a instance
.