Skip to content
Tutorials Center
My learning

Topics

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.

JavaScript
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

JS A private helper

Press Run (or Ctrl + Enter) to see the console output here.

No output.

JS Brand checks and wrong receivers

Press Run (or Ctrl + Enter) to see the console output here.

No output.

Syntax

JavaScript
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 = other throws TypeError), and you cannot delete it.
  • Invisible from outside. Private members do not show up in Object.keys(), Object.getOwnPropertyNames(), JSON.stringify() or Reflect.ownKeys(), as the first example shows for the prototype.
  • Subclass instances carry the brand of their parents, so #x in subclassInstance is true. A subclass cannot call a parent's private method by name, though.
  • this matters. const f = obj.describe; f() loses this, and then this.#m() throws. Bind the method or use an arrow function.
  • Proxies fail brand checks: a Proxy wrapped 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 with TypeError; use the class name.
  • Brand checks replace instanceof when you need proof that an object really passed through your constructor, since instanceof can 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.