Introduction to Classes (Preview)

ES6 introduces the concept of classes. Though it has been possible to imitate this behavior for some time with prototypes, ES6 simply wraps the prototype model using a clearer syntax.

Previously, you could use JavaScript’s prototype model to create a class-like object using the Function type.

Despite this approach being perfectly acceptable, the complexity quickly increases if you attempt to perform some sort of class extension.

This can easily be made more concise with ES6 classes.

Static Methods

You can also add static methods to classes with the static keyword.

Static and Instance Variables

The ES6 specification does not support declaring static and instance variables directly. However, you can set initial values for instance variables inside of the constructor, like so:

However, there is a stage 1 proposal for ES7 that enables declaration of static and instance variables. More information on this can be found later in the course.

Lesson tags: ES6, JavaScript, Programming
Back to: Modern JavaScript > Classes and Functions