I have the following class and I cannot change it
class Foo {
constructor() {
console.log('foo')
}
}
I would like to patch it using a function so that it extends another class (calling super() as the first line of it's constructor)
function extendWithBar(TargetCtor) {
class Bar {
constructor() {
console.log('bar')
}
}
//... secret sauce
// Equivalent of "Foo extends Bar"
// can extend other classes too
return TargetExtendingBar
}
// FooBar is a new constructor, it doesn't need to relate to Foo
// it only needs to have the same constructor
const FooBar = extendBar(Foo)
const foobar = new FooBar()
// Output:
// "bar"
// "foo"
Is this possible in JavaScript, if so, how?