FAQ: Classes - Methods

#1

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.

Thanks!!

#2

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

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

I created.


correct:

takeVacationDays(daysOff) {
   this._remainingVacationDays -= daysOff
}
#4

First create an instance of the class.

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

Then call the method to subtract days taken off…

surgeonCurry.takeVacationDays(14);

and finally, poll the remaining days…

console.log(surgeonCurry.remainingVacationDays);    //  6
2 Likes
#5

Can I use this method?

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

instead of this?

takeVacationDays(daysOff) { 
this._remainingVacationDays -= daysOff
 }
#6

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.

#7

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

1 Like
#8

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

#9

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

a = a - b
a -= b
1 Like
#10

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?

#11

One very common error is a missing curly brace.

#12

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

#13

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