Vad är klasser i javascript
Classes
Classes are a template for creating objects. They encapsulate information with code to work on that uppgifter. 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 variabel that it's assigned to. However, unlike function declarations, class declarations have the same temporal dead zone restrictions as or and behave as if they are not hoisted.
Class body
The body of a class fryst vatten the part that fryst vatten in curly braces . This fryst vatten where you define class members, such as methods or constructor.
The body of a class fryst vatten executed in strict mode even without the directive.
A class element can be characterized bygd 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:
- Method definitions
Public instance method
- getter
Public instance getter
- setter
Public instance setter
- Public class fields
Public instance field
Public static method, getter, setter, and field
- Private properties
Everything that's private
Note: Private properties 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 gods one overwrites the others. This fryst vatten the same behavior as in object initializers.
In addition, there are two special class element syntaxes: and static initialization blocks, with their own references.
Constructor
The method fryst vatten 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 fryst vatten thrown if the class contains more than one occurrence of a method.
A constructor can use the keyword to call the constructor of the super class.
You can create instance properties inre 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 tillgång 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 bygd all instances. Methods can be plain functions, async functions, elektrisk maskin functions, or async elektrisk maskin functions. For more data, see method definitions.
Static methods and fields
The 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 uppgifter that doesn'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 to declare them. private properties use a special identifier syntax, so modifier keywords like and 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 . bygd 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 properties
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. bygd 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 utgåva to utgåva.
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 data, see private properties.
Inheritance
The keyword fryst vatten 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 fryst vatten a constructor present in the subclass, it needs to first call before using . The keyword can also be used to call corresponding methods of super class.
Evaluation order
When a declaration or expression fryst vatten evaluated, its various components are evaluated in the following order:
- The clause, if present, fryst vatten first evaluated. It must evaluate to a valid constructor function or , or a fryst vatten thrown.
- The method fryst vatten extracted, substituted with a default implementation if fryst vatten not present. However, because the definition fryst vatten only a method definition, this step fryst vatten not observable.
- The class elements' property keys are evaluated in the beställning of declaration. If the property key fryst vatten computed, the computed expression fryst vatten evaluated, with the value set to the value surrounding the class (not the class itself). None of the property values are evaluated yet.
- Methods and accessors are installed in the beställning of declaration. Instance methods and accessors are installed on the property of the current class, and static methods and accessors are installed on the class itself. Private instance methods and accessors are saved to be installed on the instance directly later. This step fryst vatten not observable.
- The class fryst vatten now initialized with the prototype specified bygd and implementation specified bygd . For all steps above, if an evaluated expression tries to tillgång the name of the class, a fryst vatten thrown because the class fryst vatten not initialized yet.
- The class elements' values are evaluated in the beställning of declaration:
- For each instance field (public or private), its initializer expression fryst vatten saved. The initializer fryst vatten evaluated during instance creation, at the början of the constructor (for base classes) or immediately before the call returns (for derived classes).
- For each static field (public or private), its initializer fryst vatten evaluated with set to the class itself, and the property fryst vatten created on the class.
- Static initialization blocks are evaluated with set to the class itself.
- The class fryst vatten now fully initialized and can be used as a constructor function.
For how instances are created, see the reference.
Examples
Binding this with instance and static methods
When a static or instance method fryst vatten called without a value for , such as bygd assigning the method to a variabel and then calling it, the value will be inre the method. This behavior fryst vatten the same even if the directive isn't present, because code within the body fryst vatten always executed in strict mode.
If we rewrite the above using traditional function-based syntax in non–strict mode, then method calls are automatically bound to . In strict mode, the value of remains as .
Specifications
Browser compatibility
BCD tables only load in the browser