JavaScript Reference ยท Classes and Modules
Private methods and #x in obj
Reference for JavaScript private methods, private accessors and the #x in obj brand check: syntax, errors on wrong objects, static privates and subclass behavior.
A private method is a method whose name starts with #. It can be called only from code inside the class body, so helper logic stays hidden and can change without breaking callers. Private accessors (get #x()) and private static methods work the same way.
The expression #x in obj is a brand check: it asks whether an object was built by this class (has the private member #x), without risking a TypeError.
class Counter { #count = 0; #log(msg) { console.log(msg); } increment() { this.#log("+1"); return ++this.#count; } static isCounter(value) { return #count in value; } }
Try it
Press Run (or Ctrl + Enter) to see the console output here.
No output.
Press Run (or Ctrl + Enter) to see the console output here.
No output.
Syntax
class Name { #method() {} get #accessor() {} set #accessor(v) {} async #asyncMethod() {} *#generator() {} static #staticMethod() {} use(obj) { this.#method(); return #method in obj; } }
| Form | Meaning |
|---|---|
#name() |
Private instance method. |
get #name() / set #name(v) |
Private accessor pair. |
static #name() |
Private static method, callable only inside the class. |
#name in obj |
true if obj has that private member. Only valid inside the class that declares #name. The right side must be an object, otherwise TypeError. |
Exceptions: calling obj.#method() on an object that lacks the member throws TypeError; using a #name that the class never declared is a SyntaxError found when the code is parsed.
Usage notes
- Installed per instance, shared per class. Each instance is "branded" with the private methods when it is constructed, but the function itself exists only once.
- Read-only. You cannot reassign a private method (
this.#m = otherthrowsTypeError), and you cannotdeleteit. - Invisible from outside. Private members do not show up in
Object.keys(),Object.getOwnPropertyNames(),JSON.stringify()orReflect.ownKeys(), as the first example shows for the prototype. - Subclass instances carry the brand of their parents, so
#x in subclassInstanceistrue. A subclass cannot call a parent's private method by name, though. thismatters.const f = obj.describe; f()losesthis, and thenthis.#m()throws. Bind the method or use an arrow function.- Proxies fail brand checks: a
Proxywrapped around an instance is not itself branded. - Static privates are accessed through the exact class that declares them. A subclass calling
this.#helper()in an inherited static method fails withTypeError; use the class name. - Brand checks replace
instanceofwhen you need proof that an object really passed through your constructor, sinceinstanceofcan be fooled by changing prototypes.
Browser support
Private methods, private accessors and #x in obj are supported in current browsers. They are newer than public fields, so check a compatibility table before targeting older ones.