在下面的javascript中的自定义类中,在回调函数中,为什么this.obj只有局部变量obj有我想要的东西?谢谢。javascript如何在回调函数中获得this.variable
function ClassTest(director) {
this.obj = {"test1": "test1"};
}
function test1(input, callback) {
callback("success");
}
ClassTest.prototype.test = function() {
var obj = this.obj;
test1("niuniu",function(e){
console.log(this.obj); // undefined
console.log(obj); // this one has stuff
});
}
// run
new ClassTest().test()
添加'var that = this;',然后在回调中使用'that'来引用'this' :) –