JavaScript Reference ยท Classes and Modules
Class fields and private members
Reference for JavaScript class fields: public, private (#name) and static fields, initialization order, arrow-function fields and how fields differ from methods.
A class field declares a property directly in the class body, without writing this.name = value in the constructor. Every new instance gets its own copy, created before the constructor body runs (or, in a subclass, right after super() returns).
Put a # in front of the name and the field becomes private: it exists only inside the class body and cannot be read, written, listed or even detected from outside. Add static and the field belongs to the class itself rather than to instances.
class Account { owner = "unknown"; // public field #balance = 0; // private field static count = 0; // static field }
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 { publicField; publicWithValue = expression; #privateField = expression; ["computed" + "Name"] = expression; static staticField = expression; static #privateStatic = expression; method() { return this.#privateField; } static check(obj) { return #privateField in obj; } }
- Initializers run for each instance, top to bottom, with
thisset to the new object. A field without an initializer is created with the valueundefined. - Private names must be declared in the class body. Using an undeclared
#nameis aSyntaxErrorat parse time, not a runtime error. #name in objis a brand check:trueif the object has that private field, without throwing.
Usage notes
- Fields are defined, not assigned. They are created with
Object.defineProperty()semantics, so setters inherited from a parent class are not triggered by a field of the same name. - Fields are own properties, so
Object.keys(), spreading andJSON.stringify()include public ones. Private fields never appear. - Methods are different. Methods live once on the prototype and are shared, whereas a field holds a separate value per instance. Use arrow-function fields when a callback needs its
thisbound, at the cost of one function per instance. - Initialization order. In a subclass, the parent's fields exist during the parent's constructor but the child's fields do not yet, as the second example shows.
- Real privacy.
#xis enforced by the language, unlike the_xnaming convention. Readingobj.#xon an object without that field throwsTypeError. - No inheritance of access. A subclass cannot read its parent's private fields, and two classes can both declare
#xwithout clashing. - Not deletable or reflectable.
delete this.#xis a syntax error, andObject.getOwnPropertyNames()cannot see private names. - Proxies do not forward private access: calling a method that uses
this.#xon aProxyof an instance throws.
Browser support
Public and private instance fields and static fields work in all current browsers. #x in obj and static blocks arrived later but are supported as well; check a compatibility table for older targets.