Classes Flashcards
Image you own a dog daycare and want to create a catalog of all the dogs who belong to the daycare, rather than writing an individual object for each one, what would be a better way to do so?
class Dog { constructor(name, age), this._name = name, this._age = age };
const dog1 = new Dog(‘Bosley’, 4);
If you have two classes that are similar, it is best to create child/sub classes to the parent class, if the sub classes share similar properties. How can you do this? (Give proper example, and at the bottom update one of the properties then log it to the console)
class HospitalEmployee { constructor(name) { this._name = name; this._remainingVacationDays = 20;
}
static generatePassword() { const password = Math.floor(Math.random() * 10000); return password; }
get name() { return this._name; }
get () {
return this._remainingVacationDays;
}
takeVacationDays(daysOff) {
this._remainingVacationDays = this._remainingVacationDays - daysOff;
}
}
class Nurse extends HospitalEmployee { constructor(name, certifications) { super(name) this._name = name; this._certifications = [];
}
get name() { return this._name;
}
get certifications() { return this._certifications }
addCertifications(certifications) {
this._certifications.push(certifications);
}
}
class Doctor extends HospitalEmployee { constructor(name, insurance) { super(name); this._name = name; this._insurance = insurance;
}
get name() { return this._name;
}
get insurance() { return this._insurance;
}
addInsurance(amount) {
this._insurance = this._insurance + amount;
} } const doc1 = new Doctor('Mohit Kumar', 0); doc1.addInsurance(2000) console.log(doc1._insurance);
How would you find the average from a list of arrays?
getAverageRating() { let ratingSum = this.rating.reduce((currentSum + rating) => currentSum + rating); return ratingSum / this._rating.length;
}
}
Which of the following problems do classes alleviate?
A. Classes make it easy to create multiple objects that share property names and methods.
B. Objects make it easy to create multiple classes that share properties and methods.
C. Classes make it easy to create multiple objects that share property names, but not methods.
D. Classes inherit properties and methods from objects that make it easy to create objects with the same property values, but different property names.
A. Classes make it easy to create multiple objects that share property names and methods.
What is the purpose of the super keyword?
The super keyword is used in subclasses to call a parent constructor()
How would you create an instance of the RentalUnit class, and save it to a variable called myRental?
class RentalUnit { constructor(address, cost) { this._address = address; this._cost = cost; }
get address() { return this._address; }
get cost() { return this._cost; }
calculateMonthly() {
return this.cost / 12;
}
}
const myRental = new RentalUnit(‘41 Park Lane’, 40000);
Why will the code below throw an error?
class RentalUnit { constructor(address, costPerYear) { this._address = address; this._costPerYear = costPerYear; }
get address() { return this._address; }
get costPerYear() { return this._costPerYear; }
calculateMonthly() {
return this.costPerYear / 12;
}
}
class Apartment extends RentalUnit { constructor(address, costPerYear, numberOfBedrooms) { this._numberOfBedrooms = numberOfBedrooms; super(address, costPerYear); }
get numberOfBedrooms() { return this._numberOfBedrooms; } }
const myApartment = new Apartment(‘1234 W 54th’, 22000, 3);
In the class Apartment, the super keyword must be called before the this keyword.