In the last exercise, we accessed properties using what is known as dot notation. Good name, right? So to access a property, we useObjectName.PropertyName (e.g., bob.name)
In addition to dot notation, we can also access properties using bracket notation. In this case we use ObjectName["PropertyName"] to access the desired property. Note, we need " " around the property's name.
Take a look at our next example object calleddog. Notice on line 8 how we save the dog's species into a variable by accessing thespecies property of dog using bracket notation.
Instructions
Use bracket notation to save the dog's
weight
and age
into variables as well.
// Take a look at our next example object, a dog
var dog = {
species: "greyhound",
weight: 60,
age: 4
};
var species = dog["species"];
// fill in the code to save the weight and age using bracket notation
var weight =dog["weight"];
var age =dog["age"];
No comments:
Post a Comment