> 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/lexical-scope-and-arrow-functions/vehicular-combat-game-example.md).

# Vehicular Combat Game Example

Here is an example of how you might use arrow functions in the context of a vehicular combat game:

```javascript
const Vehicle = {
  health: 100,
  damage: 50,

  attack: function(target) {
    target.health -= this.damage;
    console.log(`${this.name} attacked ${target.name} for ${this.damage} damage. ${target.name}'s health is now ${target.health}.`);
  },

  repair: () => {
    this.health += 50;
    console.log(`${this.name} repaired for 50 health. ${this.name}'s health is now ${this.health}.`);
  }
};

const Tank = {
  name: 'Tank',
  health: 200
};

const Jeep = {
  name: 'Jeep',
  health: 100
};

Tank.attack(Jeep);  // Tank attack Jeep for 50 damage. Jeep's health is now 50.
Jeep.repair();  // Uncaught TypeError: Cannot read property 'name' of undefined
```

In this example, we have a `Vehicle` object with two methods: `attack` and `repair`. The `attack` method takes a target object and reduces its health by the `damage` amount. The `repair` method increases the health of the `Vehicle` object by 50.

We then create two objects based on the `Vehicle` object: `Tank` and `Jeep`. The `Tank` object has a `name` of "Tank" and a `health` of 200, while the `Jeep` object has a `name` of "Jeep" and a `health` of 100.

When we call the `Tank.attack(Jeep)` method, the `Jeep` object's health is reduced by 50, as expected. However, when we try to call the `Jeep.repair()` method, we get an error because the `this` value inside the `repair` method is `undefined`. This is because the `repair` method is an arrow function, which does not have its own `this` value. Instead, it inherits the `this` value from the enclosing scope, which in this case is the global scope. Since there is no `name` property in the global scope, we get an error when we try to access `this.name`.

If we had defined the `repair` method as a regular function, like this:

```javascript
repair: function() {
  this.health += 50;
  console.log(`${this.name} repaired for 50 health. ${this.name}'s health is now ${this.health}.`);
}
```

Then the `repair` method would work as expected, because the `this` value inside the function would be the object that the function was called on (in this case, the `Jeep` object).
