TetSharing
JavaScript tutorial · 19 chapters
  1. Get started
  2. let, const and var
  3. Data types
  4. Operators and equality
  5. Strings
  6. Template literals
  7. if, else and switch
  8. Loops
  9. Functions
  10. Arrow functions
  11. Arrays
  12. map, filter, reduce
  13. Objects
  14. Destructuring and spread
  15. Classes
  16. Errors and try...catch
  17. Promises
  18. async and await
  19. JSON

Final JavaScript quiz
Cheat sheet: javascript array methods

Chapter 15 of 19 · By Ethan Cole · Updated November 21, 2026

JavaScript Classes: constructor, Methods, extends

A class is a template for creating objects that share the same shape and behavior. new Account("Ava") creates an instance: a new object with its own data, and access to the methods defined in the class. Classes arrived in ES2015 as cleaner syntax over JavaScript's older prototype system, and they are standard in modern code.

constructor and this

The constructor method runs when you call new. Inside it, this is the new object, and assignments like this.owner = owner create its properties. Methods written in the class body, such as deposit, are shared by all instances. Returning this from a method enables chaining, as in new Account("Ava").deposit(50).deposit(25).

Private fields, getters and static members

  • #balance = 0 declares a private field. Only code inside the class can read or write it; acc.#balance outside the class is a syntax error. That protects the balance from being changed without going through deposit, which validates the amount.
  • get balance() defines a getter: callers read acc.balance like a property, but a method runs behind the scenes.
  • static count = 0 belongs to the class itself rather than to instances, so it is read as Account.count. Here it counts how many accounts were created.
JavaScript · editable

Expected output

Ava 75
220 true 2
function ["owner", "rate"]

Inheritance with extends

class Savings extends Account creates a subclass that inherits everything from Account. A subclass constructor must call super(...) before it uses this; that runs the parent constructor. Methods can call inherited methods with this.deposit(...), or the parent's version of an overridden method with super.deposit(...). instanceof checks the chain: a Savings object is also an Account.

The last line of the output shows two facts about classes. typeof Account is "function", because a class is a special kind of function under the hood. And Object.keys(s) lists only the public own properties, owner and rate; the private field is invisible.

When to use classes

Classes suit things with both data and behavior that you create many of, such as accounts, UI components or game entities. For simple data, plain objects and functions are often enough. Unlike function declarations, classes are not hoisted: define them before you use them.

Common mistakes

  • Forgetting new, which throws TypeError: Class constructor Account cannot be invoked without 'new'.
  • Using this in a subclass constructor before calling super().
  • Passing a method as a callback, as in setTimeout(acc.deposit, 10), and losing this. Wrap it in an arrow function.

Summary

  • class + constructor define how instances are built; new creates them.
  • Methods are shared; #private fields hide data; get defines computed properties; static belongs to the class.
  • extends and super() create subclasses.

Quiz: Classes

1. What must a subclass constructor do before using this?

super() runs the parent constructor and initializes this.

2. Where can a #private field be accessed?

Private fields are only reachable by code in the class that declares them.

3. What is typeof a class?

Classes are a special kind of function.

Official docs

Written under the pen name Ethan Cole with AI-assisted drafting. Every code example on this page is executed by our test suite before publishing. Found a mistake? Tell us.