Members

In JavaScript ES6, classes support different types of members, such as methods and properties. TypeScript enriches that by bringing fields, read-only fields, and static members. Consider the following example:

class SumCalculator {    static readonly supportedOperand = Operand.Sum;    private _history: Calculable[] = [];    get history() { return this._history; }    set history(value: Calculable[]) {        this._history = value || [];    }              constructor(private readonly logPrefix: string) { }    logHistory() {        console.log(            `${this.logPrefix}: current history length: ${this.history.length}`);    }}console.log(SumCalculator.supportedOperand);const calc = new SumCalculator(‘MyApp');calc.history = [{left: 2, right: 3}];calc.logHistory();

In the preceding code, you ...

Get Hands-On Full-Stack Web Development with ASP.NET Core now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.