Chapter 4Objects and Functions

In the previous chapter, we looked at arrays. Now we’re going to look at objects, which are similar to arrays in that they are containers for collections of data. Though there are similarities―arrays are actually a type of object―there are also some significant differences.


Creating an Object

As with arrays, there are a couple of ways to create objects, and, just like arrays, one is preferred over the other. So even though you can do this:

        var myObject = new Object();      

it is much better to do this:

        var myObject = {};      

The latter is simpler, safer, and therefore preferable. If you remember from Chapter 3, Array() can be overwritten for malicious purposes. So can Object(), which is why it's safer to use ...

Get Jump Start JavaScript now with the O’Reilly learning platform.

O’Reilly members experience live online training, plus books, videos, and digital content from nearly 200 publishers.