JavaScript中类和子类表达式的对象

发布于 2025-02-12 02:40:05 字数 466 浏览 2 评论 0原文

JavaScript中有没有办法制作一个对象,其中包含子类的表达式?我尝试过这种方式,但我想我不能以这种方式参考超级阶级。

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!');
        }
    }
}

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!');
        }
    }
}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

春庭雪 2025-02-19 02:40:05

看起来OP的目的是建立一个可以根据键实例化的类索引。怎么样,按照往常来定义类,然后在索引中参考它们...

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()

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()

第七度阳光i 2025-02-19 02:40:05

感觉有些怪异,但是解决方案是将其分为多个步骤。如果声明了超级类,则可以在第二步中提及。

mdn> mdn> mdn :必须声明类表达式在可以使用

之前

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!');
    }
}

一种方法,我仍然想知道。

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文