【发布时间】:2017-05-01 02:49:02
【问题描述】:
我在 Javascript 中创建了一个类,但我希望在我的 constructor 中创建一个 Ajax 请求,并且返回到我的对象属性,但它不起作用。我哪里错了?
class pergunta {
constructor(perguntas = [], respostas = [], desafios = [], valor = 100, posicao = 0){
this.perguntas = perguntas;
this.respostas = respostas;
this.desafios = desafios;
this.posicao = posicao;
this.valor = valor;
jQuery.ajax({
url: '../php/consulta.php' + location.search,
type: "GET",
dataType: 'json',
success: function(pergunta, desafio){
this.perguntas = pergunta.pergunta;
this.respostas = pergunta.resposta;
this.desafios = desafio.descricao;
}
});
}
}
const perguntas = new pergunta();
console.log(perguntas);
【问题讨论】:
-
检查
this在你的匿名函数中指的是什么。
标签: javascript json ajax rest oop