Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit 0561b90

Browse files
authored
Update class.md
1 parent 05f5e31 commit 0561b90

File tree

1 file changed

+6
-4
lines changed

1 file changed

+6
-4
lines changed

advanced/class.md

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -280,20 +280,22 @@ let a = new Animal('Jack');
280280
// index.ts(13,9): TS2674: Constructor of class 'Animal' is protected and only accessible within the class declaration.
281281
```
282282

283-
修饰符还可以使用在构造函数参数中,等同于类中定义该属性,使代码更简洁。
283+
### 参数属性
284+
285+
修饰符和`readonly`还可以使用在构造函数参数中,等同于类中定义该属性同时给该属性赋值,使代码更简洁。
284286

285287
```ts
286288
class Animal {
287289
// public name: string;
288290
public constructor (public name) {
289-
this.name = name;
291+
// this.name = name;
290292
}
291293
}
292294
```
293295

294296
### readonly
295297

296-
只读属性关键字,只允许出现在属性声明或索引签名中
298+
只读属性关键字,只允许出现在属性声明或索引签名或构造函数中
297299

298300
```ts
299301
class Animal {
@@ -316,7 +318,7 @@ a.name = 'Tom';
316318
class Animal {
317319
// public readonly name;
318320
public constructor(public readonly name) {
319-
this.name = name;
321+
// this.name = name;
320322
}
321323
}
322324
```

0 commit comments

Comments
 (0)