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. Private class fields

Battle Royale Games Example

Here is an example of a Player class with private fields in the context of a battle royale game:

class Player {
  #kills = 0;  // Private field
  #deaths = 0;  // Private field
  #alive = true;  // Private field

  constructor(name) {
    this.name = name;  // Public field
  }

  // Public method
  getKills() {
    return this.#kills;
  }

  // Public method
  getDeaths() {
    return this.#deaths;
  }

  // Public method
  isAlive() {
    return this.#alive;
  }

  // Public method
  kill() {
    this.#kills++;
  }

  // Public method
  die() {
    this.#deaths++;
    this.#alive = false;
  }
}

const player1 = new Player('John');
const player2 = new Player('Jane');

player1.kill();
player1.kill();
player2.die();

console.log(player1.getKills());  // Output: 2
console.log(player2.getDeaths());  // Output: 1
console.log(player1.isAlive());  // Output: true
console.log(player2.isAlive());  // Output: false

In this example, the Player class has private fields #kills, #deaths, and #alive, as well as public fields name, getKills, getDeaths, isAlive, kill, and die.

The private fields #kills and #deaths are used to track the number of kills and deaths a player has. The private field #alive is used to track whether a player is currently alive or not.

The public methods getKills, getDeaths, and isAlive are used to access the values of the private fields. For example, player1.getKills() returns the number of kills player1 has. The public methods kill and die are used to modify the values of the private fields. For example, calling player1.kill() increments player1's #kills field by 1.

Private fields and methods can be useful for encapsulation, as they allow you to hide implementation details and prevent direct modification of sensitive data. In this case, using private fields to store the number of kills, deaths, and whether a player is alive allows us to ensure that this data is only modified through the appropriate methods, rather than being directly modified by calling code.

PreviousPrivate class fieldsNextAysnc/Await

Last updated 2 years ago

🥦