Chapter 10. Arrays Made Easy

 

This chapter covers

  • Working with JavaScript arrays
  • Understanding Prototype’s functional approach to arrays
  • Extending arrays and hashes using the Enumerable mixin

We’ve covered the most common language features now, namely the Object and the Function. Using these, we can easily organize our data and invoke operations on it. In the previous two chapters, we’ve already seen how Prototype makes it easier to work with these features in a meaningful way.

There is a third element that is almost as fundamental to most programming languages, and that is the array. Arrays can be used to organize data into lists, and recursively into more complex structures. JavaScript supports arrays through the Array object in a powerful and flexible way. Prototype extends the standard Array in quite remarkable ways, adding over thirty new methods. Along the way, it adds some more array-like power to other objects too.

We’ll begin this chapter by reviewing the capabilities provided for Arrays by the JavaScript language. We’ll then look at the way Prototype handles Arrays, and see how this can streamline our code when working with complex data structures. We’ll finish off with a short look at the way in which Prototype makes some of its Array capabilities available to other objects.

10.1. Introducing Arrays

10.2. The native JavaScript Array

10.3. Prototype.js and Arrays

10.4. Methods of the Enumerable object

10.5. Working with Hashes and ObjectRanges

10.6. Summary