← Back to index

Classes - JavaScript | MDN

Created: 2022-02-08 20:57  |  Source: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Classes

Search MDN

Classes

Classes are a template for creating objects. They encapsulate data with code to work on that data. Classes in JS are built on prototypes but also have some syntax and semantics that are not shared with ES5 class-like semantics.

Defining classes

Classes are in fact "special 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 ("Rectangle" here).

class Rectangle {
  constructor(height, width) {
    this.height = height;
    this.width = width;
  }
}

Hoisting

An important difference between function declarations and class declarations is that while functions can be called in code that appears before they are defined, classes must be defined before they can be constructed. Code like the following will throw a ReferenceError:

const p = new Rectangle(); // ReferenceError

class Rectangle {}

This occurs because while the class is hoisted its values are not initialized.

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. However, it can be accessed via the name property.

// unnamed
let Rectangle = class {
  constructor(height, width) {
    this.height = height;
    this.width = width;
  }
};
console.log(Rectangle.name);
// output: "Rectangle"

// named
let Rectangle = class Rectangle2 {
  constructor(height, width) {
    this.height = height;
    this.width = width;
  }
};
console.log(Rectangle.name);
// output: "Rectangle2"

Note: Class expressions must be declared before they can be used (they are subject to the same hoisting restrictions as described in the class declarations section).

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 constructor.

Strict mode

The body of a class is executed in strict mode, i.e., code written here is subject to stricter syntax for increased performance, some otherwise silent errors will be thrown, and certain keywords are reserved for future versions of ECMAScript.

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 the super class.

Static initialization blocks

Class static initialization blocks allow flexible initialization of class static properties including the evaluation of statements during initialization, and granting access to private scope.

Multiple static blocks can be declared, and these can be interleaved with the declaration of static properties and methods (all static items are evaluated in declaration order).

Prototype methods

See also method definitions.

class Rectangle {
  constructor(height, width) {
    this.height = height;
    this.width = width;
  }
  // Getter
  get area() {
    return this.calcArea();
  }
  // Method
  calcArea() {
    return this.height * this.width;
  }
}

const square = new Rectangle(10, 10);

console.log(square.area); // 100

Generator methods

See also Iterators and generators.

class Polygon {
  constructor(...sides) {
    this.sides = sides;
  }
  // Method
  *getSides() {
    for(const side of this.sides){
      yield side;
    }
  }
}

const pentagon = new Polygon(1,2,3,4,5);

console.log([...pentagon.getSides()]); // [1,2,3,4,5]

Static methods and properties

The static keyword defines a static method or property for a class. Static members (properties and methods) are called without instantiating their class and cannot be called through a class instance. Static methods are often used to create utility functions for an application, whereas static properties are useful for caches, fixed-configuration, or any other data you don't need to be replicated across instances.

class Point {
  constructor(x, y) {
    this.x = x;
    this.y = y;
  }

  static displayName = "Point";
  static distance(a, b) {
    const dx = a.x - b.x;
    const dy = a.y - b.y;

    return Math.hypot(dx, dy);
  }
}

const p1 = new Point(5, 5);
const p2 = new Point(10, 10);
p1.displayName; // undefined
p1.distance;    // undefined
p2.displayName; // undefined
p2.distance;    // undefined

console.log(Point.displayName);      // "Point"
console.log(Point.distance(p1, p2)); // 7.0710678118654755

Binding this with prototype and static methods

When a static or prototype method is called without a value for this, such as by assigning the method to a variable and then calling it, the this value will be undefined inside the method. This behavior will be the same even if the "use strict" directive isn't present, because code within the class body's syntactic boundary is always executed in strict mode.

class Animal {
  speak() {
    return this;
  }
  static eat() {
    return this;
  }
}

let obj = new Animal();
obj.speak(); // the Animal object
let speak = obj.speak;
speak(); // undefined

Animal.eat() // class Animal
let eat = Animal.eat;
eat(); // undefined

If we rewrite the above using traditional function-based syntax in non–strict mode, then this method calls are automatically bound to the initial this value, which by default is the global object. In strict mode, autobinding will not happen; the value of this remains as passed.

function Animal() { }

Animal.prototype.speak = function() {
  return this;
}

Animal.eat = function() {
  return this;
}

let obj = new Animal();
let speak = obj.speak;
speak(); // global object (in non–strict mode)

let eat = Animal.eat;
eat(); // global object (in non-strict mode)

Instance properties

Instance properties must be defined inside of class methods:

