I'm experimenting with a few aspects of Javascript's inheritance via a question I came across:
Create a reservation system which books airline seats or hotel rooms. It charges various rates for particular sections of the plane or hotel. Example, first class is going to cost more than coach. Hotel rooms have penthouse suites which cost more. Keep track of when rooms will be available and can be scheduled.
I'm working on the Hotel room problem.
I've decided to go with a single room base object with one data member called 'duration' which keeps track of the number of days the room is booked for, and two methods : book and checkout .
I have a derived object 'SingleRoom' which inherits from SingleRoom.
Here's the code :
function Room(duration){
this.duration = duration;
}
Room.prototype.book = ()=>{
if (this.prototype.count > 0){
this.prototype.count -= 1;
return true;
}
else {
return false;
}
}
Room.prototype.checkOut = ()=>{
this.prototype.count += 1;
console.log("Room checked out");
}
function SingleRoom(duration){
this.price = 1000;
}
SingleRoom.prototype = new Room();
SingleRoom.prototype.constructor = SingleRoom;
SingleRoom.prototype.count = 3;
var sr1 = new SingleRoom(2);
if(sr1.book() === false){
console.log("Could not book room");
}
I'm getting the following error at 'this.prototype.count' : Cannot read property count of undefined
Any idea what the issue is ?
Thanks!
this.count? Why do you define `count in thesubclass but use it in the superclass methods?this.prototype.*to access prototype properties.Room:SingleRoom.prototype = new Room();(that's not the correct way to do it, but that's what he/she's trying to do).