I'm currently studying classes in TypeScript (and I don't have that much experience in OOP).
The snippet below is from classes chapter in https://www.typescriptlang.org/docs/handbook/classes.html
Here is the thing:
I get that it should not be able to access the protected property "name" directly, as in console.log(howard.name)
and I know that it should be ok to access it through a method of a derived class.
I do get an error message when I compile this on TypeScript. But the fact is that when I open the resulting JS file on the browser (compiled), I can see the output in the Console, just like if it were a public property.
Is this normal in OOP? Why bother creating classes with protected/private attributes in TypeScript if they will be accessible anyway in the JS compiled file?
class Person {
protected name: string;
constructor(name: string) { this.name = name; }
}
class Employee extends Person {
private department: string;
constructor(name: string, department: string) {
super(name);
this.department = department;
}
public getElevatorPitch() {
return `Hello, my name is ${this.name} and I work in ${this.department}.`;
}
}
let howard = new Employee("Howard", "Sales");
console.log(howard.getElevatorPitch());
console.log(howard.name); // error