// 抽象基类,使用泛型来正确推导子类类型 class BaseSingleton { static _instance constructor() { if (this.constructor === BaseSingleton) { throw new Error("禁止直接实例化 BaseOne 抽象类") } if (this.constructor._instance) { throw new Error("构造函数私有化失败,禁止重复 new") } // this.constructor 是子类,所以这里设为 instance this.constructor._instance = this } static getInstance() { const clazz = this if (!clazz._instance) { const self = new this() const handler = { get: function (target, prop) { const value = Reflect.get(target, prop) if (typeof value === "function") { return value.bind(target) } return Reflect.get(target, prop) }, } clazz._instance = new Proxy(self, handler) } return clazz._instance } } export { BaseSingleton }