4

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.

2
  • Thinking about where you can take functions, or what you can do with them, is a very open-ended thing. Countless things are possible. In my opinion, rather than wondering where Javascript can take you, a better approach is to start with a specific goal and then figure out how to achieve that with Javascript. Commented Apr 12, 2016 at 23:51
  • Okay, thank you, I will try that. Commented Apr 12, 2016 at 23:57

1 Answer 1

1

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.

Sign up to request clarification or add additional context in comments.

1 Comment

Thank you Travis J, much appreciated. Sometimes I have trouble "expanding" the function examples.