我有一個叫做question的類。我試圖通過創建其對應的對象來從另一個類中調用此顯示方法。但是我得到這個錯誤。如果你能幫助我,我會很高興。Javascript class「Object has no method error」
///////////////QUESTION///////////////
function Question(id, text){
this.id = id;
this.text = text;
}
function QType1(id, text, choices, answers){
//this is true or false
Question.call(this, id, text) ;
this.choices = choices;
this.answers = answers;
}
QType1.prototype = new Question();
//inherit Question
QType1.prototype.constructor = QType1;
QType1.Display = function(){
console.log("Display Question");
}
///////////////QUIZ//////////////////
function quiz(){}
quiz.prototype.SetHomeScreen = function(x,y){
var svgCanvas = CreateCanvas(x,y);
AddText(svgCanvas,100,50, quiz_input.Settings.Layout.Text);
console.log("Text Added");
start_but = AddStartButton(svgCanvas, 300, 250);
console.log("start button Added");
start_but
.on("click", function(d,i) {
startquiz();
});
var startquiz = function(){
console.log(this);
quiz.prototype.StartQuiz();
};
}
quiz.prototype.question_objs = [];
quiz.prototype.user_ans = [];
quiz.prototype.corr_ans = [];
quiz.prototype.LoadQuestions = function(){
for(var i=0, l=questions.length; i<l; i++){
this.question_objs.push(new QType1(questions[i].id, questions[i].settings.text, questions[i].settings.choices, questions[i].settings.answers));
}
console.log(this.question_objs);
}
quiz.prototype.DisplayQuestions = function(){
var i = 0;
var l = this.question_objs.length;
while(i < l){
console.log(this.question_objs[i] instanceof QType1);
this.question_objs[i].Display();
}
}
quiz.prototype.StartQuiz = function(){
quiz.prototype.LoadQuestions();
console.log("Starting Quiz");
quiz.prototype.DisplayQuestions();
}
我得到的錯誤信息是。 由於提前
您是否使用'new'關鍵字實例化對象? – FK82
QType1.prototype.Display = function(){,您在方法DisplayQuesitons中使用「this」。我不明白「QType1」是「this」的一部分。從通話中刪除「this」。 –
是對象使用新關鍵字@ this.question_objs.push(新QType1(問題[I] .id,問題[I] .settings.text,問題[I] .settings.choices,問題[I])實例化。 settings.answers)); 。我將QType1.Display更改爲QType1.prototype.Display,但仍然出現相同的錯誤。 – surya