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

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/18554683/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-11 01:08:25  来源:igfitidea点击:

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

javascripttypescriptstatic-methods

提问by diosney

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.comments for the issues (3) I want to address.

请参阅Issue #x.我要解决的问题 (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.

谢谢。

回答by Fenton

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

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? 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.

如果我Test.aStaticFunction()从实例外部调用,当创建了 100 个实例时,静态函数应该使用哪个实例?没有办法告诉。如果您的方法需要了解来自实例的数据或调用实例上的方法,那么它几乎肯定不应该是静态的。

So although the code below works, it probably isn't really what you require - what you probably need is to remove the statickeyword 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());
    }
}

回答by basarat

thisis related to an instance whereas staticmembers are independent of any instance. 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 :

this与实例相关,而static成员独立于任何实例。因此,如果您想访问静态成员中的实例成员,则必须将其传入。但是在这种情况下,我首先看不出有静态成员的原因。我相信你需要两个功能。一种是静态的,一种是非静态的。那做两件不同的事情,所以:

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.

也就是说,您可以使用静态属性在静态和成员函数之间共享属性。

回答by Maxmaxmaximus

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. There is an open ticketin it's issue tracker, but not much progress over last 5 years.

编辑:如评论中所述,打字稿不支持this.constructor. 它的问题跟踪器中有一张未解决的票,但在过去 5 年中进展不大。