Welcome to collectivesolver - Programming & Software Q&A with code examples. A website with trusted programming answers. All programs are tested and work.

Contact: aviboots(AT)netvision.net.il

Buy a domain name - Register cheap domain names from $0.99 - Namecheap

Scalable Hosting That Grows With You

Secure & Reliable Web Hosting, Free Domain, Free SSL, 1-Click WordPress Install, Expert 24/7 Support

Semrush - keyword research tool

Boost your online presence with premium web hosting and servers

Disclosure: My content contains affiliate links.

39,950 questions

51,892 answers

573 users

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 159 views
1 answer 225 views
1 answer 122 views
1 answer 161 views
1 answer 212 views
212 views asked Jul 16, 2014 by avibootz
...