我有一个js对象,我有一个方法调用另一个方法并获得一个promise,但是从.then()中我不能访问成员函数foo()。为什么我不能访问foo()以及如何访问它?这里是我的代码:如何从promise中调用object的方法.then()?
function LoadImageGroupFormat() {
return new Promise(function(resolve, reject) {
var xhttp = new XMLHttpRequest();
xhttp.open('GET', "imagegroup_format.txt");
xhttp.onload = function() {
if (xhttp.status == 200) resolve(xhttp.responseText);
else reject(Error(xhttp.statusText));
};
xhttp.onerror = function() {
reject(Error("Network Error"));
};
xhttp.send();
});
}
//the object
var Registerhandler = {
imageGroupIndex: 0,
foo: function() {
//Do something else here
},
GetImageGroupFormat: function() {
var imageGroupIndex = this.imageGroupIndex;
LoadImageGroupFormat().then(function(ImageGroup_Format) {
//Do something with ImageGroup_Format
imageGroupIndex++; //This works
foo(); //Doesn't work - undefined
}, function(error) {
console.error("Failed to Load ImageGroup Format", error);
});
}
}
感谢有这方面的帮助。
'imageGroupIndex ++;' “作品” 因为你犯了一个局部变量'imageGroupIndex',它不是修改'Registerhandler.imageGroupIndex' –
我将如何修改实例的变量? –