JavaScript Class Generator
List the fields of a class and get modern JavaScript: truly private #fields, getters and setters, a constructor that takes one object or positional arguments, type checks, toJSON for serialisation and a static fromJSON that turns date strings back into Date objects. JSDoc comments document every field, and the class can extend another class, such as Error.
- Runs in your browser
- No sign-up
- Free to use
How to use JavaScript Class Generator
- Enter the class name and, optionally, a parent class.
- List the fields as name: type = default.
- Choose the constructor style and options.
- Copy or download the class.
JavaScript Class Generator features
#private fields
ES2022 private fields exposed through getters.
Setters with validation
Type checks run in one place for constructor and setters.
Object constructor
new User({ email, name }) with defaults per field.
Serialisation
toJSON writes dates as ISO strings; fromJSON restores them.
Inheritance
extends with super(), including custom Error classes.
JSDoc
Types for fields, constructor parameters and accessors.
When to use JavaScript Class Generator
- Creating model classes for API data in a front-end app.
- Writing value objects such as Point or Money.
- Defining custom error types with extra fields.
- Learning modern class syntax with private fields.
JavaScript Class Generator FAQ
What are #private fields?
Fields whose names start with # can only be accessed inside the class body. Unlike the _name convention, the language enforces it; they are supported by all current browsers and Node.js.
Why a constructor that takes an object?
new User({ email, name }) is readable, does not depend on argument order and lets you skip optional fields. Positional arguments suit small classes such as Point(x, y).
How are dates serialised?
toJSON writes Date fields as ISO strings, which JSON.stringify uses automatically. fromJSON converts those strings back into Date objects.
What does readonly do?
A field marked readonly gets a getter but no setter, so it can only be set in the constructor.
Can I generate an Error subclass?
Yes. Extend Error and the constructor calls super and sets the error name, so stack traces show the class name.
Is anything uploaded?
No. The class is generated in your browser.
Classes in modern JavaScript
JavaScript classes have grown into a complete feature. Besides constructors, methods and inheritance, ES2022 added class fields and truly private #fields, which only code inside the class can read or write. A generator helps to apply these consistently, because a well-built class repeats the same pattern for every field: declaration, constructor assignment, getter, setter, validation and serialisation.
Fields are listed in a compact notation with a type, an optional default and flags for optional and read-only fields. With private fields on, each field is stored as #name and exposed through a getter; setters are added for fields that may change. Validation lives in the setters, and the constructor assigns through them, so every value passes the same checks wherever it comes from.
The constructor can take a single object, which reads well at the call site and makes optional fields easy, or positional arguments for small value types. Default values are written as parameter defaults, so a [] or {} default creates a new array or object for every instance rather than sharing one.
Serialisation is often needed for classes that hold data. toJSON returns a plain object that JSON.stringify uses automatically, writing dates as ISO 8601 strings. The static fromJSON accepts a JSON string or an already parsed object and creates a new instance, turning date strings back into Date objects so that the round trip is lossless.
Extending another class adds the extends clause and a super() call. For Error subclasses the generator also sets this.name, so that logs and stack traces show the custom error name instead of plain Error.