Private archive copy · Unofficial site · Content copyright belongs to Runoob Tutorial and the original authors · View Original

JavaScript Class constructor() Method

JavaScript Class constructor() Method

JavaScript Class (class) JavaScript Classes (class)


Example

Example

Creates a class named "Runoob" and initializes it:

class

Runoob

{

constructor

(

name

,

url

)

{

this

.

name

=

name

;

this

.

url

=

url

;

}

}

let

site

=

new

Runoob

(

"

Runoob Tutorial

"

,

"

https://www.runoob.com

"

)

;

Try it »


Definition and Usage

The constructor() method is a special method (constructor) used to create and initialize objects created in a class.

The constructor method constructor() is called automatically when an object is created.

If no constructor is explicitly specified, a default constructor method is added.

If you do not specify a constructor (constructor) method, a default constructor (constructor) is used.

Inside a constructor, you can use the super keyword 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 major browsers today.

Chrome Edge Firefox Safari Opera
Yes Yes Yes Yes Yes

Internet Explorer 11 or older IE does not support the constructor() method.


JavaScript Class (class) JavaScript Classes (class)