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');
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 
286288class  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 
299301class  Animal  {
@@ -316,7 +318,7 @@ a.name = 'Tom';
316318class  Animal  {
317319    //  public readonly name;
318320    public  constructor (public  readonly  name ) {
319-         this .name  =  name ;
321+         //   this.name = name;
320322    }
321323}
322324``` 
    
 
   
 
     
   
   
          
     
  
    
     
 
    
      
     
 
     
    You can’t perform that action at this time.
  
 
    
  
     
    
      
        
     
 
       
      
     
   
 
    
    
  
 
  
 
     
    
0 commit comments