Vyjadrí gramatiku jazyka objektmi a vyhodnotí zapísané výrazy.
class Expression { evaluate() { throw new Error('abstraktná metóda'); }} class NumberLiteral extends Expression { constructor(value) { super(); this.value = value; } evaluate() { return this.value; }} class Negation extends Expression { constructor(operand) { super(); this.operand = operand; } evaluate() { return -this.operand.evaluate(); }} class Sum extends Expression { constructor(left, right) { super(); this.left = left; this.right = right; } evaluate() { return this.left.evaluate() + this.right.evaluate(); }} // The written expression 5 + (-3) is a tree of objects, not text.const expression = new Sum(new NumberLiteral(5), new Negation(new NumberLiteral(3))); expression.evaluate(); // 2