HTML Academy
Overriding object properties
Objects8/30
Back to the list of tasks
  • 1. Shall we play?
  • 2. Players, let’s start!
  • 3. My attempt number one
  • 4. Hello, object!
  • 5. Reading from the object
  • 6. Count off!
  • 7. Overriding object properties
  • 8. Objects and references
  • 9. My game
  • 10. Giving out the attempts
  • 11. Who is the winner?
  • 12. Announce the entire list, please
  • 13. Looking for a cat with great results
  • 14. No one will hide
  • 15. New conditions
  • 16. Hard to come across
  • 17. Let’s bring it all to light
  • 18. Roll the dice, gentlemen cats!
  • 19. Let’s make adjustments
  • 20. Summary of “Objects”. Part 1
  • 21. Tenth program: “Golden ball”
  • 22. Build it yourself!
  • 23. My first method
  • 24. Implementing methods
  • 25. Object as a dictionary
  • 26. Bracket notation
  • 27. Let’s not forget about the context
  • 28. Store check
  • 29. Summary of “Objects”. Part 2
  • 30. Eleventh program: “The house that Muffin built”
My game
  • Sign up
  • Log in

Loading…
Everything will be ready in few seconds

  • Theory
  • Theory
  • Comments

Objects and references

Before we continue to write the game of dice, let’s consider one important feature of objects that we come across in practice.

var firstCat = {
  name: 'Muffin',
  age: 5
};

var secondCat = firstCat;
// Logs {"name":"Muffin","age":5}
console.log(secondCat);

firstCat.name = 'Snowball';
// Logs {"name":"Snowball","age":5}
console.log(secondCat);

We changed the name through firstCat, yet we see the change through secondCat too. Why did that happen? Does it even make any sense?

In this example, both variables refer to the same object. Assigning the object to another variable copies the reference, so no second object is created. Passing an object to a function also passes a copy of its reference. Therefore, changes to the object’s properties are visible through every variable that refers to it, whether there are twenty references or forty. This shared reference is an important feature to remember.

In our program, the same thing happens. We create the array cats with player objects and transfer it to the runGame function. Inside the function, we use this array under a different name, players, and change the objects stored in it. If after the function completes its work we log cats in the console, we will see that its contents have changed, although we did not return anything from the function and it seemed that we worked with a completely different array.

Changing player objects inside the loop also changes what we see through cats. That can be easy to overlook when reading or debugging the program. To make the function’s result explicit for ourselves and other developers, we will return the updated array players and assign the result to cats. The players’ properties have already changed; returning the array makes the result visible at the call site. Both names still refer to the same array.

Comments

  • script.js
JavaScript
var diceNumber = 2; var firstCat = { name: 'Muffin', points: 0 }; var secondCat = { name: 'Rudolph', points: 0 }; var cats = [firstCat, secondCat]; var runGame = function (quantity, players) { for (var i = 0; i < players.length; i++) { var throwResult = muffin.throwDice(quantity, quantity * 6); players[i].points += throwResult; console.log(players[i].name + ' rolled ' + players[i].points); } }; runGame(diceNumber, cats);

What didn’t you like in this task?

Thanks! We’ll fix everything at once!

Console

The code has changed, click “Run” or turn autorun on.

Result

Goalscompleted
  1. Log in the console array cats immediately before and immediately after calling the runGame function.
  2. After the loop, return player array players from the runGame function.
  3. Instead of the usual call of runGame, assign the function’s return value to cats.
  4. Delete the first log of cats in the console.

Cookies ∙ Privacy ∙ License Agreement ∙ About ∙ Contacts ∙ © HTML Academy OÜ, 2019−2026

VISAMastercard

Log in

or

Forgot your password?

Sign up

Sign up

or
Log in

Restore access

Have you forgotten your password or lost access to your profile? Enter your email connected to your profile and we will send you a link to restore access.

Forgot to connect your email to the profile? Email us and we’ll help.