0

Is there a way in Javascript to make an Object containing expressions of both classes and subclasses of it? I tried this way, but I guess I can't refer to the superclass this way.

const myClasses = {
    'animal': class {
        a;
        b;

        constructor(x, y) {
            this.a = x;
            this.a = y;
        }
    },
    'dog': class extends animal {
        constructor(d, e) {
            super(d, e);
        }

        bark () {
            console.log('woof!');
        }
    }
}
oelna
  • 2,210
  • 3
  • 22
  • 40
  • 1
    What is the need to do this? What are you trying to solve with this solution? – evolutionxbox Jul 01 '22 at 15:22
  • For a JS game I have multiple assets that I tried to organize into an object. The goal was to be able to spawn multiple new instances of the objects, like `const dog1 = myClasses.dog()`. – oelna Jul 01 '22 at 15:27
  • What benefit does that bring over `const dog1 = new Dog()`? – evolutionxbox Jul 01 '22 at 15:29
  • 2
    You can not access `animal` before it is created so you need to rethink what you are doing. – epascarello Jul 01 '22 at 15:29
  • Yeah, I guess you are right. I posted my solution (create `animal` first) as an answer. I hope people who end up here can take it as an opportunity to rethink what they're doing, too. Thank you! – oelna Jul 01 '22 at 15:36

2 Answers2

1

It looks like the OP is aiming for an index of classes that can be instantiated based on a key. How about, define the classes as you normally would, then refer to them in the index...

class animal {
  a;
  b;

  constructor(x, y) {
    this.a = x;
    this.a = y;
  }
}

class dog extends animal {
  constructor(d, e) {
    super(d, e);
  }

  bark() {
    console.log('woof!');
  }
}

const myClasses = { animal, dog };

const myAnimalName = 'dog';
const klass = myClasses[myAnimalName];

const myAnimal = new klass()
myAnimal.bark()
danh
  • 62,181
  • 10
  • 95
  • 136
  • I guess I'd call it directly as `const myDog = new myClasses['dog'];`, but yeah, this looks like a clean solution! Thanks! – oelna Jul 01 '22 at 15:47
  • 1
    Sure, wasn't sure how abstract you wanted to be, even more tersely: `const myDog = new myClasses.dog()` – danh Jul 01 '22 at 15:50
0

It feels a bit weird, but a solution to this was to split it up into multiple steps. If the super class is declared, it can be referred to in a second step, obviously.

MDN: Class expressions must be declared before they can be used

const myClasses = {
    'animal': class {
        a;
        b;

        constructor(x, y) {
            this.a = x;
            this.a = y;
        }
    }
}

myClasses.dog = class extends myClasses.animal {
    constructor(d, e) {
        super(d, e);
    }

    bark () {
        console.log('woof!');
    }
}

If there is a way to do it in one step, I'd still like to know.

oelna
  • 2,210
  • 3
  • 22
  • 40