Classes

We will learn how to define and use classes in JavaScript.

Classes in JavaScript

Classes are a concept most will know from object-oriented languages, such as Java. However, ES2015 adds Java-like classes to JavaScript. In the past, object orientation could only be mimicked using the prototype property of a function and defining its methods and properties. Compared to many other object-oriented languages, though, it seemed overly complicated and wordy in JavaScript.

Since ES2015, classes can be defined by using the keyword class. While React uses principles of functional programming, it also relies heavily on ES2015 classes to instantiate React Class components.

Note: Before ES2015, it was possible to define React components using createClass(), but this technique has since been deprecated and should no longer be used.


Components of a class

A class consists of a name, an optional constructor that gets called at the creation of a class instance, and an unlimited number of class methods.

Get hands-on with 1400+ tech skills courses.