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’;
}
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 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
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);
}
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’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: