Codecademy Forums

FAQ: Classes - Review: Classes

Hello! I am wondering if classes make the ‘factory functions’ we learned during the objects lesson obsolete? Is there a time where it is more appropriate to use a factory function? Or should we generally always use classes to build multiple objects?

Obsolete, no. Special purpose, yes.

1 Like

I came across this article which you may find interesting.

Classes and factory functions each have their own pros and cons, so it’s up to you to decide which to use in a given situation. The writer of the linked article is firmly in favor of factory functions over classes. I, myself, remain undecided. :slightly_smiling_face:

1 Like

What we should do is keep an open mind. Consider what the long term goal is, then decide which is most expedient and simple to justify.

Personally, a class makes sense when we are working with a lot of objects with the same general makeup, same properties, shared methods, inheritable, &c. A factory makes sense when we want a special function with a variable constant that is passed to the factory and constant in the returned function. These returned functions are disposable when we’re done working with them.

1 Like

Repost - replying to the thread rather than a previous reply.

Just a very quick question on the super function. When we call it, we should call it ONCE at the beginning of the body of constructor() and pass in the arguments that are needed in the constructor() of the superclass (parent class) right?

For example, instead of just super(name) like in the exercises, we should call super(name, age, gender) if those were parameters in the parent class?

Thanks!

Yes. Typically you would pass an argument for each property in the constructor of the parent class that requires a parameter. Some properties may be assigned arbitrarily by the constructor as we saw with the number of vacation days in the exercise.

Hello, so I created an instance, doc of class Doctor , and when I created it I passed it 14 for the remainingVacationDays property .
Why is it, when I log doc.remainingVacationDays it prints out 20 ? Shoudn’t it log 14 ? I know 20 comes from the parent’s class constructor, but why can’t I overwrite this number in the instance ?

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;
  }
}

class Doctor extends HospitalEmployee{
 constructor(name,remainingVacationDays,insurance){
    super(name,remainingVacationDays);
    this._insurance = insurance;
  } 
}

const doc = new Doctor('doc',14,true); 
console.log(doc.remainingVacationDays); // Logs 20 .

Now , I know if I write Doctor class constructor like below it logs 14, but what if I want to log out the value in the instance ?

class Doctor extends HospitalEmployee{
  constructor(name,remainingVacationDays,insurance){
    super(name);
    this._remainingVacationDays = 14;
    this._insurance = insurance;
}
  }

Hello, @micro1086421766. Welcome to the forum.

Your constructor only takes one parameter, name. Adding a second argument when you instantiate a new doctor object does nothing. The first argument becomes the value of the name parameter, and anything after that argument is ignored. You could modify your constructor to take a vacation days parameter, and then assign that value instead of the arbitrary number 20.

I believe it is in the instructions to write that property into the constructor with a preset value of 20.

As for it doing nothing, that is not the case. It is an instance variable whether given in the parameters or hard coded into the constructor.

const nurseOlynyk = new Nurse('Olynyk', ['Trauma','Pediatrics']);
nurseOlynyk.takeVacationDays(5);
console.log(nurseOlynyk.remainingVacationDays);
nurseOlynyk.addCertification('Genetics');
console.log(nurseOlynyk.certifications);

Output

15
[ 'Trauma', 'Pediatrics', 'Genetics' ]

Yes. The instructions do call for the preset value of 20. I was referring to his Doctor class that he added in addition to the exercise instructions:

Passing the additional remainingVacationDays parameter to the parent constructor that only accepts the name parameter does not overwrite the assignment of 20 to the remainingVacationDays property.

The instructions stipulate having that property in a Doctor class constructor.

Doctor

  • properties: _name , _remainingVacationDays (set to 20 inside constructor() ), _insurance
  • methods: .takeVacationDays()

Yes, as extra practice. The OP is trying to overwrite the arbitrary assignment of 20 remainingVacationDays by passing 14 as an additional argument. The HospitalEmployee constructor does not take a remainingVacationDays parameter, so passing the argument does nothing, and 20 days is assigned. I was explaining that it is possible to re-write the constructor to take a remainingVacationDays parameter if he wanted to since he’s already passed all of the required steps.

1 Like

Ah, so the concern has been passing it to super, and somehow that escaped me. D’oh!

The course author is recommending that we override the super attribute, and instantiate it within this class. The attribute will be present, but not inheriting from the class.

Given the context, that would explain why the Doctor has its own .takeVacationDays() method, since the Doctor class attribute would not be in parent context if we ran the inherited method.

My silly question now is, “can we trick the method of the parent class to act upon the override value our subclass has on the attribute?” Needs testing in an unambiguous environment.

1 Like