单例模式
传统的单例模式可以用来解决所有代码必须写到 class
中的问题:
class Singleton {
private static instance: Singleton;
private constructor() {
// ..
}
public static getInstance() {
if (!Singleton.instance) {
Singleton.instance = new Singleton();
}
return Singleton.instance;
}
someMethod() {}
}
let someThing = new Singleton(); // Error: constructor of 'singleton' is private
let instacne = Singleton.getInstance(); // do some thing with the instance
然而,如果你不想延迟初始化,你可以使用 namespace
替代:
namespace Singleton {
// .. 其他初始化的代码
export function someMethod() {}
}
// 使用
Singleton.someMethod();
::: warning 单例只是全局的一个别称。 :::
对大部分使用者来说,namespace
可以用模块来替代。
// someFile.ts
// ... any one time initialization goes here ...
export function someMethod() {}
// Usage
import { someMethod } from './someFile';
看完两件小事
如果你觉得这篇文章对你挺有启发,我想请你帮我两个小忙:
- 把这篇文章分享给你的朋友 / 交流群,让更多的人看到,一起进步,一起成长!
- 关注公众号 「IT平头哥联盟」,公众号后台回复「资源」 免费领取我精心整理的前端进阶资源教程