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.
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,instanceofand prototype checks on classes and instances. - Know the behaviour changes when constructor functions move to classes: TDZ instead of hoisting,
newrequired, strict mode, non-enumerable methods. - Write a subclass with
extends, callsuper()beforethis, and reuse a parent method withsuper.method(). - Choose between a
#privatefield, a getter with no setter, a public field and astaticmember from an encapsulation or sharing requirement. - Predict
thisin static factories and detached class methods, and subclassErrorfor a custom error type. - Key terms: derived class (a class declared with
extends); private element (a#namefield 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 (aget/setpair 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)
- Classesdeveloper.mozilla.org (opens in a new tab)
- classdeveloper.mozilla.org (opens in a new tab)
- Using classesdeveloper.mozilla.org (opens in a new tab)
- Method definitionsdeveloper.mozilla.org (opens in a new tab)
- superdeveloper.mozilla.org (opens in a new tab)
- constructordeveloper.mozilla.org (opens in a new tab)
- Private elementsdeveloper.mozilla.org (opens in a new tab)
- staticdeveloper.mozilla.org (opens in a new tab)
- Public class fieldsdeveloper.mozilla.org (opens in a new tab)
- instanceofdeveloper.mozilla.org (opens in a new tab)
- Errordeveloper.mozilla.org (opens in a new tab)
- 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