1
0
mirror of https://github.com/ruanyf/es6tutorial.git synced 2025-05-24 10:22:23 +00:00

Merge pull request #1140 from w-l-l/patch-3

docs(class): fix typo
This commit is contained in:
Ruan YiFeng 2022-09-21 15:18:12 +08:00 committed by GitHub
commit 0eaf2d8ec2
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

View File

@ -947,11 +947,30 @@ class C {
上面代码中,类的内部有一个 static 代码块,这就是静态块。它的好处是将静态属性`y``z`的初始化逻辑,写入了类的内部,而且只运行一次。
每个类只能有一个静态块,在静态属性声明后运行。静态块的内部不能有`return`语句。
每个类允许有多个静态块,每个静态块中只能访问之前声明的静态属性。
```javascript
class C {
static x = 1;
static {
console.log(this.x, this.y, this.z); // 1 undefined undefined
}
static y = 2;
static {
console.log(this.x, this.y, this.z); // 1 2 undefined
}
static z = 3;
static {
console.log(this.x, this.y, this.z); // 1 2 3
}
}
```
静态块的内部不能有`return`语句。
静态块内部可以使用类名或`this`,指代当前类。
```c
```javascript
class C {
static x = 1;
static {