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工作) –