Review: Using JavaScript Objects

Dot notation

Above, you accessed the object's properties and methods using dot notation. The object name (person) acts as the namespace - it must be entered first to access anything inside the object. Next, you write a dot, then the item you want to access - this can be the name of a simple property, an item of an array property, or a call to one of the object's methods, for example:

person.age;
person.bio();


Objects as object properties

An object property can itself be an object. For example, try changing the name member from

const person = {
  name: ["Bob", "Smith"],
};

to

const person = {
  name: {
    first: "Bob",
    last: "Smith",
  },
  // …
};

To access these items, you need to chain the extra step onto the end with another dot. Try these in the JS console:

person.name.first;
person.name.last;

If you do this, you'll also need to go through your method code and change any instances of

name[0];
name[1];

to

name.first;
name.last;

Otherwise, your methods will no longer work.