freecoding.school100% FREE · NO SIGNUP
Closure CavesISSUE #41 of 61

classes · inheritance · extends · super

LambdaVSThe Mutator
Lambda saysextends creates a subclass; super calls the parent constructor and methods.

class Admin extends User inherits everything from User. In the subclass constructor you must call super(...) before using this. super.method() calls the parent's version, so you can extend behavior rather than replace it. Favor shallow hierarchies — deep inheritance gets brittle.

Power-ups you unlock

The Mutator attacks — common mistakes

Boss battleSubclass a base class, call super in the constructor, and extend a method.

Example code

<!doctype html><html><head><meta charset="utf-8"></head>
<body style="background:#06040d;color:#e6e0ff;font-family:monospace;padding:20px"><pre id="o"></pre>
<script>class User{constructor(n){this.n=n;} role(){return 'user';}}
class Admin extends User{ role(){ return super.role()+' (admin)'; } }
document.getElementById('o').textContent=new Admin('drew').role();</script></body></html>
▶ Open the interactive comic issue
‹ Classes · Constructor · Methods · GettersClasses · Static · Private · #Fields ›