跳过类控制,直接控制变量,实现单例自由
function singleton(target: any, propertyKey: string) {
const privateKey = _${propertyKey};
Object.defineProperty(target, propertyKey, {
get: function () {
if (!this[privateKey]) {
this[privateKey] = new this();
}
return this[privateKey];
},
configurable: false,
enumerable: false,
});
}
export class ClassTest {
@singleton
public static instance: ClassTest;
public test() {
cc.log('ClassTest');
}
}
