Chapter 7. Object arguments: functions working with objects

 

This chapter covers

  • Using objects as arguments
  • Accessing object properties from within a function
  • Adding new object properties from within a function
  • Returning objects from functions
  • Setting functions as properties of objects

This is the last of the run of four chapters introducing functions. By now, you know about using functions to execute code on demand and passing information to and from functions using parameters, arguments, and the return keyword. You’ve also seen how to use objects to collect values together as named properties. Well, it’s time to combine functions and objects to turbocharge productivity, efficiency, and readability.

Remember the first-aid kit from chapter 3? We consider the kit to be a single object that we can pass around, pack in a rucksack, and speak about. Such encapsulation, or chunking, where we treat a collection as singular, is an important part of how we as humans cope with complex information, both in language and in memory. When the need arises, we can consider the elements that make up the kit: antiseptic, plasters, bandages, and so on.

You use the same concept of encapsulation throughout this chapter as you pass objects to and from functions as arguments and return values.

7.1. Using objects as arguments

7.2. Returning objects from functions

7.3. Methods—setting functions as properties of objects

7.4. The Crypt–player objects as arguments

7.5. Summary

sitemap