简体   繁体   English

TypeScript:访问类中的静态方法(相同的或不同的)

[英]TypeScript: Access static methods within classes (the same or another ones)

Suppose we have the following code:假设我们有以下代码:

[Test.js file]:
class Test {
    ...
    public static aStaticFunction():void {
          ...

           this.aMemberFunction(); // <- Issue #1.
    }

    private aMemberFunction():void {
          ...

          this.aStaticFunction(); // <- Issue #2.
    }
}

[Another.js file]:
class Another {
    ...

    private anotherMemberFunction():void {
          Test.aStaticFunction(); // <- Issue #3.
    }
}

See the Issue #x.请参阅Issue #x. comments for the issues (3) I want to address.对问题的评论 (3) 我想解决的问题。

I've been playing with some configurations by now and I don't get it all yet.我现在一直在玩一些配置,但我还没有完全掌握。

Can you help me to understand how can I access this methods in the three places?你能帮我理解我如何在三个地方访问这个方法吗?

Thanks.谢谢。

There is some code below, but there are some important concepts to bear in mind.下面有一些代码,但有一些重要的概念需要记住。

A static method does not exist on any instance.任何实例上都不存在静态方法。 There are good reasons for this:这有充分的理由:

  1. It can be called before you have created a new instance可以在创建new实例之前调用它
  2. It can be called from outside an instance, so you wouldn't know which instance the call was related to它可以从实例外部调用,因此您不会知道该调用与哪个实例相关

So in all cases where you call the static method, you need to use the full name:所以在所有调用静态方法的情况下,都需要使用全名:

Test.aStaticFunction();

If the static method needs to call an instance method, you need to pass that in. This does set off alarm bells for me though.如果静态方法需要调用实例方法,则需要将其传入。不过,这确实为我敲响了警钟。 If the static method depends on an instance method, it probably shouldn't be a static method.如果静态方法依赖于实例方法,则它可能不应该是静态方法。

To see what I mean, think about this problem.要明白我的意思,请考虑这个问题。

If I call Test.aStaticFunction() from outside of an instance, when 100 instances have been created, which instance should the static function use?如果我从实例外部调用Test.aStaticFunction() ,当创建了 100 个实例时,静态函数应该使用哪个实例? There is no way of telling.没有办法告诉。 If your method needs to know data from the instance or call methods on the instance, it almost certainly shouldn't be static.如果您的方法需要知道来自实例的数据或调用实例上的方法,它几乎肯定不应该是静态的。

So although the code below works, it probably isn't really what you require - what you probably need is to remove the static keyword and make sure you have an instance to call in your other classes.因此,尽管下面的代码有效,但它可能并不是您真正需要的 - 您可能需要的是删除static关键字并确保您有一个实例可以在其他类中调用。

interface IHasMemberFunction {
    aMemberFunction(): void;
}

class Test {
    public static aStaticFunction(aClass: IHasMemberFunction):void {
           aClass.aMemberFunction();
    }

    private aMemberFunction():void {
          Test.aStaticFunction(this);
    }
}

class Another {
    private anotherMemberFunction():void {
          Test.aStaticFunction(new Test());
    }
}

this is related to an instance whereas static members are independent of any instance. this与实例相关,而static成员独立于任何实例。 So if you want to access members of an instance within a static member you have to pass it in. However in that case I don't see a reason for having a static member in the first place.因此,如果您想在静态成员中访问实例的成员,则必须将其传入。但是在这种情况下,我首先看不出有静态成员的原因。 I believe you need two functions.我相信你需要两个功能。 one static and one non-static.一种是静态的,一种是非静态的。 That do two different things, so :那做两件不同的事情,所以:

class Test {

    public  notaStaticFunction():void {
           this.aMemberFunction(); // <- Issue #1.
    }

    public static aStaticFunction():void {

    }

    private aMemberFunction():void {
          this.notaStaticFunction(); // <- Issue #2.
    }
}

class Another {
    private anotherMemberFunction():void {
          Test.aStaticFunction(); // <- Issue #3.
    }
}

That said you can share properties between static and member functions using static properties.也就是说,您可以使用静态属性在静态和成员函数之间共享属性。

dont use class name like Class.staticMethod(), use this:不要使用像 Class.staticMethod() 这样的类名,使用这个:

this.constructor.staticMethod()

to maintain the inheritance of static methods维护静态方法的继承

Edit: as mentioned in comments, typescript does not support this.constructor .编辑:如评论中所述,打字稿不支持this.constructor There is an open ticket in it's issue tracker, but not much progress over last 5 years.它的问题跟踪器中有一张未解决的票,但在过去 5 年中进展不大。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM