0

Why forgets javascript within a class, after calling a ajax-response function the class name? In the following code the "This text is never shown" is never shown:

var obj = {

test3: function() {
    alert('This text is never shown.');
},
test2: function(responsetext) {
    alert('OK '+responsetext);
    this.test3();  // except when "this" is repleced by "obj"
},
ajax: function(responsefunction) {
    let xhr = new XMLHttpRequest();
    xhr.onreadystatechange = function(){
        if (xhr.readyState === 4) {
            if (xhr.status === 200) {
                responsefunction(xhr.responseText); 
            } else {
                alert("Unexpected result: "+xhr.status);
            }
        }
    }
    xhr.open('POST',document.URL,true);
    xhr.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded');
    xhr.send('NAME=A');
},
test:function(){
    this.ajax(this.test2);
}

}; obj.test();

0 Answers0