Share this Page URL

Hour 4. How JavaScript Programs Work > Handling Events - Pg. 32

How JavaScript Programs Work 32 Understanding Objects In Hour 2,"Creating a Simple Script," you learned that variables are containers that can store a number, a string of text, or another value. JavaScript also supports objects. Like variables, objects can store data--but they can store two or more pieces of data at once. The items of data stored in an object are called the properties of the object. As an example, you could use objects to store information about people as in an address book. The properties of each person object might include a name, address, and other information. JavaScript uses periods to separate object names and property names. For example, for a person object called Bob, the properties might include Bob.address and Objects can also include methods. These are functions that work with the object's data. For example, our person object for the address book might include a display() method to display the person's information. In JavaScript terminology, the statement Bob.display() would display Bob's details. Note The document.write function we discussed earlier this hour is actually a method of the document object. You will learn more about this object in Hour 10, "Working with the Document Object Model." Don't worry if this sounds confusing--you'll be exploring objects in much more detail later in this book. For now, you just need to know the basics. JavaScript supports three kinds of objects: · Built-in objects are objects built into the JavaScript language. You've already encountered one of these, Date, in Hour 2. Other built-in objects include Array and String, which you'll explore in Hour 6, "Using Strings and Arrays," and Math, which is explained in Hour 9, "Using Built-in