我有这个类结构(或多或少):
function DataLoader = function () {
this.setup = function () {
this.page = new Page();
this.page.setup();
};
Page = function () {
this.apiSelect = null;
this.onchangelistener = function () {
//---------------- look here ---------------
console.log(this.apiSelect); //Why is this.apiSelect undefined?
//---------------- look here ---------------
};
this.setup = function () {
this.apiSelect = document.getElementById("foo");
//This is also weird to me, I had to do it like this
//to get the functionality I wanted...
document.getElementById("foo").onchange = this.onchangelistener;
};
};
};
var dl = new DataLoader();
dl.setup();
我对 Javascript 很陌生,现在还没有太多的细节,但这对我来说似乎很奇怪。当 onchange 事件触发时,它会调用 onchangelistener。为什么 this.apiSelect 未定义?我的意思是我已经为它增加了价值。
我当前的代码看起来像这样