例如,我有这么一段Java代码:
public class Test1 {
public String readDB(){
return "example";
}
public String displayData(){
return "This is a"+this.readDB();
}
}
然而当我简单转换为Javascript时就出现问题了:
function Test1(){};
Test1.prototype.readDB = function(){
return 'example';
};
Test1.prototype.displayData = function(){
return 'This is a'+this.readDB();
}
这个this
指向的就不是Test1
的实例了,就变成了window
对象了,我应该如何解决这个问题呢?
谢谢
这个
this
指向的是Test1
的实例呀,不知道你为什么说变成了window
对象