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. Passing object by a link
  • 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

Passing object by a link

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 did not touch the second object secondCat, but it changed along with the first object firstCat. Why did that happen? Does it even make any sense?

In JavaScript, quite. There is always one object, no new place is created in memory for a copy of the object. Each variable contains not a new separate entity, but a reference to a single object. 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.

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.

Despite the fact that objects are known to behave like that, the work of the program can become obscure and therefore difficult to understand or debug, if we want to transfer the changed array somewhere else, because we change the array implicitly during the course of the loop. The code should be written as simply and clearly as possible, for yourself and for other developers. Therefore, we will return the changed array players from the function and write it to the array cats, thus obviously updating it.

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, record the result of the function’s operation in variable cats.
  4. Delete the first log of cats in the console.

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

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.