File tree Expand file tree Collapse file tree 1 file changed +6
-4
lines changed Expand file tree Collapse file tree 1 file changed +6
-4
lines changed Original file line number Diff line number Diff line change @@ -280,20 +280,22 @@ let a = new Animal('Jack');
280
280
// index.ts(13,9): TS2674: Constructor of class 'Animal' is protected and only accessible within the class declaration.
281
281
```
282
282
283
- 修饰符还可以使用在构造函数参数中,等同于类中定义该属性,使代码更简洁。
283
+ ### 参数属性
284
+
285
+ 修饰符和` readonly ` 还可以使用在构造函数参数中,等同于类中定义该属性同时给该属性赋值,使代码更简洁。
284
286
285
287
``` ts
286
288
class Animal {
287
289
// public name: string;
288
290
public constructor (public name ) {
289
- this .name = name ;
291
+ // this.name = name;
290
292
}
291
293
}
292
294
```
293
295
294
296
### readonly
295
297
296
- 只读属性关键字,只允许出现在属性声明或索引签名中 。
298
+ 只读属性关键字,只允许出现在属性声明或索引签名或构造函数中 。
297
299
298
300
``` ts
299
301
class Animal {
@@ -316,7 +318,7 @@ a.name = 'Tom';
316
318
class Animal {
317
319
// public readonly name;
318
320
public constructor (public readonly name ) {
319
- this .name = name ;
321
+ // this.name = name;
320
322
}
321
323
}
322
324
```
You can’t perform that action at this time.
0 commit comments