From a technical perspective, it is important to understand that in Javascript, access modifiers like private and readonly do not exist at runtime. These are features present in Typescript to assist developers.
Here's an example of how Typescript handles access modifiers:
class Car {
private ownerName: string;
readonly vehicleIdNumber: string;
mileage: number;
}
let car = new Car();
//typescript compilation error:
// Property 'ownerName' is private and only accessible within class 'Car'.
car.ownerName = "test";
//typescript compilation error:
// Cannot assign to 'vehicleIdNumber' because it is a read-only property.
car.vehicleIdNumber = "543798";
//No error here!
car.mileage = 10000;
When this code is converted to Javascript, the access modifiers disappear and you can use those attributes freely. Here's what the resulting Javascript code would be:
class Car {
}
let car = new Car();
car.ownerName = "test";
car.vehicleIdNumber = "543798";
car.mileage = 10000;
In terms of Angular, using private or readonly on an @Input() property may not be practical.
Private members should typically only be accessed within the class they are declared in. Trying to access them outside the class violates this rule when dealing with components.
Readonly members are usually not meant to be modified after construction, but in Angular, @Input() properties are set after construction yet before ngOnInit().