I have a Heap class that has a private values field. This is good since I do not want anyone to be able to directly modify values.
class Heap {
#values;
constructor(array = []) {
this.#values = array;
}
insert(item) {
this.#values.push(item);
this.#bubbleUp(this.#values.length - 1);
}
}
But now I would like to extend my Heap class into a PriorityQueue subclass. In this class I need to change the signature of some of the methods (for example insert) so that it assigns a priority to a value. However, I cannot figure out how to access the values field from the base class. For instance, given the following PriorityQueue class:
class PriorityQueue extends Heap {
constructor(array = []) {
super(array);
}
insert(item, priority) {
// Error: Private field '#values' must be declared in an enclosing class
this.#values.push({ item, priority });
this.#bubbleUp(this.#values.length - 1);
}
}
I get an error when trying to push a value onto values.
Is there any way around this problem? I want to make a field private in the base class, but still accessible to the subclass.
Thanks!
insertmethod?super.insert({ item, priority }).