FAQ: Classes - Methods


Hello All,

May i know in this excercise:

for this step:

can i use:

takeVacationDays(daysOff) {
this._remainingVacationDays = this._remainingVacationDays - daysOff;

and how about this?

takeVacationDays(daysOff) {
remainingVacationDays = remainingVacationDays - daysOff;

i guess i am still confused about the meaning of _x and x in getter and setter.



For the final step, I’m not sure how to call the method

takeVacationDays(daysOff) {
    this._remainingVacationDays -= this.daysOff

I created.


takeVacationDays(daysOff) {
   this._remainingVacationDays -= daysOff

First create an instance of the class.

const surgeonCurry = new Surgeon('Curry', 'Cardiovascular');

Then call the method to subtract days taken off…


and finally, poll the remaining days…

console.log(surgeonCurry.remainingVacationDays);    //  6

Can I use this method?

takeVacationDays(daysOff) {
    this._remainingVacationDays - daysOff;

instead of this?

takeVacationDays(daysOff) { 
this._remainingVacationDays -= daysOff

No, because that is an expression, not a statement, and nothing happens to the attribute value.

The second example IS a statement that assigns a new value to the attribute.


Ohhh I see!:grinning::grinning:
Thank you for the fast reply!

1 Like

Why is the following code does not work for me?
I tried to run:
this._remainingVacationDays-daysOff = this._remainingVacationDays;
Isn’t is supposed to be the same as:
this._remainingVacationDays -= daysOff


The evalauation should not be on the left side of the assignment.

a = a - b
a -= b
1 Like

Im doing number 2 and this is what i did.
constructor(name, department, remainingVacationDays) {
this._name = name;
this._department = department;
this._remainingVacationDays = 20;
Can anyone show me what i did wrong?


One very common error is a missing curly brace.


class Dog {
constructor(name) {
this._name = name;
this._behavior = 0;
why is there no behavior in the constructor parameter?


Because all new instances start out with that attribute initialized to 0. The intention would appear to be to have that variable accumulate over time.

1 Like