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 845707 of class expression

  • Revision slug: Web/JavaScript/Reference/Operators/class
  • Revision title: class expression
  • Revision id: 845707
  • Created:
  • Creator: fscholz
  • Is current revision? No
  • Comment fix compat parsing error

Revision Content

{{Harmony}} {{jsSidebar("Operators")}}

The class expression is one way define a class in ECMAScript 6. Similar to function expressions, class expressions can be named or unnamed. If named, the name of the class is local the class body only. JavaScript classes are using prototype-based inheritance.

Syntax

var MyClass = class [className] [extends] {
  // class body
};

Description

A class expression has a similar syntax to a class statement. However, with class expressions, you are able to omit the class name ("binding identifier"), which you can't with class statements.

Just like with class statements, the class body of class expressions is executed in strict mode.

Examples

A simple class expression

This is just a simple anonymous class expression which you can refer to using the variable "Foo".

var Foo = class {
  constructor() {}
  bar() {
    return "Hello World!";
  }
};

var instance = new Foo();
instance.bar(); // "Hello World!"

Named class expressions

If you want to refer to the current class inside the class body, you can create a named class expression. This name is only visible in the scope of the class expression itself.

// TBD
var Foo = class NamedFoo {
  
}

Specifications

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

Browser compatibility

{{CompatibilityTable}}

Feature Chrome Firefox (Gecko) Internet Explorer Opera Safari
Basic support {{CompatChrome(42.0)}} {{CompatNightly("firefox")}} {{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)}} {{CompatNightly("firefox")}} {{CompatUnknown}} {{CompatUnknown}} {{CompatUnknown}} {{CompatChrome(42.0)}}

See also

Revision Source

<div>{{Harmony}} {{jsSidebar("Operators")}}</div>

<p>The <strong>class expression</strong> is one way define a class in ECMAScript 6. Similar to <a href="/en-US/docs/Web/JavaScript/Reference/Operators/function">function expressions</a>, class expressions can be named or unnamed. If named, the name of the class is local the class body only. JavaScript classes are using prototype-based inheritance.</p>

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

<pre class="syntaxbox">
var MyClass = class <em>[className]</em> [extends] {
&nbsp; // class body
};</pre>

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

<p>A class expression has a similar syntax to a <a href="/en-US/docs/Web/JavaScript/Reference/Statements/class">class statement</a>. However, with class expressions, you are able to omit the class name ("binding identifier"), which you can't with class statements.</p>

<p>Just like with class statements, the class body of class expressions is executed in <a href="/en-US/docs/Web/JavaScript/Reference/Strict_mode">strict mode</a>.</p>

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

<h3 id="A_simple_class_expression">A simple class expression</h3>

<p>This is just a simple anonymous class expression which you can refer to using the variable "Foo".</p>

<pre class="brush: js">
var Foo = class {
  constructor() {}
  bar() {
    return "Hello World!";
  }
};

var instance = new Foo();
instance.bar(); // "Hello World!"
</pre>

<h3 id="Named_class_expressions">Named class expressions</h3>

<p>If you want to refer to the current class inside the class body, you can create a named class expression. This name is only visible in the scope of the class expression itself.</p>

<pre class="brush: js">
// TBD
var Foo = class NamedFoo {
  
}
</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>
 </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>{{CompatNightly("firefox")}}</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>{{CompatNightly("firefox")}}</td>
   <td>{{CompatUnknown}}</td>
   <td>{{CompatUnknown}}</td>
   <td>{{CompatUnknown}}</td>
   <td>{{CompatChrome(42.0)}}</td>
  </tr>
 </tbody>
</table>
</div>

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

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