TypeScript singleton pattern All In One
TypeScript singleton pattern All In One
singleton pattern / 单例模式
/**
*
* @author xgqfrms
* @license MIT
* @copyright xgqfrms
* @created 2022-03-31
* @modified
*
* @description
* @augments
* @example
* @link
*
*/
const log = console.log;
// 只能创建单个类的实例
class Human {
// 私有构造器函数,不可以通过 new 创建类的实例
constructor(name) {
this.name = name;
}
// public static createInstance() {
static createInstance(name) {
if (!Human.isExist) {
Human.isExist = true;
this.instance = new Human(name);
}
else {
// ignore
}
return this.instance;
}
}
Human.isExist = false;
// const human = new Human('eric');
// Constructor of class 'Human' is private and only accessible within the class declaration.ts(2673)
const h1 = Human.createInstance('abc');
const h2 = Human.createInstance('xyz');
log('h1 name', h1.name);
log('h2 name', h2.name);
// h1 name abc
// h2 name abc
/**
*
* @author xgqfrms
* @license MIT
* @copyright xgqfrms
* @created 2022-03-31
* @modified
*
* @description
* @augments
* @example
* @link
*
*/
const log = console.log;
class Human {
// 私有构造器函数,不可以通过 new 创建类的实例
constructor(name) {
this.name = name;
}
// public static createInstance() {
static createInstance(name) {
if (!Human.isExist) {
Human.isExist = true;
Human.instance = new Human(name);
}
else {
// ignore
}
return Human.instance;
}
}
Human.isExist = false;
// const human = new Human('eric');
// Constructor of class 'Human' is private and only accessible within the class declaration.ts(2673)
const h1 = Human.createInstance('abc');
const h2 = Human.createInstance('xyz');
log('h1 name', h1.name);
log('h2 name', h2.name);
// h1 name abc
// h2 name abc
demo
"use strict";
/**
*
* @author xgqfrms
* @license MIT
* @copyright xgqfrms
* @created 2022-03-31
* @modified
*
* @description
* @augments
* @example
* @link
*
*/
const log = console.log;
class Person {
// public name: string;
name: string
constructor(name: string) {
this.name = name;
}
}
// 可以创建多个不同的类的实例
const p1 = new Person('abc');
const p2 = new Person('xyz');
// singleton pattern 单例模式
// 只能创建单个类的实例
class Human {
private static isExist: boolean = false;
private static instance: Human;
// public name: string
name: string
// 私有构造器函数,不可以通过 new 创建类的实例
private constructor(name: string) {
this.name = name;
}
// public static createInstance() {
static createInstance(name: string) {
if (!Human.isExist) {
Human.isExist = true;
Human.instance = new Human(name);
} else {
// ignore
}
return Human.instance;
}
}
// const human = new Human('eric');
// Constructor of class 'Human' is private and only accessible within the class declaration.ts(2673)
const h1 = Human.createInstance('abc');
const h2 = Human.createInstance('xyz');
log('h1 name', h1.name);
log('h2 name', h2.name);
export {}
"use strict";
/**
*
* @author xgqfrms
* @license MIT
* @copyright xgqfrms
* @created 2022-03-31
* @modified
*
* @description
* @augments
* @example
* @link
*
*/
const log = console.log;
class Person {
constructor(name) {
this.name = name;
}
}
// 可以创建多个不同的类的实例
const p1 = new Person('abc');
const p2 = new Person('xyz');
// singleton pattern 单例模式
// 只能创建单个类的实例
class Human {
// 私有构造器函数,不可以通过 new 创建类的实例
constructor(name) {
this.name = name;
}
// public static createInstance() {
static createInstance(name) {
if (!Human.isExist) {
Human.isExist = true;
Human.instance = new Human(name);
}
else {
// ignore
}
return Human.instance;
}
}
Human.isExist = false;
// const human = new Human('eric');
// Constructor of class 'Human' is private and only accessible within the class declaration.ts(2673)
const h1 = Human.createInstance('abc');
const h2 = Human.createInstance('xyz');
log('h1 name', h1.name);
log('h2 name', h2.name);
export {};
class Human {
// 初始化
private static isExist: boolean = false;
// 未初始化
private static instance: Human;
// public name: string
name: string
// 私有构造器函数,不可以通过 new 创建类的实例
private constructor(name: string) {
this.name = name;
}
// public static createInstance() {
static createInstance(name: string) {
if (!Human.isExist) {
Human.isExist = true;
// this.instance = new Human(name);
Human.instance = new Human(name);
} else {
// ignore
}
console.log(this.instance === Human.instance);
// return this.instance;
return Human.instance;
}
}
// const human = new Human('eric');
// Constructor of class 'Human' is private and only accessible within the class declaration.ts(2673)
const h1 = Human.createInstance('abc');
const h2 = Human.createInstance('xyz');
log('h1 name', h1.name);
log('h2 name', h2.name);
js singleton pattern
ES5 / ES6
// IIFE
var Singleton = (function () {
var instance;
// 闭包 closure
function createInstance() {
var object = new Object("I am the instance");
return object;
}
return {
// 单例模式 getInstance
getInstance: function () {
if (!instance) {
// 实例化 1 次
instance = createInstance();
}
return instance;
}
};
})();
function run() {
var instance1 = Singleton.getInstance();
var instance2 = Singleton.getInstance();
console.log("Same instance? " + (instance1 === instance2));
}
js custom new keyword
// 1. create object, allocate memory
// 2. bind this
// 3. bind prototype
// 4. return object
function CustomNew (Func, args) {
// const obj = Object.create(func);
const obj = {};
// const obj = Object.assign({});
Func.call(obj, args);
// Func.call(obj, ...args);
// Func.apply(obj, [...args]);
obj.__proto__ = Func.prototype;
return obj;
}
https://www.cnblogs.com/xgqfrms/tag/new
https://www.cnblogs.com/xgqfrms/p/15998513.html
https://www.cnblogs.com/xgqfrms/p/13717265.html
refs
https://www.typescriptlang.org/play
©xgqfrms 2012-2025
www.cnblogs.com/xgqfrms 发布文章使用:只允许注册用户才可以访问!
原创文章,版权所有©️xgqfrms, 禁止转载 🈲️,侵权必究⚠️!
本文首发于博客园,作者:xgqfrms,原文链接:https://www.cnblogs.com/xgqfrms/p/16084639.html
未经授权禁止转载,违者必究!
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 10年+ .NET Coder 心语 ── 封装的思维:从隐藏、稳定开始理解其本质意义
· 地球OL攻略 —— 某应届生求职总结
· 周边上新:园子的第一款马克杯温暖上架
· 提示词工程——AI应用必不可少的技术
· Open-Sora 2.0 重磅开源!
2021-03-31 如何在 uni-app 里面使用 echarts All In One
2021-03-31 Jest & TypeScript
2021-03-31 auto convert cURL to Fetch All In One
2021-03-31 企业微信如何添加机器人 All In One
2020-03-31 移动端 750px UI 设计稿
2020-03-31 classnames & React & taro All In One
2020-03-31 taro external-class