0

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?

jtwalters
  • 655
  • 4
  • 17
  • What are your criteria for "*is a valid class*"? Have you considered `child.constructor === B`? Though that can be faked too, and may fail over frames. – RobG Feb 20 '17 at 00:35
  • You can only pass the class "B" of "b.js" and "C" of "c.js". If if another class even with same name don't add it to the list. Like: `new function B(){};`. I get the child `instanceof require('./b')` like the best way. But I'm not sure if that is clean. – jtwalters Feb 20 '17 at 00:39
  • Why do you think the recursive import is a bad practice? You seem to be overly concerned with whether your code is "hackable". JavaScript is a dynamically typed language. The more you fight that with strict class types, the worse your code will get. – 4castle Feb 20 '17 at 01:46
  • Please, explain what's your final objective in more details. What you want to use it for? This will help to find a proper pattern. The code above will create `this.child` in both B and C. Is that a desirable behaviour? Do you really want to make B instance to have getBChilds that will return a different set of objects than A instance? Btw, constructor.name [is not minifiable](http://stackoverflow.com/questions/29310530/get-the-class-name-of-es6-class-instance). – Estus Flask Feb 20 '17 at 10:47

1 Answers1

0

I thinkk , I recommend the instanceof method and pass constructor function.

setChild(origin, child) {

    if (child instanceof origin) {
        this.child.b.push();
        return true;
    }

    if (child instanceof origin) {
        this.child.b.push();
        return true;
    }

    return false;
}
console.log(a.setChild(B,new B()));
console.log(a.setChild(B,new B()));
console.log(a.setChild(C,new C()));
console.log(a.setChild(A,new A()));
console.log(a.setChild(B,new (function B(){})));