JavaScript Tutorial
  • 🍼Introduction
  • 🥛Hello Gamers
  • 🍯Variables
    • Action Game Example
  • 🌰Strings
    • RPG Example
  • 🍪Character Acces
    • Simulation Example
  • 🍩Plus (+) operator
    • Strategy Game Example
  • 🥜Functions
    • Adventure Game Example
  • 🍿Template strings
    • Strategy Game Example
  • 🍫Numbers
    • Sports Game Example
  • 🍬Converting Numbers to Strings
    • Fighting Game Example
  • 🍭Operations
    • FPS Game Example
  • 🍮Conditions
    • Platformer Game Example
  • 🎂Arrays
    • Puzzle Game Example
  • 🍰Objects
    • Racing Game Example
  • 🧁Arrow Functions
    • Rhythm Game Example
  • 🔌Shameless Plug
  • 🥧Implicit Return
    • Platform Game Example
  • 🍦String Methods
    • GRID Game Example
  • 🍨Object Literals
    • Board Game Example
  • 🍧DynamicProperty
    • Political Game Example
  • 🍡Reading Dynamic Property
    • Hide & Seek Game
  • 🥮Objects Continued
    • Medieval Game Example
  • 🥠Object Shorthands
    • Trivia Game example
  • 🔌Shameless Plug (again)
  • 🍥Destructuring & Concatenation
    • Dig a hole game example
  • 🍏Optional Chaining
    • Action Game Example
  • 🍐Nullish coalescing operator
    • Adventure Game Example
  • 🍊Refactoring If Conditions
    • Arcade Game Example
  • 🍋Implicit Conversion & Falcy Values
    • Strategy Game Example
  • 🍌Arrays of objects
    • Sports Game Example
  • 🍉Transforming Array of Objects
    • Fighters Game Example
  • 🍇Some important array methods
    • FPS Example
  • 🫐Reducing arrays of objects
    • Third Person Shooter Example
  • 🍓Try....catch
    • Platformer Example
  • 🍈Immutability
    • Puzzle game example
  • 🍒Creating new arrays or objects
    • Racing Game Example
  • 🔌Shameless Plug (again)
  • 🍑Immutable object operations
    • Basics
    • Rhythm Game Example
  • 🥭Classes
    • Basics
    • Platformer Game Example
  • 🍍Instance Method
    • Basics
    • Role-playing game example
  • 🥥Object Oriented Programming
    • Basics
    • Stealth Game Example
  • 🥝Getters & Setters
    • Basics
    • Survival Game Example
  • 🍅Static Methods & Chaining
    • Basics
    • Tactical Role Playing Game
  • 🍆Class Inheritance
    • Tower Defense Game Example
  • 🥑Use of Super
    • Vehicular Combat Game example
  • 🫒Public Class Field
    • MMORPG Example
  • 🥦Private class fields
    • Battle Royale Games Example
  • 🥬Aysnc/Await
    • Collectible Card Game example
    • Video Board game example
  • 🫑SetTimeout
    • Dice game example
  • 🥒Execution Order
    • Digital Collectible Card Game example
  • 🌶️The callback pattern
    • Life Simulation Game example
  • 🌽Promises
    • Music Game Example
  • 🥕Writing a function that returns a promise
    • Party game example
  • 🧄Introduction to JSON
    • Tile based game example
  • 🧅JSON.parse(string) / JSON.stringify(object)
    • Rhythm action game example
  • 🍠JSON Example for a game
    • Fetching JSON game data
  • 🥐Handling fetch errors
    • Fetch and HTTP Requests
  • 🥯ES Modules
    • Tower Defence Game Example
  • 🍞Default Export
    • Strategy Adventure Game Example
  • 🥖Fetch Wrapper
    • Strategy Role-playing game example
  • 🔌Final Shameless Plug
  • 🫓Lexical Scope and Arrow Functions
    • Vehicular Combat Game Example
  • 🥨Passing Functions
    • Vehicular simulation game example
  • 🥚Dynamic Imports
    • Video card game example
  • 🍠What is a closure
    • Video casino game example
  • 🎆Congratulations
Powered by GitBook
On this page
  1. Static Methods & Chaining

Tactical Role Playing Game

Here is an example of method chaining in the context of a hypothetical tactical role-playing game (TRPG):

class Character {
  constructor(name, health, attack, defense) {
    this._name = name;
    this._health = health;
    this._attack = attack;
    this._defense = defense;
  }
  
  get name() {
    return this._name;
  }
  
  get health() {
    return this._health;
  }
  
  set health(newHealth) {
    this._health = newHealth;
  }
  
  get attack() {
    return this._attack;
  }
  
  get defense() {
    return this._defense;
  }
  
  attack(character) {
    character.health -= this.attack;
  }
  
  defend() {
    this.defense += 5;
    return this;
  }
  
  usePotion() {
    this.health += 10;
    return this;
  }
}

class Player extends Character {
  constructor(name, health, attack, defense, level, experience) {
    super(name, health, attack, defense);
    this._level = level;
    this._experience = experience;
  }
  
  get level() {
    return this._level;
  }
  
  set level(newLevel) {
    this._level = newLevel;
  }
  
  get experience() {
    return this._experience;
  }
  
  set experience(newExperience) {
    this._experience = newExperience;
  }
  
  levelUp() {
    this.level++;
    this.attack += 5;
    this.defense += 5;
    this.health += 10;
    return this;
  }
}

const player = new Player("Bob", 100, 20, 10, 1, 0);
const enemy = new Character("Goblin", 50, 15, 5);

player.attack(enemy);
console.log(enemy.health); // 35
player.defend().usePotion().levelUp().attack(enemy);
console.log(player.attack); // 25
console.log(player.defense); // 20
console.log(player.health); // 120
console.log(enemy.health); // 20

In this example, we have a base Character class that represents any character in the game with a name, health, attack, and defense. The Player class extends the Character class and adds a level and experience. The Character class has attack, defend, and usePotion methods that return the character object itself, allowing the methods to be chained together. The Player class also has a levelUp method that increases the player's level and stats.

In the example code, we create a player and an enemy character, and then use method chaining to chain together calls to the defend, usePotion, and levelUp methods before attacking the enemy again. This allows the player to increase their defense, heal themselves, level up, and then attack the enemy in a single statement

PreviousBasicsNextClass Inheritance

Last updated 2 years ago

🍅