从Typescript中的非静态方法调用静态方法
Calling static method from non-static method in Typescript
我的所有层次结构中都有一些静态内容(在本例中为 _image
)。我希望能够访问通讯员 _image
而无需重复代码:
那就太好了:
class Actor {
static _image; // I need it to be static
method show(){ // I need it to be non-static
this.setImage(this.class._image); //doesn't work....
}
}
class GoodActor extends Actor {
static _image = 'good.png'
}
class BadActor extends Actor {
static _image = 'bad.png'
}
class MediumActor extends Actor {
static _image = 'medium.png'
}
但是没用。现在我只需要:
class Actor {
}
class GoodActor extends Actor {
static _image = 'good.png' // I need it to be static
method show(){ // I need it to be non-static
this.setImage(GoodActor._image);
}
}
class BadActor extends Actor {
static _image = 'bad.png' // I need it to be static
method show(){ // I need it to be non-static
this.setImage(BadActor._image);
}
}
class MediumActor extends Actor {
static _image = 'medium.png' // I need it to be static
method show(){ // I need it to be non-static
this.setImage(MediumActor._image);
}
}
假设这四个 类 有更多的方法。我不想在每个子类中重复 show()
方法...但是我 需要 show()
方法 non static 和_image
将被静态访问。
我读过这个问题 https://github.com/Microsoft/TypeScript/issues/7673 但不幸的是我不能在那里问,因为他们没有修复它就关闭了它。 None 讲到这个需要动态解析要调用的静态方法的问题
更新: 为什么您不想在缓存图像之前构建单个对象?如果您使构建成本低廉,那么使用静态字段就没有任何好处。
示例:
class Actor {
image: string;
showImage() {
console.log(this.image);
}
}
class GoodActor extends Actor {
image = 'good.png';
}
class BadActor extends Actor {
image = 'bad.png';
}
const myActorTypes: (typeof Actor)[] = [GoodActor, BadActor];
function preloadImages() {
for (let myActorType of myActorTypes) {
preloadImage(new myActorType().image);
}
}
function preloadImage(image: string) {
console.log(`Loading ${image}`);
}
preloadImages();
// "Loading good.png"
// "Loading bad.png"
FTR,您可以访问当前对象的class。它被称为 constructor
,而不是 class
,您需要声明它,以便它具有比 Function
.
更有用的类型
class Actor {
static _image: string; // I need it to be static
// Keep static members, remove construct signature because
// subclasses may define constructors with different parameters.
"constructor": Pick<typeof Actor, keyof typeof Actor>;
show(){ // I need it to be non-static
this.setImage(this.constructor._image);
}
}
class GoodActor extends Actor {
static _image = 'good.png'
}
class BadActor extends Actor {
static _image = 'bad.png'
}
class MediumActor extends Actor {
static _image = 'medium.png'
}
我的所有层次结构中都有一些静态内容(在本例中为 _image
)。我希望能够访问通讯员 _image
而无需重复代码:
那就太好了:
class Actor {
static _image; // I need it to be static
method show(){ // I need it to be non-static
this.setImage(this.class._image); //doesn't work....
}
}
class GoodActor extends Actor {
static _image = 'good.png'
}
class BadActor extends Actor {
static _image = 'bad.png'
}
class MediumActor extends Actor {
static _image = 'medium.png'
}
但是没用。现在我只需要:
class Actor {
}
class GoodActor extends Actor {
static _image = 'good.png' // I need it to be static
method show(){ // I need it to be non-static
this.setImage(GoodActor._image);
}
}
class BadActor extends Actor {
static _image = 'bad.png' // I need it to be static
method show(){ // I need it to be non-static
this.setImage(BadActor._image);
}
}
class MediumActor extends Actor {
static _image = 'medium.png' // I need it to be static
method show(){ // I need it to be non-static
this.setImage(MediumActor._image);
}
}
假设这四个 类 有更多的方法。我不想在每个子类中重复 show()
方法...但是我 需要 show()
方法 non static 和_image
将被静态访问。
我读过这个问题 https://github.com/Microsoft/TypeScript/issues/7673 但不幸的是我不能在那里问,因为他们没有修复它就关闭了它。 None 讲到这个需要动态解析要调用的静态方法的问题
更新: 为什么您不想在缓存图像之前构建单个对象?如果您使构建成本低廉,那么使用静态字段就没有任何好处。
示例:
class Actor {
image: string;
showImage() {
console.log(this.image);
}
}
class GoodActor extends Actor {
image = 'good.png';
}
class BadActor extends Actor {
image = 'bad.png';
}
const myActorTypes: (typeof Actor)[] = [GoodActor, BadActor];
function preloadImages() {
for (let myActorType of myActorTypes) {
preloadImage(new myActorType().image);
}
}
function preloadImage(image: string) {
console.log(`Loading ${image}`);
}
preloadImages();
// "Loading good.png"
// "Loading bad.png"
FTR,您可以访问当前对象的class。它被称为 constructor
,而不是 class
,您需要声明它,以便它具有比 Function
.
class Actor {
static _image: string; // I need it to be static
// Keep static members, remove construct signature because
// subclasses may define constructors with different parameters.
"constructor": Pick<typeof Actor, keyof typeof Actor>;
show(){ // I need it to be non-static
this.setImage(this.constructor._image);
}
}
class GoodActor extends Actor {
static _image = 'good.png'
}
class BadActor extends Actor {
static _image = 'bad.png'
}
class MediumActor extends Actor {
static _image = 'medium.png'
}