当前位置: 首页 > news >正文

【ES6】使用Proxy实现单例模式

前言

由于JS没有private关键字,无法私有化构造器,所以下面代码无法限制:

class Person {constructor() {console.log("Person created");}
}const p1 = new Person();
const p2 = new Person();console.log(p1 === p2); // false

在这里插入图片描述

实现

通过 Person.getInstance() 生成对象

class Person {constructor() {console.log("Person created");}static _ins = nullstatic getInstance() {if (!this._ins) {this._ins = new Person();}return this._ins;}
}const p1 = Person.getInstance();
const p2 = Person.getInstance();console.log(p1 === p2);

在这里插入图片描述

但是如果创建对象时使用new Person(),仍无法实现单例模式。

下面封装一个函数,把任何类传入,将其变为单例模式:

function singleton(className) {let insreturn class {constructor(...args) {if (!ins) {ins = new className(...args)}return ins}}
}
class Person {constructor() {console.log("Person created");}
}// const p1 = new Person();
// const p2 = new Person();
const SingletonPerson = singleton(Person);
const p1 = new SingletonPerson();
const p2 = new SingletonPerson();console.log(p1 === p2);

在这里插入图片描述

但是这种实现方式仍有缺陷,并不能添加原型方法

const SingletonPerson = singleton(Person);
const p1 = new SingletonPerson();
const p2 = new SingletonPerson();
SingletonPerson.prototype.say = function () {console.log("hello world");
}
p1.say();

在这里插入图片描述

下面使用 Proxy 实现,不返回一个新类,而是代理,给代理对象的原型上加方法等于直接给该对象的原型加方法。

function singleton(className) {let insreturn new Proxy(className, {construct(target, args) {if (!ins) {ins = new target(...args);}return ins}})
}
class Person {constructor() {console.log("Person created");}
}
const SingletonPerson = singleton(Person);
const p1 = new SingletonPerson();
const p2 = new SingletonPerson();
SingletonPerson.prototype.say = function () {console.log("hello world");
}
p1.say();
console.log(p1 === p2);

在这里插入图片描述

相关文章:

  • 北京网站建设多少钱?
  • 辽宁网页制作哪家好_网站建设
  • 高端品牌网站建设_汉中网站制作
  • 论文阅读:scMGCA----模型方法
  • Go语言Time包的使用
  • C. Propagating tree
  • HTML5 浏览器支持
  • XML CSS:结构和样式的完美结合
  • 秋招突击——8/16——字节广告业务——面经整理——二面挂
  • 【docker compose 部署和 go 热部署工具fresh】
  • git rebase和git merge的区别
  • EazyDraw for Mac 矢量图绘制设计软件
  • MySQL 学习笔记之事务操作
  • yd云手机登录算法分析
  • EXCEL 分段排序--Excel难题#86
  • 趣味算法------尾部零的个数(C语言,python双重解法)
  • 【微信小程序】使用 npm 包 - API Promise化-- miniprogram-api-promise
  • 出现Property ‘sqlSessionFactory‘ or ‘sqlSessionTemplate‘ are requiredProperty报错
  • [ JavaScript ] 数据结构与算法 —— 链表
  • ABAP的include关键字,Java的import, C的include和C4C ABSL 的import比较
  • Android框架之Volley
  • Lsb图片隐写
  • php ci框架整合银盛支付
  • Spring Boot快速入门(一):Hello Spring Boot
  • springboot_database项目介绍
  • Vue组件定义
  • WordPress 获取当前文章下的所有附件/获取指定ID文章的附件(图片、文件、视频)...
  • 前端 CSS : 5# 纯 CSS 实现24小时超市
  • 前端面试题总结
  • 通过git安装npm私有模块
  • 微信开放平台全网发布【失败】的几点排查方法
  • 微信小程序上拉加载:onReachBottom详解+设置触发距离
  • 一起参Ember.js讨论、问答社区。
  • 用Node EJS写一个爬虫脚本每天定时给心爱的她发一封暖心邮件
  • 在weex里面使用chart图表
  • 阿里云服务器如何修改远程端口?
  • 交换综合实验一
  • ​马来语翻译中文去哪比较好?
  • ​软考-高级-信息系统项目管理师教程 第四版【第23章-组织通用管理-思维导图】​
  • ​无人机石油管道巡检方案新亮点:灵活准确又高效
  • #php的pecl工具#
  • (6)添加vue-cookie
  • (BAT向)Java岗常问高频面试汇总:MyBatis 微服务 Spring 分布式 MySQL等(1)
  • (C++17) std算法之执行策略 execution
  • (C语言)求出1,2,5三个数不同个数组合为100的组合个数
  • (安卓)跳转应用市场APP详情页的方式
  • (超简单)使用vuepress搭建自己的博客并部署到github pages上
  • (二)测试工具
  • (官网安装) 基于CentOS 7安装MangoDB和MangoDB Shell
  • (亲测成功)在centos7.5上安装kvm,通过VNC远程连接并创建多台ubuntu虚拟机(ubuntu server版本)...
  • (贪心) LeetCode 45. 跳跃游戏 II
  • (五) 一起学 Unix 环境高级编程 (APUE) 之 进程环境
  • (已解决)vue+element-ui实现个人中心,仿照原神
  • (转)shell中括号的特殊用法 linux if多条件判断
  • ***详解账号泄露:全球约1亿用户已泄露
  • ..thread“main“ com.fasterxml.jackson.databind.JsonMappingException: Jackson version is too old 2.3.1
  • .NET MAUI Sqlite程序应用-数据库配置(一)
  • .net访问oracle数据库性能问题