Javascript 在没有实例的运行时编译时,创建函数构造函数会导致未捕获类型错误

Javascript 在没有实例的运行时编译时,创建函数构造函数会导致未捕获类型错误,javascript,jquery,json,ajax,get,Javascript,Jquery,Json,Ajax,Get,因此,我的网站的目标是成为一个简单的问答游戏。 它使用ajax获取一个json问题数组,然后为数组中的每个问题显示实际问题,并创建包含各种选项的按钮,这些选项也存储在question对象中。我似乎也没办法让它发挥作用。 单击按钮时,它应该转到json数组中的下一个问题对象。我正在用一个测验对象来跟踪它。 现在,我在创建对象时出错,而不是在作为实例启动对象时出错 当我试图通过getQuestionhandler函数返回一个新的QuestionHandler对象来创建它时,它在Chrome开发工具中

因此,我的网站的目标是成为一个简单的问答游戏。 它使用ajax获取一个json问题数组,然后为数组中的每个问题显示实际问题,并创建包含各种选项的按钮,这些选项也存储在question对象中。我似乎也没办法让它发挥作用。 单击按钮时,它应该转到json数组中的下一个问题对象。我正在用一个测验对象来跟踪它。 现在,我在创建对象时出错,而不是在作为实例启动对象时出错

当我试图通过getQuestionhandler函数返回一个新的QuestionHandler对象来创建它时,它在Chrome开发工具中给出了一个错误:

Uncaught TypeError: Cannot read property 'undefined' of undefined
at getQuestionHandler (Handlers.js:95)
at new QuizGame (Handlers.js:68)
at QuizIndex.html:37
这是我没有css样式的全部代码:

/*函数GetJsonQuestions(){
变量url=”https://api.myjson.com/bins/10t76x";
$.ajax({
url:url,
数据:“json”,
成功:函数(){
log(“获取成功”);
},
数据类型:数据类型
});
}*/
var urlJson=”https://api.myjson.com/bins/10t76x";
var QuestionHandler=函数(问题、选项、答案){
这个问题=问题;
this.options=选项;
这个答案=答案;
this.isCorrect=函数(猜测){
如果(this.answer==猜测){
返回true;
}
};
};
var PointsHandler=函数(){
该点为0;
this.addPoint=函数(){
这个.points++;
};
};
var getAllData=function(){
var-allData=null;
$.ajax({
url:urlJson,
键入:“GET”,
数据类型:“json”,//添加了数据类型
成功:功能(res){
控制台日志(res);
所有数据=问题;
},
错误:函数(err){
日志(“错误:+err”);
}
});
返回所有数据;
};
var QuizGame=函数(){
该指数=0;
this.allJsonData=getAllData();
this.pointHandler=getPointsHandler();
this.questionHandler=getQuestionHandler();
this.gameResult=getResult();
this.getPoints=函数(){
返回this.pointHandler.points;
};
this.nextQuestion=函数(quess){
索引++;
var correct=questionHandler.isCorrect(猜测);
如果(正确){
pointsHandler.addPoints();
}
};
函数getPointsHandler(){
var handler=new PointsHandler();
返回处理程序;
}
函数getResult(){
返回“您已回答”+此.points+“更正出”+此.index+“问题”;
}
函数getQuestionHandler(){
var qHandler=new QuestionHandler(this.allJsonData[this.index]。问题,this.allJsonData[this.index]。选项,this.allJsonData[this.index]。答案);
return qHandler;//此函数似乎给出了错误
}
};
var myquick=new QuizGame();
log(myquick.allJsonData);
$(“#问题”).text(“问题编号”+myquick.index+”:“+myquick.questionHandler.question);
$.each(myquick.questionHandler.options,函数(i,val){
$(“#选项”)。追加(“+val+”);
});
$(“按钮”)。单击(功能(e){

if(myquick.index这是您的代码的一个功能性和固定的示例。它不会移动到下一个问题,因为您似乎还没有对它进行编程

var-urlJson=”https://api.myjson.com/bins/10t76x";
var QuestionHandler=函数(问题、选项、答案){
这个问题=问题;
this.options=选项;
这个答案=答案;
this.isCorrect=函数(猜测){
rtn=假;
如果(this.answer==guess)rtn=true;
返回rtn;
};
};
var PointsHandler=函数(){
该点为0;
this.addPoint=函数(){
这个.points++;
};
};
var getAllData=function(){
var-allData=null;
$.ajax({
url:urlJson,
键入:“GET”,
async:false,
数据类型:“json”,
成功:功能(res){
所有数据=问题;
},
错误:函数(err){
日志(“错误:+err”);
}
});
返回所有数据;
};
var QuizGame=函数(){
函数getResult(){
返回“您已回答”+此.points+“更正出”+此.index+“问题”;
}
该指数=0;
this.allJsonData=getAllData();
this.pointsHandler=新的pointsHandler();
this.questionHandler=新的questionHandler(this.allJsonData[this.index]。问题,this.allJsonData[this.index]。选项,this.allJsonData[this.index]。答案);
this.gameResult=getResult();
this.getPoints=函数(){
返回this.pointHandler.points;
};
this.nextQuestion=函数(猜测){
这个.index++;
var correct=this.questionHandler.isCorrect(猜测);
控制台日志(正确);
如果(正确){
this.pointsHandler.addPoint();
}
console.log(this);
};
};
var myquick=new QuizGame();
$(“#问题”).text(“问题”+(myquick.index+1)+”:“+myquick.questionHandler.question);
$.each(myquick.questionHandler.options,函数(i,val){
$(“#选项”)。追加(“+val+”);
});
$(“按钮”)。单击(功能(e){

如果(myquick.index代码中存在多个错误/错误

主要问题:您不能从
getAllData
返回
allData
,因为它是
AJAX
(异步调用)

我重构了它们,下面是一个工作示例

var-urlJson=”https://api.myjson.com/bins/10t76x";
var QuestionHandler=函数(问题、选项、答案){
var self=这个;
self.question=问题;
self.options=选项;
self.answer=答案;
self.isCorrect=function(guess){返回self.answer==guess;}
};
var PointsHandler=函数(){
var self=这个;
self.points=0;
self.addPoints=函数(){self.points++;}
};
var QuizGame=函数(数据){
var self=这个;
self.index=0;
self.allJsonData=数据;
self.pointsHandler=getPointsHandler();
self.questionHandlers=getQuestionHandlers();
self.gameResult=getResult;
self.getPoints=函数(){
返回self.pointsHandler.points;
};
self.nextQuestion=函数(猜测){
变量正确