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


























