In this chapter we've taken a look at the concept of objects and seen how vital they are to an understanding of JavaScript, which represents virtually everything with objects. We also looked at some of the various native objects that the JavaScript language provides to add to its functionality.
We saw that
JavaScript is object-based—it represents things, such as strings, dates, and arrays, using the concept of objects.
Objects have properties and methods. For example, an Array object has the length property and the sort() method.
To create a new object, we simply write new ObjectType(). We can choose to initialize an object when we create it.
To set an object's property's value or get that value, we simply write ObjectName.ObjectProperty.
Calling the methods of an object is similar to calling functions. Parameters may be passed, and return values may be passed back. Accessing the methods of an object is identical to accessing a property, except we must remember to add parentheses at the end, even when there are no parameters. For example, we would write ObjectName.ObjectMethod().
The String object provides lots of handy functionality for text and gives us ways of finding out how long the text is, searching for text inside the string, and selecting parts of the text.
The Math object is created automatically and provides a number of mathematical properties and methods. For example, to obtain a random number between 0 and 1, we use the method Math.random().
The Array object provides ways of manipulating arrays. Some of the things we can do are find the length of an array, sort its elements, and join two arrays together.
The Date object provides a way of storing, calculating with, and later accessing dates and times.
JavaScript allows us to create our own type of objects using classes. These can be used to model real-world situations and for making code easier to create and more maintainable, though they do require extra effort at the start.
In the next chapter, we'll turn our attention to the web browser itself and, particularly, the various objects that it makes available for our JavaScript programming. We'll see that the use of the browser objects is key to creating powerful web pages.