class Rectangle {
  constructor(height, width) {
    this.height = height;
    this.width = width;
  }
}

Field declarations

Public field declarations

With the JavaScript field declaration syntax, the above example can be written as:

class Rectangle {
  height = 0;
  width;
  constructor(height, width) {
    this.height = height;
    this.width = width;
  }
}

By declaring fields up-front, class definitions become more self-documenting, and the fields are always present.

As seen above, the fields can be declared with or without a default value.

See public class fields for more information.

Private field declarations

Using private fields, the definition can be refined as below.

class Rectangle {
  #height = 0;
  #width;
  constructor(height, width) {
    this.#height = height;
    this.#width = width;
  }
}

It's an error to reference private fields from outside of the class; they can only be read or written within the class body. By defining things that are not visible outside of the class, you ensure that your classes' users can't depend on internals, which may change from version to version.

Note: Private fields can only be declared up-front in a field declaration.

Private fields cannot be created later through assigning to them, the way that normal properties can.

For more information, see private class features.

Sub classing with extends

The extends keyword is used in class declarations or class expressions to create a class as a child of another class.

class Animal {
  constructor(name) {
    this.name = name;
  }

  speak() {
    console.log(`${this.name} makes a noise.`);
  }
}

class Dog extends Animal {
  constructor(name) {
    super(name); // call the super class constructor and pass in the name parameter
  }

  speak() {
    console.log(`${this.name} barks.`);
  }
}

let d = new Dog('Mitzie');
d.speak(); // Mitzie barks.

If there is a constructor present in the subclass, it needs to first call super() before using "this".

One may also extend traditional function-based "classes":

function Animal (name) {
  this.name = name;
}

Animal.prototype.speak = function () {
  console.log(`${this.name} makes a noise.`);
}

class Dog extends Animal {
  speak() {
    console.log(`${this.name} barks.`);
  }
}

let d = new Dog('Mitzie');
d.speak(); // Mitzie barks.

// For similar methods, the child's method takes precedence over parent's method

Note that classes cannot extend regular (non-constructible) objects If you want to inherit from a regular object, you can instead use Object.setPrototypeOf():

const Animal = {
  speak() {
    console.log(`${this.name} makes a noise.`);
  }
};

class Dog {
  constructor(name) {
    this.name = name;
  }
}

// If you do not do this you will get a TypeError when you invoke speak
Object.setPrototypeOf(Dog.prototype, Animal);

let d = new Dog('Mitzie');
d.speak(); // Mitzie makes a noise.

Species

You might want to return Array objects in your derived array class MyArray. The species pattern lets you override default constructors.

For example, when using methods such as map() that returns the default constructor, you want these methods to return a parent Array object, instead of the MyArray object. The Symbol.species symbol lets you do this:

class MyArray extends Array {
  // Overwrite species to the parent Array constructor
  static get [Symbol.species]() { return Array; }
}

let a = new MyArray(1,2,3);
let mapped = a.map(x => x * x);

console.log(mapped instanceof MyArray); // false
console.log(mapped instanceof Array);   // true

Super class calls with super

