Summary of “Objects”. Part 1

Object is a data type that stores information in the form of key-value pairs. Each element is mapped to its own key and the order of the elements is completely unimportant.

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'; // Replaced name property value on the outside
console.log(cat.name); // Logs 'Rocky' in the console

Transfer by reference

There is always one object here, no new place is created in memory for a copy of the object. Each variable contains a reference to a single object, not a new separate entity. Therefore, when we change something in an object through one of the variables that contains a reference to it, the changes are visible in all other variables, be it twenty or forty of them. This is an important feature of objects to remember. It’s called transferring objects by reference.

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