Implementing private functions in JavaScript classes

Hey everyone, I’m trying to figure out how to create private methods in JavaScript classes. I know how to make public methods, but I’m stuck on the private ones. Here’s what I’ve got so far:

class Cafe {
  orderCoffee() {
    // code here
  }

  sitDown() {
    // code here
  }
}

I can use these public methods like this:

const cafe = new Cafe();
cafe.orderCoffee();
cafe.sitDown();

But how do I make a private method that only orderCoffee and sitDown can use, but can’t be called from outside the class? I want to do something like this inside the class:

sitDown() {
  this.cleanTable();
}

But I don’t want this to work:

const c = new Cafe();
c.cleanTable();

Any ideas on how to set up cleanTable as a private method that follows these rules? I’ve looked at some stuff online, but I’m still confused about how to make methods that are both private and accessible to public methods. Thanks for any help!

hey nova56, u can use closures for private methods. try this:

class Cafe {
  constructor() {
    const cleanTable = () => {
      // private stuff here
    };

    this.orderCoffee = () => {
      cleanTable();
      // other code
    };

    this.sitDown = () => {
      cleanTable();
      // more code
    };
  }
}

this way cleanTable is only accessible inside the class. hope it helps!

One effective method for implementing private functions in JavaScript classes is by utilizing the Symbol data type. Here’s how you can adapt your Cafe class:

const cleanTable = Symbol('cleanTable');

class Cafe {
  [cleanTable]() {
    // Private method implementation
  }

  orderCoffee() {
    this[cleanTable]();
    // Rest of orderCoffee logic
  }

  sitDown() {
    this[cleanTable]();
    // Rest of sitDown logic
  }
}

This approach leverages Symbol’s uniqueness to create a property key that’s not accessible outside the class. It’s widely supported and provides a good balance between privacy and functionality. The cleanTable method remains invisible to external code while still being usable within the class methods.

I’ve been working with JavaScript classes for a while now, and I’ve found a couple of ways to handle private methods. One approach that’s worked well for me is using the ‘#’ prefix for private fields and methods. It’s part of the newer JavaScript syntax and provides true privacy. Here’s how you could modify your Cafe class:

class Cafe {
  #cleanTable() {
    // Private method implementation
  }

  orderCoffee() {
    // Public method
    this.#cleanTable();
  }

  sitDown() {
    // Public method
    this.#cleanTable();
  }
}

With this setup, #cleanTable is only accessible within the class. It can’t be called from outside, but orderCoffee and sitDown can use it. Just remember, this syntax is relatively new, so it might not work in older browsers or environments. If you need broader compatibility, you could also consider using a closure or a WeakMap to achieve privacy, but I’ve found the ‘#’ syntax to be the cleanest and most straightforward approach for modern JavaScript development.