i try to access this function in my Object with the console.log but i don't really understand why i can't access it! I'm beginning Javascript but i'm really stuck with accessing functions in Object.
Thanks for the help
const hotel = {
name: "Grand Hotel",
location: "Stockholm",
pricePerNight: 2200,
roomBooked: 23,
totalRoom: 223,
roomAvailable: function(){
return this.totalRoom - this.roomBooked;
}
};
hotel.roomAvailable();
console.log(hotel.roomAvailable);
asked Nov 12, 2018 at 19:15
user10404269
3 Answers 3
You're just missing the parentheses in the log function:
hotel.roomAvailable()
You are already invoking the function which will return the value. Just log the return value instead.
In your code you are just logging the definition of the function but not really invoking it.
const hotel = {
name: "Grand Hotel",
location: "Stockholm",
pricePerNight: 2200,
roomBooked: 23,
totalRoom: 223,
roomAvailable: function() {
return this.totalRoom - this.roomBooked;
}
};
var isRoomAvailable = hotel.roomAvailable();
console.log(isRoomAvailable);
answered Nov 12, 2018 at 19:18
Sushanth --
55.8k9 gold badges70 silver badges109 bronze badges
Comments
Is this what you want?
const hotel = {
name: "Grand Hotel",
location: "Stockholm",
pricePerNight: 2200,
roomBooked: 23,
totalRoom: 223,
roomAvailable: function(){
return this.totalRoom - this.roomBooked;
}
};
console.log(hotel.roomAvailable());
answered Nov 12, 2018 at 19:19
SakoBu
4,0311 gold badge18 silver badges35 bronze badges
Comments
lang-js
console.log(hotel.roomAvailable());?