function Hotel(name,rooms,bookings){
this.name = name;
this.rooms = rooms;
this.bookings = bookings;
this.checkAvailability = function(){
return this.rooms - this.bookings;
}
this.bookRoom = function(){
if(this.checkAvailability() > 1){
return this.bookings++;
}
}
this.cancelBooking = function(){
if(this.bookings < 1){
return this.bookings--;
}
}
}
var grandHotel = new Hotel('Hotel Grand', 20, 5);
var addBooking = document.getElementById("book");
addBooking.addEventListener('click', grandHotel.bookRoom, false);
如果我點擊addBooking元素我得到這個錯誤:
Uncaught TypeError: this.checkAvailability is not a function.
的可能的複製[如何進行 「this」 關鍵字的工作?](http://stackoverflow.com/questions/3127429/how-does-the-this-keyword工作) –