I have created a JavaScript class, but I am encountering issues with making an Ajax request in the constructor function. I want the response from the Ajax request to populate my object's attributes, but it is not working as expected. Can someone help me identify what I'm doing wrong?
class Question {
constructor(questions = [], answers = [], challenges = [], value = 100, position = 0){
this.questions = questions;
this.answers = answers;
this.challenges = challenges;
this.position = position;
this.value = value;
jQuery.ajax({
url: '../php/consult.php' + location.search,
type: "GET",
dataType: 'json',
success: function(question, challenge){
this.questions = question.question;
this.answers = question.answer;
this.challenges = challenge.description;
}
});
}
}
const question = new Question();
console.log(question);