This is a new technology, part of the ECMAScript 2015 (ES6) standard.
This technology's specification has been finalized, but check the compatibility table for usage and implementation status in various browsers.
Draft
This page is not complete.
JavaScript classes are introduced in ECMAScript 6 and are syntactical sugar over JavaScript's existing prototype-based inheritance. The class syntax is not introducing a new object-oriented inheritance model to JavaScript. JavaScript classes provide a much simpler and clearer syntax to create objects and deal with inheritance.
Defining classes
Classes are in fact functions, and just as you can define function expressions and function declarations, the class syntax has two components: class expressions and class declarations.
Class declarations
One way to define a class is using a class declaration. To declare a class, you use the class
keyword with the name of the class ("Polygon" here).
class Polygon { constructor(height, width) { this.height = height; this.width = width; } }
Hoisting
An important difference between function declarations and class declarations is that function declarations are hoisted and class declarations are not. You first need to declare your class and then access it, otherwise code like the following will throw a ReferenceError
:
var p = new Polygon(); // ReferenceError class Polygon {}
Class expressions
A class expression is another way to define a class. Class expressions can be named or unnamed. The name given to a named class expression is local to the class's body.
// unnamed var Polygon = class { constructor(height, width) { this.height = height; this.width = width; } }; // named var Polygon = class Polygon { constructor(height, width) { this.height = height; this.width = width; } };
Class body and method definitions
The body of a class is the part that is in curly brackets {}
. This is where you define class members, such as methods or constructors.
Strict mode
The bodies of class declarations and class expressions are executed in strict mode.
Constructor
The constructor
method is a special method for creating and initializing an object created with a class
. There can only be one special method with the name "constructor" in a class. A 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.
Prototype methods
See also method definitions.
class Polygon { constructor(height, width) { this.height = height; this.width = width; } get area() { return this.calcArea() } calcArea() { return this.height * this.width; } }
Static methods
The static
keyword defines a static method for a class. Static methods are called without instantiating their class nor are they callable when the class is instantiated. Static methods are often used to create utility functions for an application.
class Point { constructor(x, y) { this.x = x; this.y = y; } static distance(a, b) { const dx = a.x - b.x; const dy = a.y - b.y; return Math.sqrt(dx*dx + dy*dy); } } const p1 = new Point(5, 5); const p2 = new Point(10, 10); console.log(Point.distance(p1, p2));
Sub classing with extends
The extends
keyword is used in class declarations or class expressions to create a class with a child of another class.
class Animal { constructor(name) { this.name = name; } speak() { console.log(this.name + ' makes a noise.'); } } class Dog extends Animal { speak() { console.log(this.name + ' barks.'); } }
Sub classing built-in objects
TBD
Super class calls with super
The super
keyword is used to call functions on an object's parent.
class Cat { constructor(name) { this.name = name; } speak() { console.log(this.name + ' makes a noise.'); } } class Lion extends Cat { speak() { super.speak(); console.log(this.name + ' roars.'); } }
ES5 inheritance syntax and ES6 classes syntax compared
TBD
Examples
TBD
Specifications
Specification | Status | Comment |
---|---|---|
ECMAScript 2015 (6th Edition, ECMA-262) The definition of 'Class definitions' in that specification. |
Standard | Initial definition. |
Browser compatibility
Feature | Chrome | Firefox (Gecko) | Internet Explorer | Opera | Safari |
---|---|---|---|---|---|
Basic support | 42.0 | Nightly build | ? | Not supported | 9.0 |
Feature | Android | Firefox Mobile (Gecko) | IE Mobile | Opera Mobile | Safari Mobile | Chrome for Android |
---|---|---|---|---|---|---|
Basic support | Not supported | Nightly build | ? | ? | ? | 42.0 |