ECMAScript 6 classes and modules

We've seen so far that it is perfectly possible to build classes and even modules in JavaScript. The syntax is, obviously, a bit more involved than in a language such as C# or Java. Fortunately, the next version of JavaScript, ECMAScript 6 (also known as Harmony), brings support for some syntactic sugar for making classes:

class Castle extends Westeros.Structures.BaseStructure {
  constructor(name, allegience) {
    super(name);
    ...
  }

  Build() {
    ...
    super.Build();
  }
}

ECMAScript 6 also brings a well thought out module system for JavaScript. There is syntactic sugar for creating modules which looks like:

module 'Westeros' { export function Rule(rulerName, house) { ... return "Long live " + rulerName + " of house " + house; ...

Get Mastering JavaScript Design Patterns 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.