HTML Academy
Build it yourself!
Objects23/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”
Implementing methods
  • Sign up
  • Log in

Loading…
Everything will be ready in few seconds

  • Theory
  • Theory
  • Comments

My first method

The function and the object were created. Let’s continue.

We need to calculate the computer’s price and log the final message. In order to do this, we need to create functions and write them directly into the object! We can do it!

Objects can store values of any type, including functions. Such function properties are called object methods. They are called in the same way as any other functions, using parentheses, and we access methods the same way as we access object properties. As a result, the method call is written like this: object.method().

The main thing in creating a method is to come up with a suitable name that describes what this method does. In the programming world, there are established naming traditions. For example, methods that retrieve information often begin with get and are called getters. Not every function that returns a value is a getter. It looks like this:

var cat = {
  name: 'Muffin',
  color: 'red',
  age: 5,

  getGreeting: function () {
    return 'Meow, hello!';
  }
};

console.log (cat.getGreeting ()); // Logs 'Meow, hello!'

Since methods are functions, why do we even write them into an object and not use the usual external functions?

Methods are used to work with objects. They can read or update properties and return results. Yes, you can create an external function, pass an object to it and process it inside this function. But it’s much more convenient to keep in the object everything that relates to this object. For example, we store specifications inside the computer object, and we need to return a string with data and calculate the price. These two tasks are related specifically to the computer object, and we’ll write these functions into the object.

Create a simple getter for the computer object and check to see if everything works as it should.

Comments

  • script.js
JavaScript
var buildComputer = function (memory, display, processor) { var computer = { basicPrice: 5000, processor: processor, display: display, memory: memory }; return computer; }; var myComputer = buildComputer(8, 13, 'i7'); console.log(myComputer);

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. Add the getDescription method, which returns the 'computer' string, to the computer object.
  2. Instead of logging variable myComputer in the console, log message 'In the cart ' + myComputer.getDescription().

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.