Classes Flashcards

1
Q

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?

A
class Dog {
constructor(name, age),
this._name = name,
this._age = age
};

const dog1 = new Dog(‘Bosley’, 4);

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
2
Q

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)

A
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 well did you know this?
1
Not at all
2
3
4
5
Perfectly
3
Q

How would you find the average from a list of arrays?

A
getAverageRating() {
let ratingSum = this.rating.reduce((currentSum + rating) => currentSum + rating);
return ratingSum / this._rating.length;

}
}

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
4
Q

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

A. Classes make it easy to create multiple objects that share property names and methods.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
5
Q

What is the purpose of the super keyword?

A

The super keyword is used in subclasses to call a parent constructor()

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
6
Q

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

A

const myRental = new RentalUnit(‘41 Park Lane’, 40000);

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
7
Q

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

A

In the class Apartment, the super keyword must be called before the this keyword.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly