1、Singleton.ts
export function Singleton<E>() {
class SingletonE {
protected constructor() {}
private static _inst: SingletonE = null;
public static get inst(): E {
if(SingletonE._inst == null) {
SingletonE._inst = new this();
}
return SingletonE._inst as E;
}
}
return SingletonE;
}
2、Test.ts
class Test extends Singleton<Test>() {
abc = 0;
cocos() {
}
}
3、使用
Test.inst.abc;
Test.inst.cocos();