🚀 go-pugleaf

RetroBBS NetNews Server

Inspired by RockSolid Light RIP Retro Guy

Thread View: pl.comp.lang.javascript
3 messages
3 total messages Started by witold.zywica Mon, 05 Nov 2012 11:58
object + this + array = this.array is undefined
#51206
Author: 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
Re: object + this + array = this.array is undefined
#51207
Author: Herr
Date: Thu, 08 Nov 2012 00:28
8 lines
1105 bytes
witold.zywica@gmail.com wrote:

> Witam
> Mam sobie takie coś:
[ciach]

Przyznam się bez bicia że nie mogę odtworzyć problemu - wszystko działa
jak (chyba) miało działać. Mógłbyś wkleić kod na jakiegoś pastebina?
Re: object + this + array = this.array is undefined
#51208
Author: Cezar ikari Pokorski
Date: Sun, 11 Nov 2012 23:31
44 lines
2081 bytes
W dniu 05.11.2012 o 12:58 <witold.zywica@gmail.com> pisze:

> Witam
> Mam sobie takie coś:
> (...)
> 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));
>     }
> }
>
> (...)
> 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?
>

Na szybko i zmeczono powiem tak:
wydaje mi się, że chodzi o to, że "this" wewnątrz addAnswer jest po prostu  
innym thisem niż w Question i dlatego jest problem z  
this.answers===undefined.

Zrobiłbym:

      this.answers = new Array();
      var that = this;  // <--- myk 1
      this.addAnswer = function (answer, explanation, type, validator) {
          that.answers.push( // <--- myk 2
              new Answer(answer, explanation, type, validator));


Powinno pomóc na podstawowy problem.


-- 
Nie mam weny na sygnaturkę
Thread Navigation

This is a paginated view of messages in the thread with full content displayed inline.

Messages are displayed in chronological order, with the original post highlighted in green.

Use pagination controls to navigate through all messages in large threads.

Back to All Threads