Lesson 27. Classes

 

After reading lesson 27, you will

  • Understand the syntax for defining classes
  • Know how to instantiate classes and how to use constructor functions
  • Know how to export classes from modules
  • Understand that class methods are not bound
  • Know how to assign class and static properties

Classes are little more than syntactic sugar for declaring a constructor function and setting its prototype. Even with the introduction of classes, JavaScript isn’t statically or strongly typed. It remains a dynamically and weakly typed language. However, classes do create a simple self-contained syntax for defining constructors with prototypes. The main advantage over constructors, though, besides syntax, is when extending classes, which we’ll get to in the next lesson. Without having an easily extendable built-in construct such as classes, many libraries like Backbone.js, React.js, and several others had to continue to reinvent the wheel to allow extending their base objects. This library-specific form of extending objects will become a thing of the past as more and more libraries start to provide a base JavaScript class that can easily be extended. This means once you learn how to use and extend classes, you’ll have a jump start on many of today’s and tomorrow’s frameworks.

27.1. Class declarations

27.2. Instantiating classes

27.3. Exporting classes

27.4. Class methods are not bound

27.5. Setting instance properties in class definitions

27.6. Static properties

Summary