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

classes · static · private · #fields

LambdaVSThe Mutator
Lambda saysstatic members belong to the class itself; #fields are truly private.

static methods/properties live on the class, not instances (User.create()) — good for factories and constants. Fields prefixed with # (#secret) are genuinely private: inaccessible outside the class, unlike the old underscore convention. This finally gives JS real encapsulation.

Power-ups you unlock

The Mutator attacks — common mistakes

Boss battleAdd a static factory method and a #private field with a public getter.

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 Counter{ #n=0; static make(){ return new Counter(); } inc(){ return ++this.#n; } }
const c=Counter.make();
document.getElementById('o').textContent=[c.inc(),c.inc()].join(' ');</script></body></html>
▶ Open the interactive comic issue
‹ Classes · Inheritance · Extends · SuperPrototypes · The Old Inheritance ›