Reputation:
I just started reading JavaScript and JQuery by John Duckett, and I really enjoy the book so far. In the book, I'm at the part where functions are introduced, and I'm kind of lost. What are some cool things I can do with simple object literal functions. I get how to create a simple function, but the book never really gives other examples of what I can create with these functions. For example,
var hotel = {
name: "Quay",
rooms: 40,
booked: 25,
checkAvailability: function() {
return this.rooms - this.booked;
}
};
I try to branch out from that, but I keep drawing a blank on where I can take these functions. I'm a beginner with JavaScript, so I don't know too much about the language.
Upvotes: 4
Views: 57
Reputation: 82277
In general, what those functions represent are behaviors. The example you are seeing in the book is showing the behavior of the hotel to check availability. This is possible because the metrics for availability already exist.
In order to have more behavior, more metrics need to exist. So if you would consider an expanded example where rooms are also objects, then the behaviors could also be expanded.
For example, if rooms were an array of objects with prices. Then the metrics for availability could check against pricing as well, there could also be sorting, etc.
Further expansion of this approach would lead to an actual application that could handle booking with a full calendar, reservations, payments, etc.
The important aspect to look at once you can imagine the structure shown here is layers of those objects. The more complex the layering and structure becomes, the easier it will be to identify behaviors of that structure.
Upvotes: 1