Summary of “Objects”. Part 1
An object is a data type that stores information as key-value pairs. Each property has a key that lets us access its value. In these examples, we use the keys rather than rely on property order.
var cat = {
name: 'Muffin',
age: 5
};
console.log(cat.name); // Logs 'Muffin' in the console
console.log(cat.age); // Logs 5 in the console
console.log(cat.color); // Logs undefined, there is no such key in the object
cat.age++; // Increases cat’s age by 1
console.log(cat.age) // Logs 6 in the console
cat.name = 'Rocky'; // Replacing the name property value
console.log(cat.name); // Logs 'Rocky' in the consoleShared references
Here, firstCat and secondCat refer to the same object. Assigning firstCat to secondCat copies the reference; no second object is created. Changes to that object’s properties are visible through every variable referring to it, whether there are twenty references or forty. This shared reference is an important feature to remember.
var firstCat = {
name: 'Muffin',
age: 5
};
var secondCat = firstCat;
console.log(secondCat);
// Logs {"name":"Muffin","age":5}
firstCat.name = 'Snowball';
console.log(secondCat);
// Logs {"name":"Snowball","age":5}Continue