I've noticed error when a constructor in an inherited class leads to the invocation of a derived private method. Instead of being able to call the method, it fails with a TypeError. Example:
class Base {
constructor () {
this.method()
}
method() {}
}
class Derived extends Base {
#priv(){}
method() {
this.#priv()
}
}
new Derived()
// Chrome 97: Uncaught TypeError: Object must be an instance of class Derived
// Firefox 97: Uncaught TypeError: can't access private field or method: object is not the right class
// Safari 15.3: TypeError: Cannot access private method or acessor (evaluating 'this.#priv')
Shouldn't the instance in Base's constructor be a Derived object when constructing via the Derived constructor?
I originally noticed this trying to extend Map
.
class MyMap extends Map {
#log(name){
console.log(this, 'called', name)
}
set(key, value) {
this.#log('set')
return super.set(key, value)
}
}
new MyMap([['a', 1]]) // TypeError ...
This happens with each of the keyed collections because they each internally call set()
in their constructors (assuming they're provided iterables to pull from).