non js-master: Mathe-Term-Parser / Rekursions-Problem

Beitrag lesen

Shame on me,

ich habe meine Klassendefinition fehlerhaft gehabt. korrekt lautet sie:

function Term() {
 this.firstOperand = null;
 this.secondOperand = null;
 this.operation = "";
}

Term.prototype.getFirstOperand = function() {return this.firstOperand};
Term.prototype.setFirstOperand = function(sOperand) {this.firstOperand = sOperand};

Term.prototype.getSecondOperand = function() {return this.secondOperand};
Term.prototype.setSecondOperand = function(sOperand) {this.secondOperand = sOperand};

Term.prototype.getOperation = function() { return this.operation};
Term.prototype.setOperation = function(sOperation) {this.operation = sOperation};

Term.prototype.build = function() {
 var sFirst = null;
 var sSecond = null;
 if (typeof this.firstOperand == "object") {
  sFirst = this.firstOperand.build();
 } else {
  sFirst = this.firstOperand;
 }
 if (typeof this.secondOperand == "object") {
  sSecond = this.secondOperand.build();
 } else {
  sSecond = this.secondOperand;
 }
 return sFirst+this.operation+sSecond;
};

Mein Problem ist, dass wenn der erste Operand ein Object ist, dass das aufrufende Objekt wieder für den Aufruf genommen wird und nicht das Objekt, dass als erster Operand definiert. Daher kommt meine Endlosschleife. Allerdings verstehe ich nicht, wo ich den Fehler gemacht habe.