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.

1 Like

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?

1 Like

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.


Can you explain this further? I feel like I am missing something here or maybe I forgot something (or some things) from previous lessons. Why is this._remainingVacationDays - daysOff; an expression and this._remainingVacationDays -= daysOff; a statement? I feel like they should do the same thing. Is there something that I can further read to have a deeper understanding of this or maybe I need to be referred to a past lesson? I feel like I will have a lot of trouble with this in the future. I appreciate your response in advance.

An expression can be evaluated to a value. The above is indeed a value, but it is not assigned to anything so the value is never passed along.

-= is a two part operation.

a = 7

a -= 2

console.log(a)    // 5

The first operation is,

a - 2

The above is an expression.

a -= 2

is an assignment statement which assigns the value of the expression back to the original variable.

a = a - 2
1 Like

Thank you! I appreciate your response and I believe I have a better understanding from your explanation. So I am very grateful for that. I am not sure that was made exactly clear to me on previous lessons in the introduction to JavaScript course. I don’t even know what lesson to go back to in order to confirm that assertion but I certainly feel like I should revisit it in case there is something else I may have missed there. Otherwise I do feel quite confident in what I have learned so far. I just hate moments where I feel like I have a fundamental misunderstanding of something as I continue on my journey of learning and hopefully perfecting this scripting language.

You’re welcome!

Keep the docs close at hand so you can fill in the holes and have a quick reference when you’re not sure.


1 Like