Please note, this is a STATIC archive of website developer.mozilla.org from 03 Nov 2016, cach3.com does not collect or store any user information, there is no "phishing" involved.

Revision 985339 of constructor

  • Revision slug: Web/JavaScript/Reference/Classes/constructor
  • Revision title: constructor
  • Revision id: 985339
  • Created:
  • Creator: fscholz
  • Is current revision? Yes
  • Comment bug 1197932

Revision Content

{{jsSidebar("Classes")}}

The constructor method is a special method for creating and initializing an object created with a class.

Syntax

constructor([arguments]) { ... }

Description

There can only be one special method with the name "constructor" in a class. A {{jsxref("SyntaxError")}} will be thrown, if the class contains more than one occurrence of a constructor method.

A constructor can use the super keyword to call the constructor of a parent class.

If you don't specify a constructor method, a default constructor is used.

Examples

Using the constructor method

This code snippet is taken from the classes sample (live demo).

class Square extends Polygon {
  constructor(length) {
    // Here, it calls the parent class' constructor with lengths
    // provided for the Polygon's width and height
    super(length, length);
    // Note: In derived classes, super() must be called before you
    // can use 'this'. Leaving this out will cause a reference error.
    this.name = 'Square';
  }

  get area() {
    return this.height * this.width;
  }

  set area(value) {
    this.area = value;
  } 
}

Default constructors

If you don't specify a constructor method, a default constructor is used. For base classes, the default constructor is:

constructor() {}

For derived classes, the default constructor is:

constructor(...args) {
  super(...args);
}

Specifications

Specification Status Comment
{{SpecName('ES6', '#sec-static-semantics-constructormethod', 'Constructor Method')}} {{Spec2('ES6')}} Initial definition.
{{SpecName('ESDraft', '#sec-static-semantics-constructormethod', 'Constructor Method')}} {{Spec2('ESDraft')}}  

Browser compatibility

{{CompatibilityTable}}

Feature Chrome Firefox (Gecko) Internet Explorer Opera Safari
Basic support {{CompatChrome(42.0)}} {{CompatGeckoDesktop(45)}} {{CompatUnknown}} {{CompatUnknown}} {{CompatUnknown}}
Default constructors {{CompatUnknown}} {{CompatGeckoDesktop(45)}} {{CompatUnknown}} {{CompatUnknown}} {{CompatUnknown}}
Feature Android Android Webview Firefox Mobile (Gecko) IE Mobile Opera Mobile Safari Mobile Chrome for Android
Basic support {{CompatNo}} {{CompatChrome(42.0)}} {{CompatGeckoMobile(45)}} {{CompatUnknown}} {{CompatUnknown}} {{CompatUnknown}} {{CompatChrome(42.0)}}
Default constructors {{CompatUnknown}} {{CompatUnknown}} {{CompatGeckoMobile(45)}} {{CompatUnknown}} {{CompatUnknown}} {{CompatUnknown}} {{CompatUnknown}}

See also

Revision Source

<div>{{jsSidebar("Classes")}}</div>

<p>The <code>constructor</code> method is a special method for creating and initializing an object created with a <code>class</code>.</p>

<h2 id="Syntax">Syntax</h2>

<pre class="syntaxbox">
constructor([arguments]) { ... }</pre>

<h2 id="Description">Description</h2>

<p>There can only be one special method with the name "constructor" in a class. A {{jsxref("SyntaxError")}} will be thrown, if the class contains more than one occurrence of a <code>constructor</code> method.</p>

<p>A constructor can use the <code>super</code> keyword to call the constructor of a parent class.</p>

<p>If you don't specify a constructor method, a default constructor is used.</p>

<h2 id="Examples">Examples</h2>

<h3 id="Using_the_constructor_method">Using the <code>constructor</code> method</h3>

<p>This code snippet is taken from the <a href="https://github.com/GoogleChrome/samples/blob/gh-pages/classes-es6/index.html">classes sample</a> (<a href="https://googlechrome.github.io/samples/classes-es6/index.html">live demo</a>).</p>

<pre class="brush: js">
class Square extends Polygon {
  constructor(length) {
    // Here, it calls the parent class' constructor with lengths
    // provided for the Polygon's width and height
    super(length, length);
    // Note: In derived classes, super() must be called before you
    // can use 'this'. Leaving this out will cause a reference error.
    this.name = 'Square';
  }

