classkeyword. It is basically a new syntax that does the exact same thing as the object constructors and prototypes we learned about in the constructor lesson.
There is a bit of controversy about using the class syntax, however. Opponents argue that
classis basically just syntactic sugar over the existing prototype-based constructors and that it's dangerous and/or misleading to obscure what's really going on with these objects. Despite the controversy, classes are beginning to crop up in real code bases that you are almost certainly going to encounter such as frameworks like React.
Since we've already gone fairly in-depth with Constructors, you don't have too much left to learn here beyond the new syntax. If you choose to use classes in your code (that's fine!) you can use them much the same way as object constructors.
After this lesson and completing the assignments, you will be able to:
- Explain the differences between using a class to define a constructor and other prototype methods.
- Explain what "getters" & "setters" are.
- Understand what computed names and class fields are.
- Describe function binding.
- Be able to use inheritance with classes.
- Briefly talk about the conflict in JS with functional programming and classes.
- 2.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. You create your components and make them
extendthe core React component which gives you access to all their built-in functionality.
Go back to your "Library" example and refactor it to use
classinstead of plain constructors.
This section contains helpful links to other content. It isn't required, so consider it supplemental for if you need to dive deeper into something.