How to use readonly member in class in TypeScript

1 Answer

0 votes
class Employee {
    readonly eID: number;
    eName: string;
    
    constructor(ID: number, name: string)     {
        this.eID = ID;
        this.eName = name;
    }

    display = () => console.log(this.eID + ' ' + this.eName )
}
let emp = new Employee(823473, 'HAL 9000');
emp.display();

// emp.eID = 383927; // Cannot assign to 'eID' because it is a read-only property.(2540)

emp.eName = 'IBM Watson'; 
console.log(emp.eName);



  
      
/*
      
run:
      
"823473 HAL 9000" 
"IBM Watson" 
     
*/

 



answered Oct 25, 2021 by avibootz

Related questions

1 answer 182 views
1 answer 272 views
1 answer 139 views
1 answer 182 views
1 answer 232 views
232 views asked Jul 16, 2014 by avibootz
...