0

I have a class called question . I am trying to call this display method from another class by creating its correspoding object. But I get this error. Will be glad if you can help me with this.

///////////////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();
}

The Error Message which I get is. enter image description here Thanks in Advance

5
  • Did you instantiate the Object using the new keyword? Commented Oct 21, 2013 at 18:51
  • QType1.prototype.Display = function(){ , you are using "this" in method DisplayQuesitons. I do not see how "QType1" is part of "this". Just remove "this" from the call. Commented Oct 21, 2013 at 19:39
  • Yes the object is instantiated using the new keyword @this.question_objs.push(new QType1(questions[i].id, questions[i].settings.text, questions[i].settings.choices, questions[i].settings.answers)); . I changed the QType1.Display to QType1.prototype.Display and Still I am getting the same error. Commented Oct 21, 2013 at 19:47
  • for(var i=0, l=questions.length; i<l; i++){ this.question_objs[i] = new QType1(questions[i].id, questions[i].settings.text, questions[i].settings.choices, questions[i].settings.answers); } Commented Oct 21, 2013 at 20:05
  • Hi James. I didnt get it. I am not able to find my mistake.. Commented Oct 21, 2013 at 20:11

1 Answer 1

1

Two possible causes:

  1. you didn't declare function Qtype1() {} and
  2. you didn't instatiate a qtype1 object like so var q = new Qtype1()

EDIT: you didn't make Display a method of QType1. The only way you could have a QType1.Display method is if you had declared QType1 as a variable, like var QType1 = {} That way you could have a Display method directly bound to the variable. But as you declared it as a constructor you need QType1.prototype.Display = function () { console.log('Display question...'); };

Also - your inheritance is a bit messy. You're calling Question.call(this, id, text) then you're declaring QType1.prototype = new Question() and then doing the same with the constructor. You need to review your javascript prototypal inheritance theory a bit.

Sign up to request clarification or add additional context in comments.

4 Comments

Hi Joe, I did both of them. I am doing this 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)); }
And then I am calling this.question_objs[i].display();
and if you do a console.log(this.question_objs[i]) what's the output?
I am getting that as QType1 objects. When I do console.log(this.question_objs[i] instanceof QType1);, I get it as true.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.