之前在网上搜了一些单例模式,使用的不是那么顺手,今天把自己感觉用得顺手的ts单例模式记录一下:
export default class PlayerController extends Laya.Script3D{
private static _inst: PlayerController;
public static get inst(): PlayerController {
if (this._inst == null) {
this._inst = new PlayerController();
}
return this._inst;
}
public is_over:boolean=false;
onAwake(){}
}
在其它地方调用:
var is_over=PlayerController.inst.is_over;

本文介绍了一种在TypeScript中实现单例模式的方法,通过PlayerController类展示了如何确保全局唯一实例,并提供了调用示例。
809

被折叠的 条评论
为什么被折叠?



