JavaScript Classes

How does the method (takeVacationDays(daysOff)) access the getter get (remainingVacationDays()) above? I thought you’d have to call the getter in the method to access the return value provided by the getter? This is step 6, the final step, in the exercise.

What makes you say it does? (it doesn’t)

Oh it’s accessing the property inthe constructor?

No, not that either.

It says in the code what it accesses.

(What does this refer to? (not the constructor))

No, it refers to the class Surgeon

It does not.
If it did, you could write:

Surgeon._remainingVacationDays, and that would be the same thing.
And it would be problematic if all surgeons shared vacation days. There’s only one Surgeon class, so that would be a bad place to store that information.

It refers to the parameters of the constructor?

Those no longer exist. That function has exited.

…to be fair that says a whole lot of not-relevant things before it gets to the point, the thing that is the whole point of this

When a function is called as a method of an object, its this is set to the object the method is called on.

Ya, which the object is described by the parameters of the constructor I thought

The arguments given to the constructor might describe the object, partially anyway. But they are not the object, and the object already exists at that point.

If you’re going to do things with your object, then you’ll probably want access to the object.

So where is the object, how is it referenced?

It’s referenced by the const surgeonCurry = new Surgeon(‘Curry’, ‘Cardiovascular’);

So, surgeonCurry is the name of the object in that case.

That’s even further in the future

this refers to the object

But how does the object even exist within the class until it is defined?

defined? are you talking about your variable? your variable isn’t the object, variables are names.

When you use the new operator with an object type

new sometype()

Then first an object is created based off a prototype, and then a constructor function gets called. When the constructor starts executing, it will have access to the object (how else would it be able to do things like setting properties on the object)

You can see this in your own constructor function. When your constructor “wants” to make changes to the object in question, it is using this to access the object.

‘this’ refers to an object, and that object has to have those two parameters within the constructor defined right because those are instance properties

Your object doesn’t have parameters (not sure that makes sense)

The values provided to your constructor are not part of the object, not unless you somehow bind them to a property of the object.

If you have an object and a bunch of values, then, sure, if you want, you can create properties on your object to refer to your values.

Your constructor has access to a bunch of values that it received.
And it has access to the object.
So if it wishes, it could do that.

oh you bind them with the this._name = name etc.

I have a question about the extra practice part. I’m trying to create a subclass ‘doctor’. I don’t get any errors until I make an instance of the subclass, and then it tells me that in the subclass section, a value is undefined (insurance). Here are the lines that throw the reference error:

lass Doctor extends HospitalEmployee { constructor(name) { super(name); this._insurance = insurance; } get insurance () { return this._insurance; } const doctorDave = new Doctor('Dave', 'Aetna')

Here is the link to the lesson: Classes | Codecademy

And here is all the code for that page:

class HospitalEmployee { constructor(name) { this._name = name; this._remainingVacationDays = 20; } get name() { return this._name; } get remainingVacationDays() { return this._remainingVacationDays; } takeVacationDays(daysOff) { this._remainingVacationDays -= daysOff; } static generatePassword () { const randomNum = Math.floor(Math.random()*10000); return randomNum; } } class Nurse extends HospitalEmployee { constructor(name, certifications) { super(name); this._certifications = certifications; } get certifications() { return this._certifications; } addCertification(newCertification) { this.certifications.push(newCertification); } } class Doctor extends HospitalEmployee { constructor(name) { super(name); this._insurance = insurance; } get insurance () { return this._insurance; } addInsurance (newInsurance) { } insuranceCheck (patientInsurance) { if (patientInsurance === { console.log(`${} accepts ${patientInsurance}!`) } else { console.log(`${} does not accept ${patientInsurance}, we apologize.`) } } acceptInsurance () { } } const nurseOlynyk = new Nurse('Olynyk', ['Trauma','Pediatrics']); nurseOlynyk.takeVacationDays(5); console.log(nurseOlynyk.remainingVacationDays); nurseOlynyk.addCertification('Genetics'); console.log(nurseOlynyk.certifications); console.log(HospitalEmployee.generatePassword()) const doctorDave = new Doctor('Dave', 'Aetna')

I appreciate any help I can get!

I think that you’re missing having insurance as a parameter of the constructor:

In the Doctor class, you have:

  constructor(name) {
    this._insurance = insurance;

but there’s no way for the constructor to set the value of the insurance variable here.

You probably want insurance as a parameter:

  constructor(name, insurance) {
    this._insurance = insurance;

Alternatively, you could do this:

  constructor(name, insurance) {
    this._insurance = [];

and then use a different method to set ._insurance

No need for the parameter if it is set to a default array.