LESSON 47 OF 50

Define an object method

Add behavior that uses the same object’s data.

Official sourceECMAScript — Method definitionsReviewed: 2026-08-26
STEP 1 · LEARN

How it works

A method definition creates a function-valued property. When calling `profile.describe()`, `this` inside the method is profile.

Correct example
const profile = {
  name: 'Ana',
  role: 'designer',
  describe() {
    return `${this.name} - ${this.role}`;
  }
};
console.log(profile.describe());

The method reads the current object’s properties and returns the description.

Common mistake
console.log('Ana - designer');

The message is hard-coded and no method exists.

STEP 2 · APPLY

Your exercise

Add a describe method that returns name and role through this, then print profile.describe().

STEP 3 · RUN

Write and run the code

script.jsJAVASCRIPT
Isolated consoleisolated
Preparing the runtime…
Run the code to see the result.
The draft stays only in this browser.Isolated QuickJS · no DOM, session or network
STEP 4 · CHECK

What needs to pass

0%not checked
  • profile defines describe, uses this.name and this.role and returns the result.
  • profile contains name and role.
  • The output is exactly Ana - designer.
Explain the solution logic

The method keeps behavior beside the data and uses the object that received the call.