I want to create a modular class. It should be usable like following example and should be seperatable into files. The sub class (Tasks
) should have access to the methods of the parent class (Foo
).
// Use basic methods
const foo = new Foo();
foo.connect();
foo.authenticate('user', 'password');
// Use task (or any other) module
const tasks = foo.Tasks;
tasks.createTask('ask on stackoverflow');
tasks.updateTask({ done: true });
I only got the following working. But because I have to initiate a new instance of Bar
with the new
keyword I can't access the changed value of this.output
. How can I omit the new
keyword and use the same instance as foo
with my desired syntax above?
const Foo = class Foo {
constructor() {
this.output = 1;
}
changeOutput(output) {
this.output = output;
}
}
Foo.Bar = class Bar extends Foo {
getOutput() {
return this.output;
}
}
const foo = new Foo();
foo.changeOutput(2);
const bar = new Foo.Bar(); // Creates a new instance what is wrong, but foo.Bar() or anything else doesn't work (Error: is not a function).
console.log(bar.getOutput()); // Result is 1, but should be 2