【发布时间】:2021-04-26 05:37:47
【问题描述】:
目标是创建一个可以扩展不同类的原型。 我需要创建一个扩展 Mesh 或 Points of THREE.js 的类。对于这两种情况,我创建的子类都是相同的。不能是接口。
class PointsElement extends THREE.Points {
public name: string
protected width: number
protected height: number
constructor (protected target: THREE.Scene | THREE.Group, protected properties: IElementProperties) {
super()
this.name = this.properties.name
AutoBind(this)
}
public async preload () {
}
public async show () {
this.target.add(this)
}
public async hide () {
this.target.remove(this)
}
}
class MeshElement extends THREE.Mesh {
public name: string
protected width: number
protected height: number
constructor (protected target: THREE.Scene | THREE.Group, protected properties: IElementProperties) {
super()
this.name = this.properties.name
AutoBind(this)
}
public async preload () {
}
public async show () {
this.target.add(this)
}
public async hide () {
this.target.remove(this)
}
}
我想要做的是通过排除“元素”来减少代码,因为 MeshElement 和 PointsElement 中的主体是相同的,但是这些类中的每一个都扩展了不同的类
【问题讨论】:
-
条件继承被普遍称为工厂模式
-
如果我必须返回带有附加参数的新网格或点,工厂模式会很有帮助。我需要创建具有元素属性和网格或点的类。像 'class MeshElement extends THREE.Mesh { ...Element.prototype }' 和 'class PoinstElement extends THREE.Points { ...Element.prototype }'。
标签: javascript typescript three.js