HTML Academy
Bracket notation
Objects27/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”
Store check
  • Sign up
  • Log in

Loading…
Everything will be ready in few seconds

  • Theory
  • Theory
  • Comments

Let’s not forget about the context

Our program is ready! All the necessary functionality was written! All we have left to do is discuss one more issue.

Inside the methods, we access the properties of computer object computer.property. And what if in the future the name of the object changes and the number of properties increases? We must correct computer everywhere and replace it with something new. Laziness is one of the main qualities of a developer, so I know you don’t want to rewrite any names. It’s good that there is keyword this. It’s the one we will use.

this wasn’t named for nothing. As in a speech, such a pronoun points to a specific object, in JavaScript, the this keyword points to a specific object, namely the object where the function (method) was called.

Using this is easy; all you have to do it replace the name of the object with the keyword in the property reference. Instead of object.property, let’s use this.property.

var cat = {
  name: 'Rudolph',

  introduce: function () {
    console.log('Meow! I’m cat ' + this.name + '!');
  }
};

cat.introduce (); // Logs 'Meow! I’m cat Rudolph!'

The object this points to is called a call context.

Important detail: until the function is called, this does not contain any value; the context appears only when the function is called.

Keyword this is quite advantageous for developers. You will learn later (in the future courses) about other advantages and features of this, but for now we will replace the reference to the properties inside the computer object.

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 + processorPrice[computer.processor] + displayPrice[computer.display] + memoryPrice[computer.memory]; } }; 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. In all methods, replace calls to properties and methods of computer with calls using this.
  2. Inside the buildComputer function, change the name of the object from computer to customComputer and don’t forget to return the object with a new name.

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.