There are currently no frequently asked questions associated with this exercise – that’s where you come in! You can contribute to this section by offering your own questions, answers, or clarifications on this exercise. Ask or answer a question by clicking reply () below.
If you’ve had an “aha” moment about the concepts, formatting, syntax, or anything else with this exercise, consider sharing those insights! Teaching others and answering their questions is one of the best ways to learn and stay sharp.
Join the Discussion. Help a fellow learner on their journey.
Ask or answer a question about this exercise by clicking reply () below!
Agree with a comment or answer? Like () to up-vote the contribution!
I don’t understand how and from where are we able to access name & department properties when in class we have used _name & _department as property names.
Please help me understand if i have any concept gap in between. TIA
Hello, just wondering why console.log(surgeonCurry._remainingVacationDays) doesn’t print 20 instead of 17, since in the preceding step we took 3 days off of the .remainingVacationDays (the getter)? I thought using a getter was supposed to be a way of preserving the original Object?
No, that is not what getters are for. Getters allow us to change the behavior of a property. For example, a property could have an array value, but we could use getter to return a string instead.
I added some setters to the code for this section and for some reason now when the takeVacationDays method does not seem to change the value of _remainingVacationDays:
class Surgeon {
constructor(name, department) {
this._name = name;
this._department = department;
this._remainingVacationDays = 20;
}
get name(){
if (typeof this._name === "string"){
return this._name;
} else{
console.log("Error: could not retrieve name")
}
}
set name (newName){
if (typeof newName === "string"){
this._name = newName;
}else{
console.log("Error: new name must be a string");
}
}
get department(){
if (typeof this._department === "string"){
return this._department;
} else{
console.log("Error: could not retrieve department")
}
}
set department (newDepartment){
if (typeof newDepartment === "string"){
this._department = newDepartment;
}else{
console.log("Error: new department must be a string");
}
}
get remainingVacationDays(){
if (typeof this._remainingVacationDays === "number"){
return this._remainingVacationDays;
} else{
console.log("Error: could not retrieve number of remaining vacation days")
}
}
set remainingVacationDays (newNumber){
if (typeof newNumber === "number"){
this._name = newNumber;
}else{
console.log("Error: new number of vacation days must be a number");
}
}
takeVacationDays(daysOff){
if (typeof daysOff === "number"){
this.remainingVacationDays = this.remainingVacationDays - daysOff;
}else{
console.log("Error: number of days off must be a number");
}
}
}
const surgeonCurry = new Surgeon('Curry', 'Cardiovascular');
const surgeonDurant = new Surgeon('Durant', 'Orthopedics');
console.log(surgeonCurry.name)
surgeonCurry.takeVacationDays(3)
console.log(surgeonCurry.remainingVacationDays)
this looks fine. (you can log if you want). So where does this take us? assignment will trigger the setter:
set remainingVacationDays (newNumber){
if (typeof newNumber === "number"){
this._name = newNumber;
}else{
console.log("Error: new number of vacation days must be a number");
}
}
it seems the setter doesn’t update the right property
Can anyone explain this?
Like we haven’t declared the properties anywhere in the class and what’s the point of adding _ in front of the property name anyway?
Can’t we use the normal variables for properties?
This is very confusing.
Someone please help .