Path: blob/main/files/en-us/web/javascript/reference/classes/index.md
6514 views
------{{JsSidebar("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 unique to classes.
For more examples and explanations, see the Using classes guide.
Description
Defining classes
Classes are in fact "special functions", and just as you can define function expressions and function declarations, a class can be defined in two ways: a class expression or a class declaration.
Like function expressions, class expressions may be anonymous, or have a name that's different from the variable that it's assigned to. However, unlike function declarations, class declarations have the same temporal dead zone restrictions as let or const and behave as if they are not hoisted.
Class body
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.
The body of a class is executed in strict mode even without the "use strict" directive.
A class element can be characterized by three aspects:
Kind: Getter, setter, method, or field
Location: Static or instance
Visibility: Public or private
Together, they add up to 16 possible combinations. To divide the reference more logically and avoid overlapping content, the different elements are introduced in detail in different pages:
: Public instance method
: Public instance getter
: Public instance setter
: Public instance field
: Public static method, getter, setter, and field
: Everything that's private
Note: Private features have the restriction that all property names declared in the same class must be unique. All other public properties do not have this restriction — you can have multiple public properties with the same name, and the last one overwrites the others. This is the same behavior as in object initializers.
In addition, there are two special class element syntaxes: constructor and static initialization blocks, with their own references.
Constructor
The {{jsxref("Classes/constructor", "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 {{jsxref("SyntaxError")}} is 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.
You can create instance properties inside the constructor:
Alternatively, if your instance properties' values do not depend on the constructor's arguments, you can define them as class fields.
Static initialization blocks
Static initialization blocks allow flexible initialization of static properties, including the evaluation of statements during initialization, while granting access to the private scope.
Multiple static blocks can be declared, and these can be interleaved with the declaration of static fields and methods (all static items are evaluated in declaration order).
Methods
Methods are defined on the prototype of each class instance and are shared by all instances. Methods can be plain functions, async functions, generator functions, or async generator functions. For more information, see method definitions.
Static methods and fields
The {{jsxref("Classes/static", "static")}} keyword defines a static method or field for a class. Static properties (fields and methods) are defined on the class itself instead of each instance. Static methods are often used to create utility functions for an application, whereas static fields are useful for caches, fixed-configuration, or any other data that don't need to be replicated across instances.
Field declarations
With the class field declaration syntax, the constructor example can be written as:
Class fields are similar to object properties, not variables, so we don't use keywords such as const to declare them. In JavaScript, private features use a special identifier syntax, so modifier keywords like public and private should not be used either.
As seen above, the fields can be declared with or without a default value. Fields without default values default to undefined. By declaring fields up-front, class definitions become more self-documenting, and the fields are always present, which help with optimizations.
See public class fields for more information.
Private class features
Using private fields, the definition can be refined as below.
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.
Private fields can only be declared up-front in a field declaration. They cannot be created later through assigning to them, the way that normal properties can.
For more information, see private class features.
Inheritance
The {{jsxref("Classes/extends", "extends")}} keyword is used in class declarations or class expressions to create a class as a child of another constructor (either a class or a function).
If there is a constructor present in the subclass, it needs to first call super() before using this. The {{jsxref("Operators/super", "super")}} keyword can also be used to call corresponding methods of super class.
Examples
Binding this with instance and static methods
When a static or instance method is called without a value for {{jsxref("Operators/this", "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 is the same even if the {{jsxref("Strict_mode", ""use strict"")}} directive isn't present, because code within the class body is always executed in strict mode.
If we rewrite the above using traditional function-based syntax in non–strict mode, then this method calls are automatically bound to {{jsxref("globalThis")}}. In strict mode, the value of this remains as undefined.
Specifications
{{Specifications}}
Browser compatibility
{{Compat}}
See also
{{jsxref("Functions", "Functions", "", "true")}}
{{jsxref("Statements/class", "class declaration", "", "true")}}
{{jsxref("Operators/class", "class expression", "", "true")}}
{{jsxref("Classes/Public_class_fields", "Public class fields", "", "true")}}
{{jsxref("Classes/Private_class_fields", "Private class features", "", "true")}}
{{jsxref("Operators/super", "super")}}
Fields and public/private class properties proposal (stage 3)