At the moment, not all of these features are available in all environments, but we’re not here to talk about feature support. Instead, we’re making the assumption that they are available in your environment or you’re using a tool like Babel to transform your JavaScript.

Classes, in ES2015, provide constructors, static and instance methods, inheritance, and the ability to extend a class by another class.

Best of all, you don’t have to call classes “functions” anymore.

Syntax

Defining a class

Defining a class involves the class keyword followed by the name for the class (typically in PascalCase).

Boom.

constructor

The optional constructor method allows you to perform actions when an instance of a class is instantiated although you probably just want to take this opportunity to define properties.

extend & super

extend allows you to create a sub class!

When creating a sub class, you call super with the necessary parameters for the base class. Any additional properties must be defined after that super call.

More on super

Methods from base classes are available on super within their corresponding sub classes.

static

static methods may only be called on the class itself as opposed to on its instantiated instances.

Static methods on a base class may be called on super from within a subclass.

Getters and Setters

Getters and setters work!


Hopefully this gives you a running start with ES2015 classes! If you have any questions, comments, or concerns, leave a comment!