> For the complete documentation index, see [llms.txt](https://demirels-organization.gitbook.io/javascript-tutorial/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://demirels-organization.gitbook.io/javascript-tutorial/instance-method/role-playing-game-example.md).

# Role-playing game example

Here is an example of using instance methods in the context of a role-playing video game:

```javascript
class Character {
  constructor(name, hp, attackPower) {
    this.name = name;
    this.hp = hp;
    this.attackPower = attackPower;
  }

  // Instance method to attack another character
  attack(otherCharacter) {
    otherCharacter.hp -= this.attackPower;
    console.log(`${this.name} attacked ${otherCharacter.name} for ${this.attackPower} damage!`);
  }

  // Instance method to heal this character
  heal(amount) {
    this.hp += amount;
    console.log(`${this.name} healed for ${amount} HP!`);
  }
}

class Warrior extends Character {
  constructor(name, hp, attackPower, shield) {
    super(name, hp, attackPower);
    this.shield = shield;
  }

  // Override the attack method to include the shield value
  attack(otherCharacter) {
    const totalAttackPower = this.attackPower + this.shield;
    otherCharacter.hp -= totalAttackPower;
    console.log(`${this.name} attack ${otherCharacter.name} for ${totalAttackPower} damage!`);
  }
}

class Mage extends Character {
  constructor(name, hp, attackPower, magicPower) {
    super(name, hp, attackPower);
    this.magicPower = magicPower;
  }

  // Instance method to cast a spell on another character
  castSpell(otherCharacter) {
    otherCharacter.hp -= this.magicPower;
    console.log(`${this.name} casts a spell on ${otherCharacter.name} for ${this.magicPower} damage!`);
  }
}

const warrior1 = new Warrior('Bob', 100, 10, 5);
const mage1 = new Mage('Alice', 80, 5, 15);

warrior1.attack(mage1); // "Bob attack Alice for 15 damage!"
mage1.castSpell(warrior1); // "Alice casts a spell on Bob for 15 damage!"
warrior1.heal(10); // "Bob healed for 10 HP!"
```

n this example, we have a base `Character` class with an `attack` method that reduces the hit points (HP) of another character, and a `heal` method that increases the character's own HP. We also have a `Warrior` class that extends the `Character` class and has a `shield` property, and an `attack` method that includes the shield value in the attack power. Finally, we have a `Mage` class that extends the `Character` class and has a `magicPower` property, and a `castSpell` method that reduces the HP of another character using the magic power.

We create a warrior and a mage character, and use their instance methods to attack and cast spells on each other. The warrior also uses the `heal` method to restore some of its own HP.


---

# Agent Instructions
This documentation is published with GitBook. GitBook is the documentation platform designed so that both humans and AI agents can read, navigate, and reason over technical content effectively. Learn more at gitbook.com.

## Querying This Documentation
If you need additional information that is not directly available in this page, you can query the documentation dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter:

```
GET https://demirels-organization.gitbook.io/javascript-tutorial/instance-method/role-playing-game-example.md?ask=<question>
```

The question should be specific, self-contained, and written in natural language.
The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
