I have this code, each class in each file:
a.js:
Import B from './b'
Import C from './c'
class A {
constructor() {
this.child = {
b : [],
c : []
};
}
setChild(child) {
if (child instanceof B) {
this.child.b.push();
return true;
}
if (child instanceof C) {
this.child.b.push();
return true;
}
return false;
}
getBChilds() {
return this.child.b;
}
getCChilds() {
return this.child.c;
}
}
b.js:
Import A from './a'
class B extends A {
}
c.js:
Import A from './a'
class C extends A {
}
And the test code can be:
test.js
let a = new A();
console.log(a.setChild(new B()));
console.log(a.setChild(new B()));
console.log(a.setChild(new C()));
console.log(a.setChild(new A()));
console.log(a.setChild(new (function B(){})));
Where the correct answer is:
true
true
true
false
false
The problem is how I can detect if the variable of setChild()
is a valid class
But I don't think make a "recursive import" is a good practice. So I think they are more cleanner/better methods to do that.
I tried with:
switch (child.constructor.name) {
case "B":
this.child.b.push();
return true;
case "C":
this.child.b.push();
return true;
}
return false;
But I can hack this with new function B(){};
. So isn't solve the problem.
PD: In cases of the compiler donse't solve the "recursive imports" I can delete that and use:
child instanceof require('./b')
child instanceof require('./c')
But is it good?