Update 171.精读《设计模式 - Singleton 单例模式》.md

代码示例问题调整,静态方法不允许使用this,调整静态类中this, 实例化属性采用静态属性。
This commit is contained in:
ethan guo 2023-12-05 13:56:49 +08:00 committed by GitHub
parent 2a310114a6
commit 60e607387a
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

View File

@ -51,17 +51,17 @@ Redux 数据流的 `connect` 装饰器就是全局访问点的一种设计。
```typescript
class Ball {
private _instance = undefined
private static _instance: Ball;
// 构造函数申明为 private就可以阻止 new Ball() 行为
private constructor() {}
public static getInstance = () => {
if (this._instance === undefined) {
this._instance = new Ball()
public static getInstance() {
if (Ball._instance === undefined) {
Ball._instance = new Ball();
}
return this._instance
return Ball._instance;
}
}