Chapter 10. Bracket notation: flexible property names
This chapter covers
- Square brackets as an alternative to dot notation
- The use of square brackets to set and get properties
- The flexibility of square bracket notation
- How to build a working game in The Crypt
In chapter 3 you saw how to create objects with curly braces and get and set properties with dot notation. You’ve used objects to model players, places, planets, and posts as well as quizzes and calendar events. You’ve added functions as properties to make methods and passed your objects to and from functions as arguments and return values. Objects are the center of the JavaScript universe!
In this chapter you take a look at a new way of working with object properties that gives you more flexibility with the property names, lets you use variables as keys, and gives you the ability to generate new properties from data while programs are running.
We also bring part 1 of Get Programming with JavaScript to a close with a working version of The Crypt, finally giving players the chance to explore a map and collect treasure! Square bracket notation provides a better way for you to manage the links between locations in the game, create a web of Place objects, and add a touch of mystery to the adventure. Ooooo, mystery ...
Up until now you’ve used dot notation to set and get object properties.