【问题标题】:TypeScript: Access static methods within classes (the same or another ones)TypeScript:访问类中的静态方法(相同或其他)
【发布时间】:2013-09-04 11:14:51
【问题描述】:

假设我们有以下代码:

[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.
    }
}

请参阅Issue #x. cmets 了解我想要解决的问题 (3)。

我一直在玩一些配置,但我还没有完全理解。

你能帮我理解一下我怎样才能在这三个地方访问这个方法?

谢谢。

【问题讨论】:

    标签: javascript typescript static-methods


    【解决方案1】:

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

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

    【讨论】:

    • 如果静态函数需要引用同一个父类中的另一个静态函数呢?
    • @diosney - 一旦静态函数依赖于实例函数,它就不再是静态的。可以在创建任何实例之前调用静态函数。
    【解决方案2】:

    下面有一些代码,但有一些重要的概念需要牢记。

    任何实例上都不存在静态方法。这有充分的理由:

    1. 可以在创建new 实例之前调用
    2. 它可以从实例外部调用,因此您不会知道该调用与哪个实例相关

    所以在所有调用静态方法的情况下,都需要使用全名:

    Test.aStaticFunction();
    

    如果静态方法需要调用实例方法,则需要将其传入。不过,这确实给我敲响了警钟。如果静态方法依赖于实例方法,它可能不应该是静态方法。

    要明白我的意思,想想这个问题。

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

    因此,尽管下面的代码有效,但它可能不是您真正需要的 - 您可能需要删除 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());
        }
    }
    

    【讨论】:

      【解决方案3】:

      不要使用像 Class.staticMethod() 这样的类名,使用这个:

      this.constructor.staticMethod()
      

      保持静态方法的继承

      编辑:如 cmets 中所述,打字稿不支持this.constructor。它的问题跟踪器中有一个open ticket,但过去 5 年没有太大进展。

      【讨论】:

      • 那是我的反对意见。问题是关于 TypeScript,而不是关于 JavaScript。你真的尝试过你的建议吗?它甚至不会编译。有关更多详细信息,请参阅此issue。也因为不回答实际操作的问题而投反对票。一旦你有足够的声望,就使用 cmets 作为旁注。
      • 我同意这里的观点。定义分配类引用的自定义属性将提供对this 类的静态内容的访问。
      猜你喜欢
      • 2022-06-25
      • 2012-01-02
      • 1970-01-01
      • 1970-01-01
      • 2023-03-07
      • 2020-05-07
      • 2013-04-18
      • 1970-01-01
      • 2016-02-25
      相关资源
      最近更新 更多