Vyjadrí gramatiku jazyka objektmi a vyhodnotí zapísané výrazy.
class Vyraz { vyhodnot() { throw new Error('abstraktná metóda'); }} class Cislo extends Vyraz { constructor(hodnota) { super(); this.hodnota = hodnota; } vyhodnot() { return this.hodnota; }} class Zapor extends Vyraz { constructor(operand) { super(); this.operand = operand; } vyhodnot() { return -this.operand.vyhodnot(); }} class Sucet extends Vyraz { constructor(lavy, pravy) { super(); this.lavy = lavy; this.pravy = pravy; } vyhodnot() { return this.lavy.vyhodnot() + this.pravy.vyhodnot(); }} // Zapísaný výraz 5 + (-3) je strom objektov, nie text.const vyraz = new Sucet(new Cislo(5), new Zapor(new Cislo(3))); vyraz.vyhodnot(); // 2