The super keyword is used to call corresponding methods of super class. This is one advantage over prototype-based inheritance.

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.`);
  }
}

let l = new Lion('Fuzzy');
l.speak();
// Fuzzy makes a noise.
// Fuzzy roars.

Mix-ins

Abstract subclasses or mix-ins are templates for classes. An ECMAScript class can only have a single superclass, so multiple inheritance from tooling classes, for example, is not possible. The functionality must be provided by the superclass.

A function with a superclass as input and a subclass extending that superclass as output can be used to implement mix-ins in ECMAScript:

let calculatorMixin = Base => class extends Base {
  calc() { }
};

let randomizerMixin = Base => class extends Base {
  randomize() { }
};

A class that uses these mix-ins can then be written like this:

class Foo { }
class Bar extends calculatorMixin(randomizerMixin(Foo)) { }

Re-running a class definition

A class can't be redefined. Attempting to do so produces a SyntaxError.

If you're experimenting with code in a web browser, such as the Firefox Web Console (Tools > Web Developer > Web Console) and you 'Run' a definition of a class with the same name twice, you'll get a SyntaxError: redeclaration of let ClassName;. (See further discussion of this issue in bug 1428672.) Doing something similar in Chrome Developer Tools gives you a message like Uncaught SyntaxError: Identifier 'ClassName' has already been declared at <anonymous>:1:1.

Specifications

Specification
ECMAScript Language Specification (ECMAScript)
# sec-class-definitions

Browser compatibility

Report problems with this compatibility data on GitHub
desktopmobileserver
ChromeEdgeFirefoxInternet ExplorerOperaSafariWebView AndroidChrome AndroidFirefox for AndroidOpera AndroidSafari on iOSSamsung InternetDenoNode.js
classes
Full support49
Full support13
Full support45
No supportNo
Full support36
Full support9
Full support49
Full support49
Full support45
Full support36
Full support9
Full support5.0
Full support1.0
Full support6.0.0
constructorFull support49
Full support13
Full support45
No supportNo
Full support36
Full support9
Full support49
Full support49
Full support45
Full support36
Full support9
Full support5.0
Full support1.0
Full support6.0.0
extendsFull support49
Full support13
Full support45
No supportNo
Full support36
Full support9
Full support49
Full support49
Full support45
Full support36
Full support9
Full support5.0
Full support1.0
Full support6.0.0
Private class fieldsFull support74
Full support79
Full support90
No supportNo
Full support62
Full support14.1
Full support74
Full support74
Full support90
Full support53
Full support14.5
Full support11.0
Full support1.0
Full support12.0.0
Private class fields 'in'Full support91
Full support91
Full support90
No supportNo
Full support77
Full support15
Full support91
Full support91
Full support90
Full support64
Full support15
Full support16.0
Full support1.9
No supportNo
Private class methodsFull support84
Full support84
Full support90
No supportNo
Full support70
Full support15
Full support84
Full support84
Full support90
Full support60
Full support15
Full support14.0
Full support1.0
Full support14.0.0
Public class fieldsFull support72
Full support79
Full support69
No supportNo
Full support60
Full support14.1
Full support72
Full support72
Full support79
Full support51
Full support14.5
Full support11.0
Full support1.0
Full support12.0.0
staticFull support49
Full support13
Full support45
No supportNo
Full support36
Full support14.1
Full support49
Full support49
Full support45
Full support36
Full support14.5
Full support5.0
Full support1.0
Full support6.0.0
Static class fieldsFull support72
Full support79
Full support75
No supportNo
Full support60
Full support14.1
Full support72
Full support72
Full support79
Full support51
Full support14.5
Full support11.0
Full support1.0
Full support12.0.0
Class static initialization blocksFull support94
Full support94
Full support93
No supportNo
Full support80
No supportNo
Full support94
Full support94
Full support93
No supportNo
No supportNo
No supportNo
Full support1.14
No supportNo

Legend

See also

Found a problem with this page?

Last modified: Jan 19, 2022, by MDN contributors

Change your languageSelect your preferred language

Related Topics

  1. JavaScript
  2. Tutorials:
  3. References:
  4. Expressions & operators
    1. Addition assignment (+=)
    2. Addition (+)
    3. Assignment (=)
    4. async function expression
    5. await
    6. Bitwise AND assignment (&=)
    7. Bitwise AND (&)
    8. Bitwise NOT (~)
    9. Bitwise OR assignment (|=)
    10. Bitwise OR (|)
    11. Bitwise XOR assignment (^=)
    12. Bitwise XOR (^)
    13. class expression
    14. Comma operator (,)
    15. Conditional (ternary) operator
    16. Decrement (--)
    17. delete operator
    18. Destructuring assignment
    19. Division assignment (/=)
    20. Division (/)
    21. Equality (==)
    22. Exponentiation assignment (**=)
    23. Exponentiation (**)
    24. function* expression
    25. Function expression
    26. Greater than or equal (>=)
    27. Greater than (>)
    28. Grouping operator ( )
    29. in operator
    30. Increment (++)
    31. Inequality (!=)
    32. instanceof
    33. Left shift assignment (<<=)
    34. Left shift (<<)
    35. Less than or equal (<=)
    36. Less than (<)
    37. Logical AND assignment (&&=)
    38. Logical AND (&&)
    39. Logical NOT (!)
    40. Logical nullish assignment (??=)
    41. Logical OR assignment (||=)
    42. Logical OR (||)
    43. Multiplication assignment (*=)
    44. Multiplication (*)
    45. new.target
    46. new operator
    47. Nullish coalescing operator (??)
    48. Object initializer
    49. Operator precedence
    50. Optional chaining (?.)
    51. Property accessors
    52. Remainder assignment (%=)
    53. Remainder (%)
    54. Right shift assignment (>>=)
    55. Right shift (>>)
    56. Spread syntax (...)
    57. Strict equality (===)
    58. Strict inequality (!==)
    59. Subtraction assignment (-=)
    60. Subtraction (-)
    61. super
    62. this
    63. typeof
    64. Unary negation (-)
    65. Unary plus (+)
    66. Unsigned right shift assignment (>>>=)
    67. Unsigned right shift (>>>)
    68. void operator
    69. yield*
    70. yield
  5. Errors
    1. Warning: -file- is being assigned a //# sourceMappingURL, but already has one
    2. TypeError: invalid Array.prototype.sort argument
    3. Warning: 08/09 is not a legal ECMA-262 octal constant
    4. RangeError: radix must be an integer
    5. SyntaxError: invalid regular expression flag "x"
    6. SyntaxError: return not in function
    7. TypeError: X.prototype.y called on incompatible type
    8. ReferenceError: can't access lexical declaration`X' before initialization
    9. TypeError: can't access property "x" of "y"
    10. TypeError: can't assign to property "x" on "y": not an object
    11. TypeError: can't define property "x": "obj" is not extensible
    12. TypeError: property "x" is non-configurable and can't be deleted
    13. TypeError: can't redefine non-configurable property "x"
    14. TypeError: cyclic object value
    15. TypeError: can't access dead object
    16. SyntaxError: applying the 'delete' operator to an unqualified name is deprecated
    17. ReferenceError: deprecated caller or arguments usage
    18. Warning: expression closures are deprecated
    19. SyntaxError: "0"-prefixed octal literals and octal escape seq. are deprecated
    20. SyntaxError: Using //@ to indicate sourceURL pragmas is deprecated. Use //# instead
    21. Warning: String.x is deprecated; use String.prototype.x instead
    22. Warning: Date.prototype.toLocaleFormat is deprecated
    23. SyntaxError: test for equality (==) mistyped as assignment (=)?
    24. TypeError: setting getter-only property "x"
    25. SyntaxError: Unexpected '#' used outside of class body
    26. SyntaxError: identifier starts immediately after numeric literal
    27. SyntaxError: illegal character
    28. TypeError: cannot use 'in' operator to search for 'x' in 'y'
    29. RangeError: invalid array length
    30. ReferenceError: invalid assignment left-hand side
    31. TypeError: invalid assignment to const "x"
    32. RangeError: invalid date
    33. SyntaxError: for-in loop head declarations may not have initializers
    34. SyntaxError: a declaration in the head of a for-of loop can't have an initializer
    35. TypeError: invalid 'instanceof' operand 'x'
    36. TypeError: 'x' is not iterable
    37. SyntaxError: JSON.parse: bad parsing
    38. SyntaxError: Malformed formal parameter
    39. URIError: malformed URI sequence
    40. SyntaxError: missing ] after element list
    41. SyntaxError: missing : after property id
    42. SyntaxError: missing } after function body
    43. SyntaxError: missing } after property list
    44. SyntaxError: missing formal parameter
    45. SyntaxError: missing = in const declaration
    46. SyntaxError: missing name after . operator
    47. SyntaxError: missing ) after argument list
    48. SyntaxError: missing ) after condition
    49. SyntaxError: missing ; before statement
    50. TypeError: More arguments needed
    51. RangeError: repeat count must be non-negative
    52. TypeError: "x" is not a non-null object
    53. TypeError: "x" has no properties
    54. SyntaxError: missing variable name
    55. TypeError: can't delete non-configurable array element
    56. RangeError: argument is not a valid code point
    57. TypeError: "x" is not a constructor
    58. TypeError: "x" is not a function
    59. ReferenceError: "x" is not defined
    60. RangeError: precision is out of range
    61. Error: Permission denied to access property "x"
    62. TypeError: "x" is read-only
    63. SyntaxError: redeclaration of formal parameter "x"
    64. TypeError: Reduce of empty array with no initial value
    65. SyntaxError: "x" is a reserved identifier
    66. RangeError: repeat count must be less than infinity
    67. Warning: unreachable code after return statement
    68. SyntaxError: "use strict" not allowed in function with non-simple parameters
    69. InternalError: too much recursion
    70. ReferenceError: assignment to undeclared variable "x"
    71. ReferenceError: reference to undefined property "x"
    72. SyntaxError: Unexpected token
    73. TypeError: "x" is (not) "y"
    74. SyntaxError: function statement requires a name
    75. SyntaxError: unterminated string literal