I'm using TypeScript 3.4.5 and I have declared a class Person like following and created its instance person:
class Person {
name: string;
private personType: string = "Awesome Guy";
protected age: number = 27;
constructor(name: string, public userName: string) {
this.name = name;
}
}
const person = new Person("John", "john");
console.log(person); // {"userName":"john","personType":"Awesome Guy","age":27,"name":"John"}
But, when I printed the value of person object, it displayed values of private and protected members also. I can even access them like person.personType and person.age (though compiler gives error), it prints correct values and not undefined. What's wrong with this? Please help to clarify the doubt.