liveBook · Manning

Class

As explained in JavaScript in Depth

A JavaScript language construct that defines a blueprint for creating objects with methods, initialization behavior, inheritance, and optionally private state.

A class looks like a constructor-and-method bundle, but its behavior is more precise than equivalent function syntax. Class constructors require new, methods are non-enumerable, and instances connect to shared prototype methods rather than copying them. section 5.6 section 6.2.4

The constructor initializes own instance state. Methods live on the prototype, while accessors also live on the prototype; class fields belong to each instance. extends establishes inheritance, super() invokes the parent constructor, and super.method() reaches a parent method. section 5.6 section 6.2.4

class FilingCabinet {
  constructor(material, color) {  #A
    this.material = material;  #A
    this.color = color;  #A
    this.drawers = [];  #A
    this.isLocked = false;  #A
  }  #A
 
  addDrawer(drawer) {
    this.drawers.push(drawer);
    console.log(`Added ${drawer.label} drawer to cabinet`);
  }
 
  findDrawer(label) {
    return this.drawers.find(drawer => drawer.label === label);
  }
 
  lock() {
    this.isLocked = true;
    console.log('Filing cabinet locked');
  }
 
  unlock(key) {
    if (key === 'master-key') {
      this.isLocked = false;
      console.log('Filing cabinet unlocked');
      return true;
    }
    console.log('Invalid key');
    return false;
  }
}

A FilingCabinet class combines constructor initialization, mutable instance state, lookup behavior, and locking operations in one object design. section 5.6

Use these rules to avoid misleading equivalences:

  • Treat classes as syntactic sugar over constructor functions and prototype assignment for ordinary object creation. section 5.6
  • Use #private fields when outside code must be genuinely unable to access or modify state; underscore naming provides only convention. section 5.6 section 6.2.4
  • Remember that classes are constructible but not directly callable, unlike ordinary constructor functions. section 6.2.4

How JavaScript in Depth covers it

How deep it goes
Explains prototype translation, constructor guardrails, property placement, inheritance calls, and private fields, stopping short of engine implementation details.
Examples
Uses FilingCabinet, Drawer, SecureDrawer, descriptor inspection, and a private Vault field.
What it assumes
Assumes basic objects and functions, while connecting class syntax to constructor functions and prototype chains.
What you can do after
Apply it via JavaScript class syntax while predicting instance properties, prototype sharing, inheritance behavior, and private-state access.
How it connects
Connects classes to constructors, prototypes, prototype chains, inheritance, accessors, and private fields.

Also explained in

Compare how 27 books explain class →

Related concepts

This explainer was generated by Manning's knowledge graph from the text of JavaScript in Depth. The section links point to the parts of the book it draws on.