ChApter 1 ■ JAvASCrIpt You Need to KNow
Listing 1-17 shows a few different ways of creating objects. I tend not to use or come across the new Object()
technique very much (commented with Example 3 in the listing), and I think you will see the other two approaches
used a lot more. Examples 1, 2, and 3 all do the same thing: they create an object, add a couple of properties to it, and
assign some values to those properties. Each example logs to the console to produce the following output:
Andrew
Andrew
Andrew
■ Note You can think of properties as variables defined on objects. however, in the world of object-oriented
programming, which I don’t cover in this book, there are far better definitions.
Reading and Modifying an Object’s Properties
Changing the values of properties can be done in a couple of ways. Listing 1-18 demonstrates accessing and changing
object values.
Listing 1-18. Accessing and Changing Object Values
<!DOCTYPE html>
As the following output demonstrates, we start off by setting a value of Andrew on the firstName property; shortly
thereafter, we change that value to Monica. On both occasions, we use dot syntax, that is, the object name, followed
by a dot and then the property name. Shortly afterward, we change it yet again, but this time, we use associative array
syntax. This syntax requires us to use the object name and then to specify the property name within brackets.