#Day68 of 100daysofcode
Today was Good Friday, and as per the name, it did turn out good as well… It was a sunny day, we went for long walks, and dined out
It was a productive day as well… I was stressed out in the past 2-3 days, but today felt better. I continued with the Classes concept in Javascript and learned about inheritance and static methods. I also created a sample Library Catalog project which now I am thinking to add realm database in that as well
Review Notes on Classes
-
Classes are templates for objects.
-
Javascript calls a constructor method when we create a new instance of a class.
-
Inheritance is when we create a parent class with properties and methods that we can extend to child classes.
-
We use the
extends
keyword to create a subclass. -
The
super
keyword calls theconstructor()
of a parent class. -
Static methods are called on the class, but not on instances of the class.
Library Project
Description:
Books-‘N-Stuff carries three different types of media: books, CDs, and movies. In this project, create a parent class named Media
with three subclasses: Book
, Movie
, and CD
. These three subclasses have the following properties and methods:
Book
- Properties :
author
(string),title
(string),pages
(number),isCheckedOut
(boolean, initiallyfalse
), andratings
(array, initially empty). - Getters : all properties have a getter
- Methods :
.getAverageRating()
,.toggleCheckOutStatus()
, and.addRating()
Movie
- Properties :
director
(string),title
(string),runTime
(number),isCheckedOut
(boolean, initiallyfalse
), andratings
(array, initially empty) - Getters : all properties have a getter
- Methods :
.getAverageRating()
,.toggleCheckOutStatus()
, and.addRating()
CD
- Properties :
artist
(string),title
(string),isCheckedOut
(boolean, initiallyfalse
), andratings
(array, initially empty),songs
(array of strings) - Getters : all properties have a getter
- Methods :
.getAverageRating()
,.toggleCheckOutStatus()
, and.addRating()
Code
class Media {
constructor(title){
this._title = title
this._isCheckedOut = false
this._ratings = []
}
get title() {
return this._title;
}
get isCheckedOut() {
return this._isCheckedOut;
}
get ratings(){
return this._ratings;
}
set isCheckedOut(value){
this._isCheckedOut = value
}
toggleCheckOutStatus() {
this._isCheckedOut = !this._isCheckedOut;
}
getAverageRating() {
let ratingsSum = this.ratings.reduce((currentSum, rating) => currentSum + rating, 0);
const ratingLength = this.ratings.length;
return ratingsSum/ratingLength;
}
addRating(rating){
this._ratings.push(rating);
}
}
class Book extends Media{
constructor(title, author, pages){
super(title)
this._author = author
this._pages = pages
}
get author() {
return this._author
}
get pages() {
return this._pages;
}
}
class Movie extends Media{
constructor(title, director, runtime){
super(title)
this._director = director
this._runtime = runtime
}
get director(){
return this._director;
}
get runtime(){
return this._runtime;
}
}
const historyOfEverything = new Book('A Short History of Nearly Everything', 'Bill Bryson', 544)
historyOfEverything.toggleCheckOutStatus()
console.log(historyOfEverything.isCheckedOut)
historyOfEverything.addRating(4)
historyOfEverything.addRating(5)
historyOfEverything.addRating(5)
console.log(historyOfEverything.getAverageRating())
const speed = new Movie('Speed', 'Jan de Bont', 116)
speed.toggleCheckOutStatus()
console.log(speed.isCheckedOut)
speed.addRating(1);
speed.addRating(1);
speed.addRating(5)
console.log(speed.getAverageRating())
I also finished Book Keeper App showing how embedded objects are created in Realm Schema and how they get synced to Atlas
Until tomorrow,