Learn JavaScript

Lesson 4 of 9 · this, Prototypes and Classes

Module 9 · this, Prototypes and Classes

Class, Closure or Plain Object: Choosing How to Bundle Data with Behaviour

Free with an accountReading

Rafi's team is rewriting a phone app's settings and its app switcher. One teammate wants a class for everything. Another, fresh from Module 6, wants closures for everything. A third says plain objects are enough. They are all partly right. Each shape bundles data with behaviour, and each one is best at something different.

Checking what this lesson needs for you…

What is inside Class, Closure or Plain Object: Choosing How to Bundle Data with Behaviour

  1. Three ways to make a counter
  2. Shared methods against one copy per object
  3. Privacy: a # field or a closure
  4. this: the closure never needs it
  5. instanceof and inheritance
  6. What each one costs
  7. The comparison table
  8. Choosing by four questions