Objects, Functions, and Classes25% of the exam

Classes: inheritance, private and static members

How class syntax maps onto constructor functions and prototypes, and how to use extends and super, private # fields, accessors, static members and Error subclasses to meet a requirement, including the ReferenceError, TypeError and SyntaxError traps.

Official exam objective
Given a business requirement, apply fundamentals of class implementation to solve the business requirement.

~7 min read · 15 practice questions ·

What the exam expects

  • Explain what a class compiles to (a constructor function plus a prototype) and predict typeof, instanceof and prototype checks on classes and instances.
  • Know the behaviour changes when constructor functions move to classes: TDZ instead of hoisting, new required, strict mode, non-enumerable methods.
  • Write a subclass with extends, call super() before this, and reuse a parent method with super.method().
  • Choose between a #private field, a getter with no setter, a public field and a static member from an encapsulation or sharing requirement.
  • Predict this in static factories and detached class methods, and subclass Error for a custom error type.
  • Key terms: derived class (a class declared with extends); private element (a #name field or method readable only inside its class body); static member (defined on the class itself, not on instances); class field (a property declared in the class body and created on each instance); accessor (a get/set pair that runs code on read or write).

Key facts

typeof a class
Detail
'function'
Class before declaration
Detail
ReferenceError (temporal dead zone)
Class without new
Detail
TypeError
Class body
Detail
Always strict mode
Class methods
Detail
On the prototype, shared, non-enumerable
this before super()
Detail
ReferenceError in a derived constructor
Default derived constructor
Detail
Passes all arguments to the parent constructor
#name outside the class
Detail
Early SyntaxError; script does not run
Static members
Detail
On the class; not reachable through instances; inherited by subclasses
Public fields
Detail
Own, enumerable properties of each instance
instanceof
Detail
Searches the whole prototype chain
Error subclass name
Detail
Inherited as "Error" unless set
Sources for this section (11)
  • Why does `console.log(wallet.#balance)` outside the class stop even earlier lines from logging?
  • When does a derived constructor throw a ReferenceError?
  • Why does `new this()` inside an inherited static factory create a subclass instance?
  • What does `typeof` return for a class?
  • Which check returns true for an instance of any subclass of `Asset`?

The questions are free to read. Answers with their sources and self-grading are included with the pack.

Keep reading with the JavaScript Developer pack

  • Core concepts
  • Exam traps
  • Worked scenario

The pack includes:

  • All 24 lessons of the JavaScript Developer guide, with reading progress
  • Interactive decision walks and step-by-step sequence steppers
  • Inline practice questions and one-click topic practice (15 questions for this lesson)
  • An official source cited for every claim
  • An AI tutor that answers from the guide
  • Unlimited timed practice exams

Practice for the JavaScript Developer exam