FAQ: Classes - Review: Classes

This community-built FAQ covers the “Review: Classes” exercise from the lesson “Classes”.

Paths and Courses
This exercise can be found in the following Codecademy content:

Web Development

Introduction To JavaScript

FAQs on the exercise Review: Classes

Join the Discussion. Help a fellow learner on their journey.

Ask or answer a question about this exercise by clicking reply (reply) below!

Agree with a comment or answer? Like (like) to up-vote the contribution!

Need broader help or resources? Head here.

Looking for motivation to keep learning? Join our wider discussions.

Learn more about how to use this guide.

Found a bug? Report it!

Have a question about your account or billing? Reach out to our customer support team!

None of the above? Find out where to ask other questions here!

3 posts were split to a new topic: Cannot push to a non-array

16 posts were split to a new topic: Assigning a value to a property without a setter

4 posts were split to a new topic: Is there still a purpose to factory functions or should we always use classes?

2 posts were split to a new topic: How do I use the super function?

7 posts were split to a new topic: How to overwrite a preset value in a child class

Hi!
I am working on the ‘Build a Library’ and as part of extended learning the exercise asks you to create a Cataog class that holds all Media objects. I created an array to hold this. How do I find the type of media in this array. I tried using “typeof” but it returns “object” and not “Book”, “CD” etc…

getTypeOfMedia(media) {
console.log(typeof media) ; //object
if (typeof media === ‘Book’)
return ‘Book’;
}

typeof is for deteriming JS data types, and has no relationship with this program The type of media is defined in the program.

Is there a type attribute defined in the classes? If so,

if (media.type === 'Book')

and so on.

Please post your class definition(s) so we can check this out further.

1 Like

This will probably work:

console.log(media.constructor.name)

Example:

class Dog {
  constructor(name, breed, age) {
    this.name = name
    this.breed = breed
    this.age = age
  }
}

const fido = new Dog('Fido', 'Basset Hound', '4')

console.log(fido.constructor.name)

Output:

Dog

See here for more infomation:

1 Like

thanks [midlindner] , the article helped! Here’s how i got it to work

printMediaType() {
this._mediaArr.forEach (
function(element) {
if (element instanceof Book){
console.log(${element} instance of Book);
} else if (element instanceof CD){
console.log(${element} instance of CD);
} else {
console.log(${element} typeof ${typeof element});
}
});
}

1 Like

To practice a bit more, I have tried to add an extra class Doctor that inherits from HospitalEmployee.

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

const doctorHofmann = new Doctor('Albert', true);
console.log(doctorHofmann.name);
console.log(doctorHofmann.insurance);

As a result I get the doctors name logged and an undefined…
I think it should log true… Can you see what I am missing?

Why should it give true? There is no insurance property, only _insurance. The _ is a naming convention that the property uses getters and/or setters. Your property doesn’t. Either make the property insurance or implement a getter.

I am puzzled by the following.

I wrote a new class object for Doctor with a method addInsurance(newInsurance)

I noticed that whether I use this.insurance.push(newInsurance) or this._insurance.push(newInsurance) in both cases the new insurance ‘health’ is still added to the array of insurances for this doctor.

I would expect that this.insurance.push(newInsurance) wouldn’t work since there is no property insurance in the class Doctor.

So why does it work?

Also why does it make no difference if you use the ‘return’ keyword or not in a method like addInsurance or addPassword?

this.insurance uses the getter, which you can see very easily if you where to add a console.log() in your getter

this is a good example where we could use a setter, we could do:

this.insurance = newInsurance

and then in the setter:

    set insurance(newInsurance) {
      this._insurance.push(newInsurance);
    }

now we changed the default assignment behavior to push to the array instead

i think its more important to understand what return does, could you explain it to me? Once you understand the how question, you should be able to answer the why question

Hi stetim94,

Did what you said and after adding the console.log() to the getter I get a TypeError on addInsurance with this.insurance.push(newInsurance). When I change it to this._insurance.push(newInsurance) it works fine again.

Now I’m left however with the following question and that is when do you create a setter and when a method when creating a class, since

set insurance(newInsurance) { this._insurance.push(newInsurance);}
and
addInsurance(newInsurance){this._insurance.push(newInsurance);}

…do exactly the same thing, albeit they’re called in a different way.

With regard to the return keyword, it stops function execution and returns a value to the function caller.
So, if I call addInsurance with an argument on an instance I’ve created I would expect to have to include the return keyword to have a value returned for storage in the object instance.

I still don’t understand why in some cases I have to use return and in others not. For example the following two methods:

takeVacationDays(daysOff) { //in this method it doesn’t matter if I use return or not
this._remainingVacationDays -= daysOff;
}

static generatePassword(){ //this method returns undefined if I don’t use return
return Math.floor(Math.random() * 10000);
}

Please clarify.

the console was purely for you, you could have added a simple:

console.log('getter called')

to see when the getter is called.

there isn’t a definitive answer here, its good to know both approaches. It can be that within a team you work, which approach to use is determined.

the unfortunate thing is that Javascript isn’t very strict, once we define a setter, we can still by pass the setter, which is shame.

If the setter was enforced (and couldn’t be bypassed), we could enforce certain rules, this can be useful when you write an library, that you can restrict certain actions on a property for other developers.

setting or updating a property rarely involves return the value. You can now easily access the property later, so no point in using return now.

a method that generates a password, i would expect a string/password to be returned.

I played around with this and added a Doctor…and it worked! I’m so excited that I am beginning to understand objects and classes! New to coding at age 52 :slight_smile:
https://gist.github.com/fcf0314f5cd82c8f06d12d2456e3dbad

2 Likes

I’m doing the extra practice and am adding the Doctor class that inherits from HospitalEmployee. For some reason it keeps throwing an error in my Doctor class and I don’t understand why. Here’s my code:

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() {
    return Math.floor(Math.random() * 10000);
  }
}


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 {
  construcctor(name, insurance) {
    super(name);
    this._insurance = insurance;
  }

  get insurance() {
    return this._insurance;
  }

  get remainingVacationDays() {
    return this._remainingVacationDays;
  }

  takeVacationDays(number) {
    this._remainingVacationDays = (this._remainingVacationDays - number);
  }
}


const nurseOlynyk = new Nurse('Olynyk', ['Trauma','Pediatrics']);


Here’s the error:

/home/ccuser/workspace/learn-javascript-classes-classes-static-methods/main.js:44
    super(name);
    ^^^^^
SyntaxError: 'super' keyword unexpected here

I don’t see the syntax problem…does anyone else see it?

Where would the super keyword be expected?
Inside a constructor, perhaps?

1 Like

I see it now, thanks!

1 Like