Classes

ECMAScript 6, the next version of JavaScript, adds class-based object-orientation to JavaScript and, since TypeScript includes all the features available in ES6, developers are allowed to use class-based object orientation today, and compile them down to JavaScript that works across all major browsers and platforms, without having to wait for the next version of JavaScript.

Let's take a look at a simple TypeScript class definition example:

class Character { public fullname: string; public constructor(firstname: string, lastname: string) { this.fullname = `${firstname} ${lastname}`; } public greet(name?: string) { if (name) { return `Hi! ${name}! my name is ${this.fullname}`; } else { return `Hi! my name is ${this.fullname}`; } } } ...

Get Learning TypeScript 2.x - Second Edition 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.