У меня есть объект js, и у меня есть метод, который вызывает другой метод и получает обещание, но изнутри .then() Я не могу получить доступ к функции-члену foo(). Почему я не могу получить доступ к foo() и как я могу получить к нему доступ? Вот мой код:Как вы называете методы объекта из обещания .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' –
Как я могу изменить переменную экземпляра,? –