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 1091575 of class

  • Revision slug: Web/JavaScript/Reference/Statements/class
  • Revision title: class
  • Revision id: 1091575
  • Created:
  • Creator: deveedutta
  • Is current revision? No
  • Comment

Revision Content

{{jsSidebar("Statements")}}

The class declaration creates a new class with a given name using prototype-based inheritance.

You can also define a class using a {{jsxref("Operators/class", "class expression", "", 1)}}. But unlike the class expression, the class declaration doesn't allow an existing class to be declared again and will throw a type error if attempted. 

Syntax

class name [extends] {
  // class body
}

Description

Just like with class expressions, the class body of a class declaration is executed in strict mode. The constructor property is optional.

Class declarations are not {{Glossary("Hoisting", "hoisted")}} (unlike function declarations).

Examples

A simple class declaration

In the following example, we first define a class named Polygon, then extend it to create a class named Square. Note that super(), used in the constructor, can only be used in constructors and must be called before the this keyword can be used.

class Polygon {
  constructor(height, width) {
    this.name = 'Polygon';
    this.height = height;
    this.width = width;
  }
}

class Square extends Polygon {
  constructor(length) {
    super(length, length);
    this.name = 'Square';
  }
}

Attempting to declare a class twice

Re-declaring a class using the class declartion throws a type error.

class Foo {};
class Foo {}; // Uncaught TypeError: Identifier 'Foo' has already been declared

The same error is thrown when a class has been defined before using the class expression.

var Foo = class {};
class Foo {}; // Uncaught TypeError: Identifier 'Foo' has already been declared

Specifications

Specification Status Comment
{{SpecName('ES6', '#sec-class-definitions', 'Class definitions')}} {{Spec2('ES6')}} Initial definition.
{{SpecName('ESDraft', '#sec-class-definitions', 'Class definitions')}} {{Spec2('ESDraft')}}  

Browser compatibility

{{CompatibilityTable}}

Feature Chrome Firefox (Gecko) Internet Explorer Opera Safari
Basic support {{CompatChrome(42.0)}} {{CompatGeckoDesktop(45)}} {{CompatUnknown}} {{CompatUnknown}} {{CompatUnknown}}
Array subclassing {{CompatChrome(43.0)}} {{CompatNo}} {{CompatUnknown}} {{CompatUnknown}} {{CompatUnknown}}
Allowed in sloppy mode {{CompatChrome(49.0)}}        
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)}}
Array subclassing {{CompatNo}} {{CompatChrome(43.0)}} {{CompatNo}} {{CompatUnknown}} {{CompatUnknown}} {{CompatUnknown}} {{CompatChrome(43.0)}}
Allowed in sloppy mode {{CompatNo}} {{CompatChrome(49.0)}}         {{CompatChrome(49.0)}}

See also

Revision Source

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

<p>The <strong>class declaration</strong> creates a new class with a given name using prototype-based inheritance.</p>

<div class="noinclude">
<p>You can also define a class using a {{jsxref("Operators/class", "class expression", "", 1)}}.&nbsp;But unlike the class expression,&nbsp;the class declaration doesn't allow an existing&nbsp;class to be declared again and will throw a type error if attempted.&nbsp;</p>
</div>

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

<pre class="brush: js">
class <em>name</em> [extends] {
  // class body
}
</pre>

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

<p>Just like with class expressions, the class body of a class declaration&nbsp;is executed in <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Strict_mode">strict mode</a>. The constructor property is optional.</p>

<p>Class declarations are not {{Glossary("Hoisting", "hoisted")}} (unlike <a href="/en-US/docs/Web/JavaScript/Reference/Statements/function">function declarations</a>).</p>

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

<h3 id="A_simple_class_declaration">A simple class declaration</h3>

<p>In the following example, we first define a class named Polygon, then extend it to create a class named Square. Note that super(), used in the constructor, can only be used in constructors and must be called before the this keyword can be used.</p>

<pre class="brush: js">
class Polygon {
  constructor(height, width) {
    this.name = 'Polygon';
    this.height = height;
    this.width = width;
  }
}

class Square extends Polygon {
  constructor(length) {
    super(length, length);
&nbsp;   this.name = 'Square';
  }
}</pre>

<h3>Attempting to declare a class twice</h3>

<p>Re-declaring a class using the class declartion throws a type error.</p>

<pre class="brush: js">
class Foo {};
class Foo {}; // Uncaught TypeError: Identifier 'Foo' has already been declared
</pre>

<p>The same error is thrown when a class has been defined before using the class expression.</p>

<pre class="brush: js">
var Foo = class {};
class Foo {}; // Uncaught TypeError: Identifier 'Foo' has already been declared
</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-class-definitions', 'Class definitions')}}</td>
			<td>{{Spec2('ES6')}}</td>
			<td>Initial definition.</td>
		</tr>
		<tr>
			<td>{{SpecName('ESDraft', '#sec-class-definitions', 'Class definitions')}}</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>Array subclassing</td>
			<td>{{CompatChrome(43.0)}}</td>
			<td>{{CompatNo}}</td>
			<td>{{CompatUnknown}}</td>
			<td>{{CompatUnknown}}</td>
			<td>{{CompatUnknown}}</td>
		</tr>
		<tr>
			<td>Allowed in sloppy mode</td>
			<td>{{CompatChrome(49.0)}}</td>
			<td>&nbsp;</td>
			<td>&nbsp;</td>
			<td>&nbsp;</td>
			<td>&nbsp;</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>Array subclassing</td>
			<td>{{CompatNo}}</td>
			<td>{{CompatChrome(43.0)}}</td>
			<td>{{CompatNo}}</td>
			<td>{{CompatUnknown}}</td>
			<td>{{CompatUnknown}}</td>
			<td>{{CompatUnknown}}</td>
			<td>{{CompatChrome(43.0)}}</td>
		</tr>
		<tr>
			<td>Allowed in sloppy mode</td>
			<td>{{CompatNo}}</td>
			<td>{{CompatChrome(49.0)}}</td>
			<td>&nbsp;</td>
			<td>&nbsp;</td>
			<td>&nbsp;</td>
			<td>&nbsp;</td>
			<td>{{CompatChrome(49.0)}}</td>
		</tr>
	</tbody>
</table>
</div>

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

<ul>
	<li><a href="/en-US/docs/Web/JavaScript/Reference/Statements/function"><code>function</code> declaration</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/Classes">Classes</a></li>
</ul>
Revert to this revision