To create a constructor in JavaScript, either define a constructor method inside a class or write a regular function and call it with new. In both cases new creates an empty object, binds it to this, runs the constructor to fill in properties, and returns the object. The class form is the modern, recommended way.
Function constructors still matter for reading older code and for understanding how prototypes work.
Inside a class, the special constructor method runs each time you call new. A class can have only one constructor.
Properties assigned to this become own properties of each instance. Methods like speak are placed on Animal.prototype and shared by all instances. Calling Animal('Max', 'dog') without new throws a TypeError, which protects you from forgetting it.
Default parameters and destructuring make constructors more readable:
Before ES2015, constructors were plain functions whose names start with a capital letter, with shared methods assigned to the prototype.
new.target is undefined when the function is called without new, so the guard above makes the call safe either way. Do not define methods inside the constructor body (this.greet = function () {...}) unless you need per-instance closures, because each object then gets its own copy of the function.
When you write new Person('Alice', 30), JavaScript performs these steps:
Person.prototype.Person with this bound to the new object.Returning a primitive from a constructor is ignored, while returning an object replaces the result. Do this only on purpose.
Modern classes can declare fields outside the constructor, including private ones with #.
The field initialisers run before the constructor body in a base class, so #count already exists when the constructor starts. Code outside the class cannot read #count.
A subclass constructor must call super(...) before it uses this.
If you omit the constructor in a subclass, JavaScript supplies one that forwards all arguments to the parent.
Use a class in all new code. It enforces new, supports extends, super, private fields and static members, and is what linters, TypeScript and modern frameworks expect. Keep function constructors only when maintaining legacy code or when you need to understand how a library sets up its prototypes.
If you need several objects without new at all, a plain factory function that returns an object literal is also a valid pattern, especially when you want closures for private state.
They produce similar results, but a class constructor must be called with new, its methods are non-enumerable and its body runs in strict mode. A function constructor has none of those guarantees.
No. A class has one constructor. To support different inputs, use default parameters, check argument types, or add static factory methods such as User.fromJSON(...).
super() first?In a derived class, this does not exist until the parent constructor has created it. Using this before super() throws a ReferenceError.
No. A constructor cannot be async. Use a static async factory method that awaits the data and then returns a new instance.
Prefer a class with a constructor method, create objects with new, and call super() first in subclasses. Function constructors with prototypes are the older equivalent, and new always creates the object, links its prototype and returns it.