HTML Academy
Hello, object!
Objects5/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”
Count off!
  • Sign up
  • Log in

Loading…
Everything will be ready in few seconds

  • Theory
  • Theory
  • Comments

Reading from the object

We created player objects, what should we do with them now? Of course, they must be transferred to runGame, instead of variables that contained names and points. But how do you use them inside a function?

Object properties can be accessed and values can be retrieved from them. There are two ways to access an individual property. We will look at one of them at right now, and we’ll get acquainted with the second one a little later.

To get the value of a property, it must be accessed using object.key. Such a record is called dot notation and returns object property value if such a property exists. Otherwise you’ll get undefined, that’s nothing. In practice, it looks like this:

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 a key in the object

Let’s replace variables with player objects in the parameters of function runGame, and with this in mind, replace the access to the parameters in the body of the function.

Comments

  • script.js
JavaScript
var diceNumber = 2; var firstCat = { name: 'Muffin', points: 0 }; var secondCat = { name: 'Rudolph', points: 0 }; var firstCatName = 'Muffin'; var firstCatPoints = 0; var secondCatName = 'Rudolph'; var secondCatPoints = 0; var runGame = function (quantity, firstPlayerName, firstPlayerPoints, secondPlayerName, secondPlayerPoints) { firstPlayerPoints += muffin.throwDice(quantity, quantity * 6); secondPlayerPoints += muffin.throwDice(quantity, quantity * 6); console.log(firstPlayerName + ' rolled ' + firstPlayerPoints); console.log(secondPlayerName + ' rolled ' + secondPlayerPoints); }; runGame(diceNumber, firstCatName, firstCatPoints, secondCatName, secondCatPoints);

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. Delete all parameters except quantity in  declaration of runGame function and add new parameters: firstPlayer and secondPlayer.
  2. Delete all arguments except diceNumber in the call of runGame function and add new arguments: firstCat and secondCat.
  3. In the body of the function, replace firstPlayerPoints with firstPlayer.points, firstPlayerName with firstPlayer.name.
  4. Similarly, replace variables with objects for the second player.
  5. Delete variables firstCatName, firstCatPoints, secondCatName, secondCatPoints.

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.