这在 Typescript 中是一个不好的做法吗?我有什么选择?
我希望有一个一致的类实例(本例中的集合)可以在整个项目中使用。 我正在使用 Typescript,所以我想解决在没有任何类型安全的情况下以 JavaScript 风格创建一些奇怪对象的概念。
由于我不需要在任何地方传递此类的真实对象,因此我完全同意拥有一个仅包含静态成员的类的想法,我可以通过调用类名在项目中访问该类。 这个概念有什么问题吗? 我应该使用单例模式,然后在文件末尾执行 export Collection.getInstance()
吗?
export class CollectionInstance{
static readonly exampleCollection = new ExampleCollection();
//noinspection JSUnusedLocalSymbols
private constructor() {
}
public static load(){
}
public static get(key: string){
}
}
I want to have a consistent instance of a class (a collection in this example) to work in the whole project with.
I am using Typescript so I want to work around the concept of creating some wonky object in the JavaScript-style without any type safety.
As I don't need to pass a real object of this class anywhere, I am completely fine with the idea of having a class with only static members, which I can access in the project by calling the class name.
Is there anything wrong with this concept?
Should I use a singleton pattern and then just do export Collection.getInstance()
at the end of the file?
export class CollectionInstance{
static readonly exampleCollection = new ExampleCollection();
//noinspection JSUnusedLocalSymbols
private constructor() {
}
public static load(){
}
public static get(key: string){
}
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论