ES6 Class - How to write private method?

Hi,

I completed the Binary Search Tree challenge…
I used ES6 syntax. I am very nooby about Classes… This is the first time when I used.
My question is:
I made some method what only help to resolve the challenge:
Examples:

  • levelOrder()
  • reverseLevelOrder()

These methods do the same, only with a little different (the directions)
So I wrote another method: bfs()
What is the correct syntax (or code) to turn private the bfs() method?
Now this method (and many more from my BinarySearchTree Class can called from outside, but these methods only use for inside the class…

My code is:
bfs(direction = "forward") {
        const queue = new DoublyLinkedList(),
              values = [],
              left = direction === "backward" ? "right" : "left",
              right = direction === "backward" ? "left" : "right"
        ;
        queue.add(this.root);
        
        while (!(queue.isEmpty())) {
            const node = queue.remove(queue.head.data);
            if (node[left]) queue.add(node[left]);
            if (node[right]) queue.add(node[right]);
            values.push(node.value);
        }
        
        return values;
        
}
    
levelOrder() {
        return this.root ? this.bfs() : null;
}
    
reverseLevelOrder() {
        return this.root ? this.bfs("backward") : null;
}

const myTree = new BinarySearchTree();
console.log(myTree.bfs()); // return an array… so this is callable from outside, I would like to hide this method…

As far as I know, the current version of javascript does not support private methods or fields in its current class syntactic sugar. There are proposals to add this functionality (eg experimental feature (stage 3) proposed at TC39] ) .

That syntax, where supported, apparently looks something like

class Rectangle {
  #height = 0;
  #width;
  constructor(height, width) {    
    this.#height = height;
    this.#width = width;
  }
}

(example from mdn)

If you opted not to use the class syntax, you could get this sort of encapsulation with a closure.

If you want a more private method, then you can not currently use the class syntax. Instead, use a regular function which has various locally scoped functions that can be called from within the function and then only return an object which contains the public methods you want to users to use.

So I can give better answers in the future, could you help me out? @lendoo

Is the accepted solution better because it offers a more detailed explanation of ‘use a closure’? Or because it offers less info on the current + proposed future language spec wrt private declarations? Or some other reason I’ve not thought of?

I could certainly do a better job of understanding an askers’ perspective and tailoring my answers accordingly. To that end it would be a great help to me if you could provide me direct insight.

Hi,
Yes, of course I try help to you. I also tried to find any way to turn private some methods in the ES6 class but I did not find… I also found your answer here
I tried this syntax but not works at all…
The answer of Randell solved the private class method problem… Until not arrive the new javascript update we need to use ES5 syntax… this is the solution.

Did you try something like this?

const findMe = 7;

class Hunter {
  find() {
    console.log(findMe);
  }
}

const hunter = new Hunter();

console.log(hunter.find());