🚀 go-pugleaf

RetroBBS NetNews Server

Inspired by RockSolid Light RIP Retro Guy

Article View: pl.comp.lang.javascript
Article #51206

object + this + array = this.array is undefined

#51206
From: witold.zywica
Date: Mon, 05 Nov 2012 11:58
56 lines
2960 bytes
Witam
Mam sobie takie coś:
function Answer(answer, explanation, type, validator) {
    this.answer = answer;
    this.explanation = explanation;
    this.type = type;
    this.validator = ('undefined' == validator ? null : validator);
}

function Question(question, explanation, type) {
    this.question = question;
    this.explanation = explanation;
    this.type = type;
    this.answers = new Array();
    
    this.addAnswer = function (answer, explanation, type, validator) {
        this.answers.push(new Answer(answer, explanation, type, validator));
    }
}

function Data(title, description) {
    this.title = title;
    this.description = description;
    this.questions = new Array();
    
    this.addQuestion = function (question_id, question, explanation, type) {
        this.questions[question_id] = new Question(question, explanation, type);
    }
    
    this.addAnswer = function (question_id, answer, explanation, type, validator) {
        this.questions[question_id].addAnswer(answer, explanation, type, validator);
    }
}

Następnie takie coś:
var data = new Data('tytul', 'opis');
for (var i=0; i<5; i++) {
    data.addQuestion(i, 'a'+i, 'b'+i, 'c'+i);
}

Na stronie dostaję błąd (firebug):
this.answers is undefined
        this.answers.push(new Answer(answer, explanation, type, validator));

Co robie zle? Jak sprawiść aby to zadziałało? 

A pozniej jeszcze dodać możliwość wstawiania odpowiedzi do pytań, czyli:
var data = new Data('tytul', 'opis');
for (var i=0; i<5; i++) {
    data.addQuestion(i, 'a'+i, 'b'+i, 'c'+i);
    for (var j=0; j<5; j++) {
        data.addAnswer(i, 'a'+j, 'b'+j, 'c'+j, 'd'+j);
    }
}

Z góry dziekuje za pomoc :D
Technical Details
Message-ID: <1c16ab7f-6d7c-4b33-9c1e-8e9e776a0ddf@googlegroups.com>
Path: mbox-mo1.pugleaf.net!news-archive.icm.edu.pl!agh.edu.pl!news.agh.edu.pl!newsfeed2.atman.pl!newsfeed.atman.pl!goblin2!goblin.stu.neva.ru!newsfeed.x-privat.org!news-out.readnews.com!transit4.readnews.com!209.85.216.87.MISMATCH!c7no6841578qap.0!news-out.google.com!gf5ni18188417qab.0!nntp.google.com!c7no7081457qap.0!postnews.google.com!glegroupsg2000goo.googlegroups.com!not-for-mail

Show Full Headers
Complete Article Headers
Received: by 10.224.223.14 with SMTP id ii14mr5438890qab.3.1352116695667;        Mon, 05 Nov 2012 03:58:15 -0800 (PST)
Received: by 10.236.126.227 with SMTP id b63mr530437yhi.0.1352116695634; Mon, 05 Nov 2012 03:58:15 -0800 (PST)
Path: mbox-mo1.pugleaf.net!news-archive.icm.edu.pl!agh.edu.pl!news.agh.edu.pl!newsfeed2.atman.pl!newsfeed.atman.pl!goblin2!goblin.stu.neva.ru!newsfeed.x-privat.org!news-out.readnews.com!transit4.readnews.com!209.85.216.87.MISMATCH!c7no6841578qap.0!news-out.google.com!gf5ni18188417qab.0!nntp.google.com!c7no7081457qap.0!postnews.google.com!glegroupsg2000goo.googlegroups.com!not-for-mail
Newsgroups: pl.comp.lang.javascript
Date: Mon, 5 Nov 2012 03:58:15 -0800 (PST)
Complaints-To: groups-abuse@google.com
Injection-Info: glegroupsg2000goo.googlegroups.com; posting-host=194.251.119.198; posting-account=GCQVcgoAAADjp-6CvhS4IjykuPgElWlE
NNTP-Posting-Host: 194.251.119.198
User-Agent: G2/1.0
MIME-Version: 1.0
Message-ID: <1c16ab7f-6d7c-4b33-9c1e-8e9e776a0ddf@googlegroups.com>
Subject: object + this + array = this.array is undefined
From: witold.zywica@gmail.com
Injection-Date: Mon, 05 Nov 2012 11:58:15 +0000
Content-Type: text/plain; charset=UTF-8
Content-Transfer-Encoding: 8bit