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.
class Stack {
constructor(){
this.items = []
}
}
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.
const items = new WeakMap();
class Stack {
constructor(){
items.set(this, []);
}
}
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:
const items = new WeakMap();
class Stack {
constructor() {
items.set(this, []);
this.stack = items.get(this, []);
push(item) {
this.stack.push(item)
}
}
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?