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.

Thanks!!

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
}
3 Likes

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
8 Likes

Can I use this method?

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

instead of this?

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

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.

10 Likes

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

2 Likes

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

a = a - b
a -= b
6 Likes

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.

3 Likes

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

2 Likes

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.

5 Likes

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.

2 Likes

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.

    expression
    _____
a = a - 2
----------
statement
9 Likes

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.

Python Operators - w3resource

1 Like

Hi;
it would be a weird request, but is somewhere in google to find an easy application that used of JavaScript programming, to see how functions and methods and… works?
Because I’m learning abstractly, so are elusive, but real easy application would be very helpful.
gratefully
martinmiciciday

hi, a can’t understant where is the button for creat a new quastion post, so i put it to this.
Why it is dows not work:

takeVacationDays(daysOff) {
console.log(this._remainingVacationDays);
return this.remainingVacationDays -= daysOff;
}

Thanks!a

Hi there,

Is there a reason why we are not using a setter for the method takeVacationDays, in this exercise?

Thanks

A setter will arbitrarily set to the new value, with no reason given. takeVacationDays is an explicit method that needs no further description. It is self-describing.

Technically, we could have a setter, then invoke it from within the above method. It doesn’t change much…

  get remainingVacationDays() {
    return this._remainingVacationDays;
  }
  
  set remainingVacationDays(newValue) {
    this._remainingVacationDays = newValue;
  }

  takeVacationDays(daysOff) {
    this.remainingVacationDays = this.remainingVacationDays - daysOff;
  }

I have not tested this, so try it and see if there are any problems.

2 Likes