Private Fields and Static Members

Intermediate · 10 min

Real, enforced privacy in classes with `#` fields, static members that belong to the class itself rather than any instance, and why it took until 2022.

Before thisES6 Classes and Inheritance

All Things JavaScript, LLC · 10 min

Private Fields and Static Members

The player loads from youtube-nocookie.com when you press play. Nothing reaches YouTube before that.

Taught by All Things JavaScript, LLCReport a problem with this video
Not started
Set this lesson's status

What you'll learn

  • A field or method prefixed with `#` (`#balance`, `#validate()`) is genuinely private — it cannot be read, written, or even detected from outside the class body, which is a different and stronger guarantee than anything JavaScript offered before ES2022.
  • Before `#` fields shipped, "private" in a JavaScript class was always a convention or a workaround — an underscore prefix that any caller could still access, a value hidden in a closure that broke normal class syntax, or a `WeakMap` keyed by instance that worked but added real complexity for something that should be simple.
  • `static` puts a property or method on the class itself, not on instances — `ClassName.method()` works without ever calling `new`, which is the right place for things like a shared counter, a factory method, or a utility that doesn't need any particular instance's data.
  • A private field declared in a parent class is not inherited the way a public one is reachable through the prototype chain — subclass code cannot see or touch a parent's `#field` directly, because true privacy means privacy from every caller, including your own subclasses.
  • Private static fields exist too, and the rule for accessing them is stricter than for public statics: access them through the class name inside the class's own methods, not through `this`, so a static method inherited by a subclass doesn't quietly break looking for a private field that only exists on the parent.

A gap classes had from the start

ES6 gave JavaScript class syntax, but it quietly left out something every mainstream object-oriented language already had: a real way to mark a field or method as private. For years, “private” in a JavaScript class meant an agreement, not an enforcement. You’d prefix a field with an underscore — _balance — and everyone was supposed to know not to touch it directly, but nothing in the language stopped them. Some codebases reached for a closure instead, capturing the private value in the constructor’s scope rather than storing it on the instance at all — that worked, but it meant giving up the clean class method syntax for anything that needed to touch the private data. Others used a WeakMap declared outside the class, keyed by each instance, to store private data separately — genuinely hidden from outside code, but at the cost of extra bookkeeping for something that, conceptually, should be simple. ES2022 finally closed this gap directly in the syntax, which is exactly why it’s worth knowing those older patterns existed: they explain why # fields were worth waiting for, instead of looking like a feature added just for the sake of it.

# fields: privacy the language actually enforces

A class field or method prefixed with # — #balance, #deposit() — is private in a way none of the older patterns could fully match. Code outside the class body cannot read it, write it, or even check whether it exists; trying to reference instance.#balance from outside the class that declared it is a syntax error, caught before the code even runs, not a runtime permission check that a determined caller could route around. That’s a meaningfully different guarantee than an underscore convention. It also means a subclass doesn’t automatically get access to a parent’s private fields the way it would with a normal inherited property — a #field belongs strictly to the exact class body that wrote it, full stop, which is the correct behavior for something whose whole point is staying internal to that one piece of code.

Static: the class’s own members, not the instance’s

static solves an unrelated but related problem: some properties and methods logically belong to the class rather than to any object the class produces. A running count of how many instances exist, a factory method that builds and returns a configured instance, a conversion utility that doesn’t need any particular instance’s data — all of these make more sense called as ClassName.thing() than as something every single instance would carry around uselessly. Static members are defined with the static keyword inside the class body and are never present on instances at all; new ClassName().staticMethod() doesn’t work, because the method was never put there.

Private and static combine, too — a static #counter is a private field that belongs to the class itself, invisible from outside and shared across every instance rather than copied per instance. The one sharp edge worth knowing here: inside a static method, access a private static field through the class’s own name (ClassName.#counter), not through this. If a subclass inherits that static method and calls it, this refers to the subclass, which never got its own copy of the parent’s private static field — using the class name directly sidesteps that entirely. The video covers this ES2022 feature set directly, including the # syntax for fields and methods and why it represents a real, newly-possible kind of privacy rather than another convention.

Where this fits

This closes out the object-model group. You’ve gone from what a property literally is — value plus descriptor flags — through the mechanism objects share behavior with (the prototype chain), to the syntax built on top of that mechanism (class, extends, and super), and finally to the piece classes added that plain prototypes never had a clean answer for: data and behavior that genuinely belongs to the class or instance alone. Everything downstream in this roadmap that reaches for a class — collections, async abstractions, anything with internal state worth protecting — builds on exactly this.

Check yourself

  1. 01Before ES2022, how would a JavaScript class actually enforce that a field couldn't be read from outside? Was that enforcement real?

    It couldn't, not really. The common patterns — naming a field `_balance` by convention, or capturing a value in a closure, or keeping a `WeakMap` outside the class keyed by instance — either relied on callers simply agreeing not to touch the data, or worked but at the cost of extra complexity (closures break normal class method syntax; a `WeakMap` needs separate bookkeeping per instance). None of them made the data actually unreachable the way `#` fields do.

  2. 02What happens if code outside a class tries to access `instance.#balance` directly?

    It throws a `SyntaxError` at the point the code is written — not even a runtime error you'd catch, but a parse-time rejection, because `#balance` syntax is only valid inside the body of the class that declared it. There's no way to reference a private field by name from outside, ever.

  3. 03Why would you make a method `static` instead of a normal instance method?

    When the method doesn't need any particular instance's data to do its job — a factory function that builds and returns a new instance, a utility calculation, or a counter tracking how many instances exist in total. Those belong to the class as a concept, not to any one object the class produced, so calling them as `ClassName.method()` without needing an instance at all is the honest representation of what they do.

  4. 04`Child extends Parent`, and `Parent` has a private field `#id`. Can code inside a `Child` method read `this.#id`?

    No. Private fields aren't inherited the way public properties are reachable through the prototype chain — only code written inside the exact class body that declared `#id` can see it. If `Child` needs access to something like it, `Parent` has to expose it deliberately, usually through a public or protected-by-convention method.

  5. 05Inside a class's own static method, should you access a private static field through `this` or through the class name?

    Through the class name (`ClassName.#field`), not `this`. If a subclass inherits that static method and calls it, `this` inside the method refers to the subclass, which doesn't have its own copy of the parent's private static field — using the class name directly avoids a broken lookup in that situation.

Go deeper

Notes

Private to you

Esc
↑↓ navigate↵ open? shortcuts

Keyboard shortcuts

Press Esc to close

Anywhere

⌘K
Search and commands
/
Search
?
This sheet
Esc
Close

Reading a lesson

J
Next lesson
K
Previous lesson
B
Bookmark