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. Passing Functions

Vehicular simulation game example

Here is an example of how you might use the concept of passing functions in the context of a vehicular simulation game

const Vehicle = {
  speed: 0,
  maxSpeed: 100,

  accelerate: (amount) => {
    this.speed += amount;
    if (this.speed > this.maxSpeed) {
      this.speed = this.maxSpeed;
    }
  },

  decelerate: (amount) => {
    this.speed -= amount;
    if (this.speed < 0) {
      this.speed = 0;
    }
  }
};

const Car = {
  name: 'Car',
  maxSpeed: 120
};

const Bike = {
  name: 'Bike',
  maxSpeed: 60
};

Car.accelerate(30);
console.log(Car.speed);  // 30
Car.accelerate(80);
console.log(Car.speed);  // 100

Bike.accelerate(30);
console.log(Bike.speed);  // 30
Bike.decelerate(20);
console.log(Bike.speed);  // 10

In this example, we have a Vehicle object with two methods: accelerate and decelerate. The accelerate method increases the speed of the Vehicle object by a given amount, while the decelerate method decreases the speed of the Vehicle object by a given amount. Both methods also ensure that the speed of the Vehicle object stays within the range 0 to maxSpeed.

We then create two objects based on the Vehicle object: Car and Bike. The Car object has a name of "Car" and a maxSpeed of 120, while the Bike object has a name of "Bike" and a maxSpeed of 60.

We can then use the accelerate and decelerate methods on the Car and Bike objects to change their speeds. As the example code shows, the Car object's speed is increased to 30 when we call Car.accelerate(30), and then increased to 100 when we call Car.accelerate(80). The Bike object's speed is increased to 30 when we call Bike.accelerate(30), and then decreased to 10 when we call Bike.decelerate(20)

PreviousPassing FunctionsNextDynamic Imports

Last updated 2 years ago

🥨