mirror of
https://github.com/ascoders/weekly.git
synced 2026-02-01 17:46:46 +00:00
Update 171.精读《设计模式 - Singleton 单例模式》.md
代码示例问题调整,静态方法不允许使用this,调整静态类中this, 实例化属性采用静态属性。
This commit is contained in:
parent
2a310114a6
commit
60e607387a
@ -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;
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user