typescript 打字稿初始化类类型的静态变量

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/24330620/
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-10-21 02:54:57  来源:igfitidea点击:

Typescript initialize static variable of a class type

typescript

提问by Chin

I have two classes Fooand Bar. In class BarI have a static variable called myFooand I want it to be automatically initialized:

我有两个班级FooBar. 在课堂上,Bar我有一个静态变量被调用myFoo,我希望它被自动初始化:

class Foo {
}

class Bar {
    static myFoo: Foo = new Foo();
}

However, I'm getting this error:

但是,我收到此错误:

Uncaught ReferenceError: Foo is not defined

未捕获的 ReferenceError: Foo 未定义

If I initialize that static variable in Bar's constructor then it works fine:

如果我在Bar的构造函数中初始化那个静态变量,那么它工作正常:

class Bar {
    static myFoo: Foo;

    constructor() {
         Bar.myFoo = new Foo();
    }
}

Why is that? What did I do wrong when I tried to initialize the static variable myFoodirectly?

这是为什么?当我尝试myFoo直接初始化静态变量时,我做错了什么?

回答by Jeffery Grajkowski

You definitely don't want to do that second thing because that's going to overwrite myFooevery time you construct a new Bar and you certainly don't want that.

您绝对不想做第二件事,因为myFoo每次构建新的 Bar 时它都会被覆盖,而您当然不希望那样做。

What you have here is a run time problem, not a compile time problem. The Fooclass has to be loadedbefore the Barclass is loaded otherwise the static initializer will fail. If both classes are in a single file in the above order it works. If the classes are in separate files and you tell TypeScript to compile to a single file it should figure out the correct order for you (though there are bugs in that area). If you're compiling to separate files you'll need to include the scripts on the page in the correct order to satisfy the dependency.

您在这里遇到的是运行时问题,而不是编译时问题。该Foo必须加载在之前Bar的类加载,否则静态初始化将失败。如果两个类都按上述顺序在一个文件中,则它可以工作。如果这些类位于单独的文件中并且您告诉 TypeScript 编译为单个文件,它应该为您找出正确的顺序(尽管该区域存在错误)。如果您要编译为单独的文件,则需要以正确的顺序在页面上包含脚本以满足依赖性。

回答by Oded Breiner

You can just have the call to initialize immediately follow the class declaration:

您可以在类声明之后立即调用 initialize :

class MyClass {
    static initialize() {
        // Initialization
    }
}
MyClass.initialize();

回答by Dave Cousineau

This requires JQuery, but is what I do to have the equivalent of a 'static constructor'.

这需要 JQuery,但这是我所做的等效于“静态构造函数”的工作。

namespace SomeNamespace {
   $(() => SomeClass.StaticConstructor());

   export class SomeClass {
      public static StaticConstructor() {
      }
   }
}

This is also useful as the "entry point" of your application, for example.

例如,这也可用作应用程序的“入口点”。

namespace SomeNamespace {
   $(() => SomeClass.Start());

   export class SomeClass {
      private static sInstance: SomeClass;

      public static Start() {
         SomeClass.sInstance = new SomeClass();
      }
   }
}