Einen Rechner erstellen, der neue Methoden akzeptiert. Aber wenn ich eine neue Methode hinzufüge, sieht es das Objekt "this" nicht. Warum Console.log gibt "undefined" zurück?Neue Methode sieht "this" nicht (JavaScript)
function Calculator() {
this.numbers = function() {
this.numberOne = 2;
this.numberTwo = 5;
},
this.addMethod = function(op, func) {
this[op] = func(this.numberOne, this.numberTwo);
// WHY LOG RETURNS "undefined"?
console.log(this.numberOne);
}
}
let calc = new Calculator();
calc.addMethod("/", (a, b) => (a/b));
document.write(calc["/"]);
Sie haben einen Tippfehler in Zeile 5: das Komma ein Semikolon sein sollte. – GMaiolo
'this.one' ist nie gesetzt, also ja, es ist' undefined'. –
+ goliadkin Ich denke, es funktioniert so oder so ... – ahitt6345