title: JavaScript Classes - Making variables private with WeakMap and still using "this" in other methods
body: <p>I am reading through the book "Learning JS DataStructs and Algorithms", and in the book it says that the "items" is public in the following class.</p>
  
  <pre><code>class Stack {
      constructor(){
        this.items = []
      }
   }
  </code></pre>
  
  <p>But, if I use a WeakMap then I can make items private again, only in the examples given they are not using the "this" like I would expect.</p>
  
  <pre><code>const items = new WeakMap();
  class Stack {
      constructor(){
        items.set(this, []);
      }
  }
  </code></pre>
  
  <p>and then it gives examples of code that does things like items.set or items.get to access things, and that seems fine, but I was wondering if I could just shorten access to the item.get(value) in the constructor on to the "this" like so:</p>
  
  <pre><code>const items = new WeakMap();
  class Stack {
      constructor() {
          items.set(this, []);
          this.stack = items.get(this, []);
  
       push(item) {
           this.stack.push(item)
       }
  }
  </code></pre>
  
  <p>Now, I can access the items.get() functionality with this.stack, but I am NOT sure if it makes it public again, and was wondering if anyone could help clear that up for me?</p>