如何在調用makeBooking
方法時獲取預訂屬性。沒有得到理想的結果,我在做什麼錯誤學習JavaScript。如何使用該對象中的方法增加對象中的屬性javascript
var hotel = {
name: "pacific",
rooms: 40,
bookings: 35,
booked: 30,
roomType: ['deluxe', 'double', 'suite'],
pool: true,
gym: true,
checkAvailability: function() {
return this.rooms - this.booked;
},
makeBooking: function() {
var roomSpace = this.checkAvailability();
var addBooking = this.booked;
if (roomSpace > 0) {
addBooking = addBooking++;
console.log('room has been booked');
} else {
console.log('no room available');
}
}
};
console.log(hotel.checkAvailability());
var roomTypePush = hotel.roomType;
roomTypePush.push('rental');
console.log(roomTypePush);
console.log(hotel.booked);
console.log(hotel.makeBooking());
console.log(hotel.booked)
你可以做'this.booked ++;',而不是說'addBooked'變量 –
addBooking + = 1 – ambes
請使用this.booked ++。增加預訂指向一個只有this.booked值的新變量。 – Vatsal