  get area() {
    return this.height * this.width;
  }

  set area(value) {
    this.area = value;
  } 
}</pre>

<h3 id="Default_constructors">Default constructors</h3>

<p>If you don't specify a constructor method, a default constructor is used. For base classes, the default constructor is:</p>

<pre class="brush: js">
constructor() {}
</pre>

<p>For derived classes, the default constructor is:</p>

<pre class="brush: js">
constructor(...args) {
  super(...args);
}</pre>

<h2 id="Specifications">Specifications</h2>

<table class="standard-table">
 <tbody>
  <tr>
   <th scope="col">Specification</th>
   <th scope="col">Status</th>
   <th scope="col">Comment</th>
  </tr>
  <tr>
   <td>{{SpecName('ES6', '#sec-static-semantics-constructormethod', 'Constructor Method')}}</td>
   <td>{{Spec2('ES6')}}</td>
   <td>Initial definition.</td>
  </tr>
  <tr>
   <td>{{SpecName('ESDraft', '#sec-static-semantics-constructormethod', 'Constructor Method')}}</td>
   <td>{{Spec2('ESDraft')}}</td>
   <td>&nbsp;</td>
  </tr>
 </tbody>
</table>

<h2 id="Browser_compatibility">Browser compatibility</h2>

<p>{{CompatibilityTable}}</p>

<div id="compat-desktop">
<table class="compat-table">
 <tbody>
  <tr>
   <th>Feature</th>
   <th>Chrome</th>
   <th>Firefox (Gecko)</th>
   <th>Internet Explorer</th>
   <th>Opera</th>
   <th>Safari</th>
  </tr>
  <tr>
   <td>Basic support</td>
   <td>{{CompatChrome(42.0)}}</td>
   <td>{{CompatGeckoDesktop(45)}}</td>
   <td>{{CompatUnknown}}</td>
   <td>{{CompatUnknown}}</td>
   <td>{{CompatUnknown}}</td>
  </tr>
  <tr>
   <td>Default constructors</td>
   <td>{{CompatUnknown}}</td>
   <td>{{CompatGeckoDesktop(45)}}</td>
   <td>{{CompatUnknown}}</td>
   <td>{{CompatUnknown}}</td>
   <td>{{CompatUnknown}}</td>
  </tr>
 </tbody>
</table>
</div>

<div id="compat-mobile">
<table class="compat-table">
 <tbody>
  <tr>
   <th>Feature</th>
   <th>Android</th>
   <th>Android Webview</th>
   <th>Firefox Mobile (Gecko)</th>
   <th>IE Mobile</th>
   <th>Opera Mobile</th>
   <th>Safari Mobile</th>
   <th>Chrome for Android</th>
  </tr>
  <tr>
   <td>Basic support</td>
   <td>{{CompatNo}}</td>
   <td>{{CompatChrome(42.0)}}</td>
   <td>{{CompatGeckoMobile(45)}}</td>
   <td>{{CompatUnknown}}</td>
   <td>{{CompatUnknown}}</td>
   <td>{{CompatUnknown}}</td>
   <td>{{CompatChrome(42.0)}}</td>
  </tr>
  <tr>
   <td>Default constructors</td>
   <td>{{CompatUnknown}}</td>
   <td>{{CompatUnknown}}</td>
   <td>{{CompatGeckoMobile(45)}}</td>
   <td>{{CompatUnknown}}</td>
   <td>{{CompatUnknown}}</td>
   <td>{{CompatUnknown}}</td>
   <td>{{CompatUnknown}}</td>
  </tr>
 </tbody>
</table>
</div>

<h2 id="See_also">See also</h2>

<ul>
 <li><a href="/en-US/docs/Web/JavaScript/Reference/Operators/super">super()</a></li>
 <li><a href="/en-US/docs/Web/JavaScript/Reference/Operators/class"><code>class</code> expression</a></li>
 <li><a href="/en-US/docs/Web/JavaScript/Reference/Statements/class"><code>class</code> declaration</a></li>
 <li><a href="/en-US/docs/Web/JavaScript/Reference/Classes">Classes</a></li>
</ul>
Revert to this revision