[英]Create Child Class From Parent (ES6 Classes in Javascript)
I am trying to create a child (sub) class by initiating a parent class with the child type as a parameter, and I am wondering how to do this.我正在尝试通过以子类型作为参数启动父 class 来创建子(子)class,我想知道如何执行此操作。
For example, say I have the following starter code:例如,假设我有以下起始代码:
class Animal{
constructor(settings){
//parent value
this.name = settings.name;
}
//parent function
sayName(){
console.log(`My name is ${this.name}.`);
}
}
class Frog extends Animal{
constructor(settings){
super(settings);
//child-specific value
this.isTreeFrog = settings.isTreeFrog;
}
//child function
livesInTheForest(){
return this.isTreeFrog;
}
}
class Rabbit extends Animal{ [...] }
class Whale extends Animal{ [...] }
I want to be able to write:我希望能够写:
let barry = new Animal({
animalType: "frog",
name: "Barry",
isTreeFrog: false
})
(rather than let barry = new Frog({name: "Barry", isTreeFrog: false})
) (而不是let barry = new Frog({name: "Barry", isTreeFrog: false})
)
and have barry
be a frog, meaning I can write things like this:让barry
成为一只青蛙,这意味着我可以这样写:
barry.sayName() //should print 'My name is Barry'
console.log(barry.livesInTheForest()) //should print 'false'
I have tried two different ways to achieve this, but both are a bit hacky and don't achieve exactly what I want.我已经尝试了两种不同的方法来实现这一点,但两者都有点老套,并没有完全达到我想要的效果。
The first involves having a value in the Animal class which stores the child in. For example, in the constructor for Animal
I might have something like this:第一个涉及在存储孩子的 Animal class 中有一个值。例如,在Animal
的构造函数中,我可能有这样的东西:
if(settings.animalType === "frog"){
this.animal = new Frog(settings);
}else [...] //other animal types
There are two main problems with this:这有两个主要问题:
barry.animal.livesInTheForest()
, which creates inconsistency as the parent functions can be called without the .animal
.我必须像这样调用子函数: barry.animal.livesInTheForest()
,这会造成不一致,因为可以在没有.animal
的情况下调用父函数。Frog
) can no longer be child classes, as otherwise I will get too much recursion as it keeps trying to call itself with super().子类(例如Frog
)不能再是子类,否则我会得到太多的递归,因为它一直试图用 super() 调用自己。I thought of a second method as well, which works like this:我也想到了第二种方法,它的工作原理是这样的:
In the parent ( Animal
) constructor:在父( Animal
)构造函数中:
//make sure this isn't being called from the child class
if(settings.animalType !== null){
if(settings.animalType === "frog"){
//set settings.animal null to avoid too much recursion
//this means that I can't set this.animalType though, so I can't access barry.animalType
settings.animalType = null;
//Is this something I can do?!
this = new Frog(settings);
} else [...] //other animal types
}
This works (I think), but I now can't set this.animalType
to settings.animalType
, meaning I can't write barry.animalType
and get frog
.这可行(我认为),但我现在无法将this.animalType
设置为settings.animalType
,这意味着我无法编写barry.animalType
并获取frog
。 Also, this seems really hacky to me and I can't help thinking that there must be a better way to do this.此外,这对我来说似乎真的很老套,我忍不住想一定有更好的方法来做到这一点。
class Animal {
static create (settings) {
return new this.subClasses[settings.type](settings)
}
}
class Rabbit extends Animal {}
class Frog extends Animal {}
class Whale extends Animal {}
Animal.subClasses = { frog: Frog, rabbit: Rabbit, whale: Whale }
const animals = ['frog', 'rabbit', 'whale'].map((type) => Animal.create({ type }))
console.log({ animals })
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.