class Surgeon {
constructor(name, department) {
this._name = name;
this._department = department;
this._remainingVacationDays = 20;
}
get name(){
return this._name;
}
get department(){
return this._department;
}
get remainingVacationDays(){
return this._remainingVacationDays;
}
takeVacationDays(daysOff){
this._remainingVacationDays -= daysOff;
}
}
const surgeonCurry = new Surgeon('Curry', 'Cardiovascular');
const surgeonDurant = new Surgeon('Durant', 'Orthopedics');
console.log(surgeonCurry.name);
surgeonCurry.takeVacationDays(3);
console.log(surgeonCurry.remainingVacationDays());
As shown, I have a class which contains getters and methods. And outside of the block I logged some values which happen to have a dot notation syntax. So for example this: console.log(surgeonCurry.name);. I don’t get how like surgeonCurry DOT name gets it’s value by using the DOT, im not getting the function behind what the DOT does can someone explain it to me.
Pardon me as I am new and might get terms wrongly.
Thx alot in advance m8~~
A class doesn’t really exist in JavaScript, instead it’s used as syntactic sugar to create a prototypal structure.
In that example, name, department, and remainingVacationDays are all available as accessor methods of the Surgeon object, which are all accessed via the dot-notation method.
You can find more about classes over at the Classes documentation page.
So if you want to discourage using such internal pseudo-private properties directly (usually denoted by a leading underscore as you did), or if getting the value involves some further computations, you’d “traditionally” write a getter method: