ES6 Classes
Fundamentals Chapter 7 built objects by hand, one at a time, each with its own copy of methods like greet() defined inline. class syntax (added in the same 2015 update as let/const) provides a blueprint for creating many similarly-shaped objects, plus a built-in way to extend one type's behaviour from another — JavaScript's equivalent of inheritance.
Defining a Class
constructor(...) runs automatically whenever new Person(...) is called — its job is to set up the new object's starting properties via this. Every other method (greet here) is defined directly inside the class body, with no function keyword and no commas between methods — a more compact form than Fundamentals Chapter 7's object-literal method syntax, but functionally similar underneath.
class is mostly a cleaner syntax over JavaScript's existing object/prototype system — typeof Person actually returns "function". The new keywords (class, constructor, extends) exist purely for readability; they don't introduce a fundamentally different kind of object.
Multiple Instances, Each With Their Own Data
Each new Person(...) call produces a completely independent object with its own name/age — but all instances share the exact same greet method definition, stored once rather than duplicated per object, which is more memory-efficient than the object-literal approach from Chapter 7 at scale.
Inheritance with extends
class Student extends Person makes Student a more specific version of Person — it automatically gets every method Person has (greet), plus whatever new methods it adds itself (study). super(name, age) must be called before this can be used at all inside a subclass constructor — it runs the parent class's constructor logic first, so this.name/this.age get set up correctly before Student adds its own school property.
constructor, it MUST call super(...) before referencing this anywhere — JavaScript throws a ReferenceError immediately otherwise. If a subclass needs no extra setup at all, its constructor can simply be omitted entirely; the parent's constructor runs automatically in that case.
Overriding a Method
Defining greet again inside Student replaces (overrides) the inherited version for any Student instance. super.greet() inside the override still reaches back to Person's original method — useful when the subclass wants to extend, not completely replace, the parent's behaviour.
Getters — Computed Properties That Look Like Plain Fields
get area() defines a method that's accessed WITHOUT parentheses — rect.area, not rect.area() — recalculated fresh every time it's read. Useful for values that are always derivable from existing properties (width × height) rather than stored separately, avoiding the risk of them drifting out of sync.
| Concept | Fundamentals Ch 7 (object literal) | This chapter (class) |
|---|---|---|
| Creating an instance | { name: ..., greet() {...} } | new Person(name) |
| Method sharing | Duplicated per object | Shared once across all instances |
| Extending behaviour | No built-in mechanism | class Sub extends Base |
| Computed property | A method called with () | get propertyName() — read without () |
Coding Challenges
Define a class Animal with a constructor taking name, and a method speak() logging "{name} makes a sound." Create two Animal instances with different names and call speak() on each.
📄 View solutionDefine a class Dog that extends Animal from Challenge 1, overriding speak() to log "{name} barks." instead, while still calling the parent's speak() first using super.speak(). Create a Dog instance and call speak() on it.
📄 View solutionDefine a class Circle with a constructor taking radius, and a getter area returning the circle's area (π × radius², using 3.14159 for π) and a getter circumference returning 2 × π × radius. Create an instance and log both computed properties.
📄 View solutionChapter 3 Quick Reference
- class Name { constructor(...) {...} method() {...} } — basic class shape
- new ClassName(...) — creates an instance, running the constructor
- class Sub extends Base — inherits all of Base's methods automatically
- super(...) — calls the parent constructor; required before using this in a subclass constructor
- super.method() — calls the parent's version of an overridden method
- get propertyName() {...} — a method read without parentheses, like a plain property
- class is mostly syntax over JavaScript's existing object/prototype system
- Next chapter: this in depth — call/apply/bind, and arrow vs regular function context