Public, Private, Protected và Readonly modifiers trong Typescript là gì?

{{FormatNumbertoThousand(model.total_like)}} lượt thích
3.951 lượt xem

Public Modifier – Public là mặc đinh. Nó được truy xuất ở mọi nơi.

class Employee {

   empName: string;

   constructor(name: string) {

      this.empName = name;

   }

   salary(salary: number = 10000)

   {

      console.log('Hello, ' + this.empName + ' Your Salary -' + salary); 
   }

}

let empSal = new Employee("Anil");

console.log(empSal.salary()); console.log(empSal.salary(40000));

Private Modifier - Khi sử dụng Private modifier, chúng ta không thể truy xuất chúng từ ngoài class của nó.

Protected Modifier - Protected modifier giống như Private nhưng khác 1 điểm là nó có thể được truy xuất bởi instance của lớp kế thừa.

Readonly Modifier - Các thuộc tính Read-only phải đợi khởi tạo tại lúc khai báo chúng hoặc trong constructor.

Câu hỏi tiếp theo: Method Overriding là gì?
{{login.error}}