JavaScript Classes
Understand JavaScript classes without losing sight of the object model: constructors, methods, fields, private elements, inheritance, super, static members, and initialization order.
Advanced
JavaScript classes provide class syntax without replacing JavaScript’s object and prototype model. This lesson owns the class-specific language: declarations and expressions, constructors, class elements, public and private fields, methods and accessors, static members, inheritance, super, private brand checks, and static initialization. Prototype lookup itself belongs to the Objects and Prototypes lesson; function-call this rules belong to Function Objects and this; error construction belongs to Errors and Defensive Control Flow.
How to use this lesson
Run each example before reading the explanation. The goal is to observe where class-created behavior lives and when initialization happens, then connect that behavior to the ECMAScript model. The examples deliberately avoid re-teaching general prototype lookup, descriptors, or function-call semantics already owned by other lessons.
A class declaration produces a class value
Output
Ready to run.
Quick check
What does a class declaration bind to its name?
A class expression is itself an expression
Output
Ready to run.
Quick check
What does a class expression evaluate to?
Constructor calls initialize instances
Output
Ready to run.
Quick check
What role does a constructor method normally have in a base class?
Instance methods are prototype-backed
Output
Ready to run.
Quick check
Where is an ordinary class instance method defined?
Class bodies are strict mode code
Output
Ready to run.
Quick check
What strictness applies to code inside a class definition?
Public instance fields are per-instance
Output
Ready to run.
Quick check
What does an instance field create?
Field initializers can observe earlier instance fields
Output
Ready to run.
Quick check
Can an instance field initializer use an earlier field through this?
Static members belong to the class value
Output
Ready to run.
Quick check
Where is a static field or method associated?
extends establishes class inheritance
Output
Ready to run.
Quick check
What relationship does extends establish?
Derived constructors use super before this
Output
Ready to run.
Quick check
Why must a derived constructor call super() before accessing this?
super method calls preserve the current receiver
Output
Ready to run.
Quick check
When super.speak() is called from Dog.prototype.speak(), what is this inside the inherited method?
Private fields are not ordinary properties
Output
Ready to run.
Quick check
How is a private field accessed from class code?
Private methods and accessors are class-private elements
Output
Ready to run.
Quick check
Which statement describes a private method such as #read?
Private presence can be tested with #name in object
Output
Ready to run.
Quick check
What does #value in object test inside the class that declares #value?
Static initialization blocks run during class evaluation
Output
Ready to run.
Quick check
When do static initialization blocks execute?
Static private elements belong to the class
Output
Ready to run.
Quick check
Where does a static private element belong?
Class initialization order matters
A class body is evaluated as a sequence of class elements. Static fields and static initialization blocks run during class definition in source order. Instance fields and instance private elements are initialized as part of construction, also according to the class’s defined initialization order. Derived construction adds the superclass step before derived instance initialization can proceed. These ordering rules are often more important than memorizing syntax.
Authoring rules
Use a class when a coherent instance protocol, construction model, or inheritance relationship makes the class syntax clearer than unrelated functions and objects.
Remember that ordinary instance methods are prototype-backed; do not assume each instance owns a fresh method function.
Use instance fields for per-instance state and static fields for state associated with the class value.
Use private elements when the invariant requires language-enforced private names; #name is not a string property key.
In derived constructors, call super() before reading or writing this.
Treat static initialization blocks as class-evaluation work, and keep side effects there deliberate because they run when the class is evaluated.
Do not use inheritance merely to share a small helper. Composition, functions, or plain objects can express a simpler relationship.
Practice
Build a small Shape → Rectangle hierarchy. Give Rectangle one public instance field and one private field, add a shared instance method, add a static factory, and include a static initialization block. Then inspect which values are on the instance, which are on the prototype, and which belong to the class value. Keep the inspection focused on class-specific behavior rather than repeating the prototype lesson.
Specification references
Normative authority: ECMA-262, current living specification. The central language area is the Functions and Classes chapter, especially Class Definitions and its evaluation semantics. Related semantics include private elements, private environments, ordinary object/prototype behavior, and execution contexts.
Thanks for your feedback.
Mark this learning resource complete to track your learning progress.