DeveloperBreeze

Static Methods Development Tutorials, Guides & Insights

Unlock 1+ expert-curated static methods tutorials, real-world code snippets, and modern dev strategies. From fundamentals to advanced topics, boost your static methods skills on DeveloperBreeze.

Understanding JavaScript Classes

Tutorial September 02, 2024
javascript

As of ECMAScript 2022, JavaScript supports private fields and methods, which are not accessible outside of the class definition. This is done by prefixing the field or method name with #.

class Counter {
  #count = 0;

  increment() {
    this.#count++;
    console.log(this.#count);
  }

  getCount() {
    return this.#count;
  }
}

const counter = new Counter();
counter.increment(); // Output: 1
console.log(counter.getCount()); // Output: 1
console.log(counter.#count); // Error: Private field '#count' must be declared in an enclosing class