Classes
Introduction
JavaScript does not have classes in the same sense as other object-oriented languages like Java or Ruby. ES6, however, did introduce a syntax for object creation that uses the class keyword. It is basically a new syntax that mostly resembles object constructors and prototypes we learned about in the constructor lesson.
Historically, especially when ES6 was released, there was some controversy with class syntax precisely because it looks like classes from languages like Java. While JavaScript’s class syntax appears to be a syntactic sugar, it has some unique properties that differentiate itself from object constructors and prototypes. Still, the underlying mechanisms have not changed despite the different syntax (no classical inheritance going on) but often trips people up since the syntax isn’t as explicit about what’s really going on with these objects.
Plenty of time has passed though and class syntax now exists in many code bases. There aren’t many new mechanisms to learn here, mainly just new syntax for mostly familiar concepts.
Lesson overview
This section contains a general overview of topics that you will learn in this lesson.
- Explain the differences between an object constructor and a class.
- Explain what getters and setters are.
- Describe basic class syntax.
- Use inheritance with classes.
- Explain how to implement private class fields and methods.
- Explain what static properties and methods are.