1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
|
---
title: Class static initialization blocks
slug: Web/JavaScript/Reference/Classes/Class_static_initialization_blocks
tags:
- Classes
- ECMAScript 2022
- JavaScript
- Language feature
- Static
- Reference
- Initialization
browser-compat: javascript.classes.class_static_initialization_blocks
---
{{jsSidebar("Classes")}}
**Class static initialization blocks** are a special feature of a {{jsxref("Statements/class", "class")}} that enable more flexible initialization of {{jsxref("Classes/static", "static")}} properties than can be achieved using per-field initialization.
Static blocks allow statements to be evaluated during initialization, which allows initializations that (for example) include `try...catch` or set multiple fields from a single value.
Initialization is performed in the context of the current class declaration, with privileged access to private state.
This means that static blocks can also be used to share information between classes with instance private fields and other classes or functions declared in the same scope (analogous to "friend" classes in C++).
{{EmbedInteractiveExample("pages/js/classes-static-initialization.html")}}
## Syntax
```js
static { ... }
```
## Description
A {{jsxref("Statements/class", "class")}} can have any number of `static {}` initialization blocks in its class body.
These are evaluated, along with any interleaved static field initializers, in the order they are declared.
Any static initialization of a super class is performed first, before that of its sub classes.
The scope of the variables declared inside the static block is local to the block.
Since `var`, `function`, `const` or `let` declared in a `static {}` initialization block are local to the block, any `var` declarations in the block are not hoisted.
```js
var y = 'Outer y';
class A {
static field = 'Inner y';
static {
var y = this.field;
}
}
// var defined in static block is not hoisted
console.log(y);
// > 'Outer y'
```
The `this` inside a static block refers to the constructor object of the class.
`super.<property>` can be used to access properties of a super class.
Note however that it is a syntax error to call {{jsxref("Operators/super", "super()")}} in a class static initialization block, or to attempt to access arguments of the class constructor function.
The scope of the static block is nested _within_ the lexical scope of the class body, and can access the private instance variables of the class.
A static initialization block may not have decorators (the class itself may).
## Examples
### Multiple blocks
The code below demonstrates a class with static initialization blocks and interleaved static field initializers.
The output shows that the blocks and fields are evaluated in execution order.
```js
class MyClass {
static field1 = console.log('field1 called');
static {
console.log('Class static block #1 called');
}
static field2 = console.log('field2 called');
static {
console.log('Class static block #2 called');
}
}
/*
> "field1 called"
> "Class static block #1 called"
> "field2 called"
> "Class static block #2 called"
*/
```
Note that any static initialization of a super class is performed first, before that of its sub classes.
### Using `this` and `super.property`
The `this` inside a static block refers to the constructor object of the class.
This code shows how to access a public static field.
```js
class A {
static field = 'A static field';
static {
var y = this.field;
}
}
```
The [`super.property`](/en-US/docs/Web/JavaScript/Reference/Operators/super) can be used inside a `static` block to reference properties of a super class.
This includes static properties, as shown below:
```js
class A {
static fieldA = 'A.fieldA';
}
class B extends A {
static {
let x = super.fieldA;
// 'A.fieldA'
}
}
```
### Access to private fields
This example below shows how access can be granted to the private object of a class from an object outside the class (example from the [v8.dev blog](https://v8.dev/features/class-static-initializer-blocks#access-to-private-fields)):
```js
let getDPrivateField;
class D {
#privateField;
constructor(v) {
this.#privateField = v;
}
static {
getDPrivateField = (d) => d.#privateField;
}
}
getDPrivateField(new D('private'));
// > private
```
### Workarounds
Prior to ES13 more complex static initialization might be achieved by using a static method that is called after the other properties to perform static initialization, or having a method that is external to the class that performs initialization tasks.
In both cases the approach is less elegant, and does not grant access to private methods in the class.
## Specifications
{{Specifications}}
## Browser compatibility
{{Compat}}
## See also
- [Class static initialization blocks](https://v8.dev/features/class-static-initializer-blocks) (v8.dev blog)
- [ES2022 feature: class static initialization blocks](https://2ality.com/2021/09/class-static-block.html) (2ality.com blog)
- [Classes](/en-US/docs/Web/JavaScript/Reference/Classes)
- {{jsxref("Operators/super", "super()")}}
- [Object.prototype.constructor](/en-US/docs/Web/JavaScript/Reference/Global_Objects/Object/constructor)
|