Expressions and patterns

Class constructor

Try the free JavaScript compiler

Special class method used for instance construction. Only one constructor definition is permitted. Ordinary calls to class constructors without construction throw TypeError.

Formal Syntax & Structure

javascript syntaxjavascript
constructor(...args) { /* initialize instance */ }

Working Example

Example Snippetjavascript
class Box{constructor(n){this.n=n;}}console.log(new Box(4).n);
JavaScript Reference