I have the following class that extends the built in data type Set. It adds a little bit of extra functionality by checking that every element that wants to be added to the set conforms to the type specified as the first parameter of the constructor. It does this silly "type checking" through the typeof
operator. I save the type of the Set in a private field. If I try to override the .add()
method so that it also does this little type checking, I get an error stating Uncaught TypeError: can't access private field or method: object is not the right class
class TypedSet extends Set {
#type;
constructor(type, elements) {
if (elements && !elements.every(e => typeof e === type)) throw new TypeError(`Not all elements conform to type t->${type}`)
super(elements)
this.#type = type
}
add(e) {
return typeof e === this.#type
? super.add(e)
: new TypeError(`Type of element e does not conform to type t: ${this.#type}`)
}
}
const s = new TypedSet('string', ['hello', 'world'])
s.add('!')
Why do I get that error if i try to access the private field -- the one I declared in the subclass -- in the overriden .add()
method ? If I rename the .add()
method to something like .typeAdd()
referencing the private field doesn't throw an error