JavaScript Classesconstructor()Methods
JavaScript Classes
Examples
Examples
Created a class named "Example", and initialized the class:
class Example {
constructor(name, url) {
this.name = name;
this.url = url;
}
}
let site = new Example("Example", "https://www.example.com");
Try it Yourself »
Definition and Usage
The constructor() method is a special method (constructor) used to create and initialize objects created within a class.
The constructor() method is automatically called when creating an object.
If no constructor method is explicitly specified, a default constructor method is added.
If a constructor method is not specified, a default constructor is used.
Within a constructor, the super keyword can be used to call a parent class's constructor.
Syntax
constructor(parameters)
Technical Details
| JavaScript Version: | ECMAScript 2015 (ES6) |
Browser Support
constructor() is an ECMAScript6 (ES6) feature.
ES6 (JavaScript 2015) is supported by all current major browsers.
| Chrome | Edge | Firefox | Safari | Opera |
| Yes | Yes | Yes | Yes | Yes |
Internet Explorer 11 or older versions of IE do not support the constructor() method.
JavaScript Classes Other Extensions