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

Basics

In JavaScript, static methods are methods that are associated with a class itself, rather than with instances of the class. They can be called without creating an instance of the class, using the name of the class followed by the method name, like this:

class MathUtils {
  static sum(a, b) {
    return a + b;
  }
}

console.log(MathUtils.sum(1, 2)); // 3

Static methods are often used for utility functions that do not need to access or modify the instance state of a class.

Method chaining is a technique for calling multiple methods on the same object, one after the other, in a single statement. Each method returns the object itself, allowing you to chain the method calls together. Here is an example of method chaining in JavaScript:

class StringBuilder {
  constructor(string) {
    this._string = string;
  }
  
  append(string) {
    this._string += string;
    return this;
  }
  
  prepend(string) {
    this._string = string + this._string;
    return this;
  }
  
  toString() {
    return this._string;
  }
}

const builder = new StringBuilder("Hello");
const result = builder.append(", World!").prepend("Hi, ").toString();
console.log(result); // "Hi, Hello, World!"

In this example, we have a StringBuilder class with three methods: append, prepend, and toString. The append and prepend methods return the StringBuilder object itself, allowing the methods to be chained together. The toString method returns the final string value.

Static methods and method chaining are not directly related, but they can be used together in a class if needed. For example, you could define a static method that creates and returns a new instance of a class, and then chain method calls onto that instance:

class MathUtils {
  static sum(a, b) {
    return a + b;
  }
  
  static createSummation(a, b) {
    return new Summation(a, b);
  }
}

class Summation {
  constructor(a, b) {
    this._a = a;
    this._b = b;
  }
  
  add(c) {
    this._a += c;
    return this;
  }
  
  toString() {
    return `${this._a} + ${this._b} = ${this._a + this._b}`;
  }
}

const result = MathUtils.createSummation(1, 2).add(3).toString();
console.log(result); // "6 + 2 = 8"

In this example, we have a MathUtils class with a static createSummation method that creates and returns a new Summation object. We can then chain method calls onto the Summation object using method chaining.

PreviousStatic Methods & ChainingNextTactical Role Playing Game

Last updated 2 years ago

🍅