解决ts 中单例 使用时需要传入本身对象的问题
更优雅的使用 单例
export function Singleton<T>() {
class SingletonE {
protected constructor() {
}
private static instance: SingletonE = null;
public static get Instance(): T {
if (SingletonE.instance == null) {
SingletonE.instance = new this();
}
return SingletonE.instance as T;
}
}
return SingletonE;
}
///使用方式
class Test extends Singleton<Test>()
{
public TestGet() : string
{
return "";
}
}
Test.Instance.TestGet();