HTML Academy
Object as a dictionary
Objects26/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”
Let’s not forget about the context
  • Sign up
  • Log in

Loading…
Everything will be ready in few seconds

  • Theory
  • Theory
  • Comments

Bracket notation

Did you notice something unusual in the example of working with the dictionary?

var catsFavoriteFood = {
  Muffin: 'fish',
  Rudolph: 'cutlet',
  Snowball: 'milk'
};

var printFavoriteFood = function (name) {
  return 'My favorite food is ' + catsFavoriteFood[name];
};

console.log (printFavoriteFood ('Snowball'));
// Logs 'My favorite food is milk'

Before, we accessed the properties of an object using a fullstop, and in this example, we work with an object as an array, we read the property using square brackets: catsFavoriteFood[name]. Yes, that’s also possible. The method with brackets is called bracket notation, the method with a dot dot notation. Programmers are so original, right?

Bracket notation is much more flexible than dot notation. For example, you can read from an object a property whose name is written to a variable:

var name = 'Muffin';
var catsFavoriteFood = {Muffin: 'fish'};

console.log(catsFavoriteFood.name);
// Logs undefined in the console

console.log(catsFavoriteFood[name]);
// Logs 'fish' in the console

The first message contains undefined, because the object does not have the name property. The second message contains the required value, because the program understands that there is a variable inside the brackets. The value of the variable is placed in the brackets, and then the required property of the object will be found.

console.log(catsFavoriteFood[name]);
// Inside the program quietly turns into:
console.log(catsFavoriteFood['Muffin']);

And one more surprise: any strings, even strings with spaces, can be used as keys in an object. Such properties cannot be read with dot notation, but you can definitely do it with no problem with bracket notation.

var cat = { 'favorite food': 'Milk' };

console.log(cat.favorite food);
// Causes an error

console.log(cat ['favorite food']);
// Works fine

By the way, why did we take the dictionaries out of the function to the very beginning of the program? Values ​​that are stored in these objects do not depend on the parameters of the function or computer object. These are the rules that apply to the whole program. They can be used in other functions and operations, if necessary. If the dictionaries were in the body of the function, they would be created only when the function was called.

Why does this even work? The object inside the function sees objects that are declared at the very beginning of the program. This is how scopes work. We will analyze this language feature later on, in other courses.

Now that the dictionaries have been created, and we learned how to work with them, we can calculate the price of the computer in one line. We will use the getPrice method. We will override it by adding new calculations.

Comments

  • script.js
JavaScript
var processorPrice = { 'i5': 5000, 'i7': 10000 }; var displayPrice = { 13: 5000, 15: 10000 }; var memoryPrice = { 8: 3000, 16: 4000 }; var buildComputer = function (memory, display, processor) { var computer = { basicPrice: 5000, processor: processor, display: display, memory: memory, getDescription: function () { return 'computer with processor ' + computer.processor + ', diagonal ' + computer.display + ', RAM ' + computer.memory; }, getPrice: function () { return computer.basicPrice; } }; return computer; }; var myComputer = buildComputer(8, 13, 'i7'); console.log('In the cart ' + myComputer.getDescription() + ' that costs ' + myComputer.getPrice());

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. Return computer.basicPrice + processorPrice[computer.processor] + displayPrice[computer.display] + memoryPrice[computer.memory] expression from getPrice method instead of computer.basicPrice.

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.