Classes Copy

Introduction

In general Object Oriented (OO) languages, use classes. However JavaScript does not have classes in the same sense as other (OO) languages like Java or Ruby.

ES6 did introduce a syntax for object creation that uses the class keyword. Although this is a new syntax it does the same thing as the object constructors and prototypes we have learned about in previous topics. Therefore in this topic you only need to learn about the new syntax.

If you want to use classes in your code, which is fine, you can use them much the same way as object constructors.

Learning Objectives

  • Pros and cons of using classes in JavaScript
  • The difference between JavaScript’s object creation differs from a language like Java or Ruby
  • The differences between using a class to define a constructor and other prototype methods
  • What "getters" & "setters" are
  • What computed names and class fields are
  • Function binding
  • How to use inheritance with classes
  • The conflict in JS with functional programming and classes

Study

  1. This article provides some pros and cons for classes. There are many people who think that class syntax is misleading for Javascript, and thus Factory Functions are inherently better. WE are not saying that classes are bad! We just want you to be informed on the opinions of both sides.

  2. This article is probably just about all you need to start using class syntax confidently. "Getters and Setters" are a useful feature!

  3. The MDN docs are, as usual, a great resource for going a little deeper. Look especially at the ‘extends’ and ‘Mixins’ sections. React (and other frameworks) uses classes in this way. In other words, you create your components and make them extend the core React component. In that way you have access to all their built-in functionality.

Leave a Reply