【发布时间】:2018-05-06 21:39:44
【问题描述】:
我有这个代码:
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);
我希望在执行 getName 时,this 变量指向 a 对象。
问题是当cb被执行时,this不属于a instance。
【问题讨论】:
标签: javascript callback this