I’d like to ask you about the correct approach to dealing with children’s classes that have their inner logic.
I have a class called MainClass. The purpose is to grab all smaller classes(called A class) and grab them into an array. On destroying it should do some stuff within itself(in the example code it triggers the console log) and later inform the MainClass to be removed from the array of active objects. There’s no connection to the DOM.
I have a few questions:
- How should I do this in plain Javascript? The only idea I have is to override one of the functions that is triggered inside the code, but I’m not sure if this is the correct approach.
- If this is the correct approach how should I pass the data to the MainClass? Currently, after pushing it to the array, I’m losing context.
- If I replace const with let in temporaryVar, override function outputCallback() in MainClass, push temporaryVar to the array, and then override it with another class will it be detectable by GarbageCollector on deleting the original class from the array?
class A {
name = 'class a';
destroyCallback() {
// do its stuff
console.log(this.name);
this.outputCallback()
}
outputCallback() {
// this should inform MainClass that it was finished + return some data
}
}
class MainClass {
classArr = [];
constructor() {
const temporaryVariable = new A();
// wrong attept - overrides inner function
temporaryVariable.outputCallback = () => {
// do some stuff
}
classArr.push(temporaryVariable );
}
}