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

iis做网站软文广告图片

iis做网站,软文广告图片,优化网站哪家好,第三方网站建设单例模式在 JavaScript 中是一种确保类只有一个实例,并提供全局访问点的方式。由于 JavaScript 的语言特性(如对象字面量、模块系统等),实现单例有多种方式。 常见实现方式 1. 对象字面量(最简单的单例) …

单例模式在 JavaScript 中是一种确保类只有一个实例,并提供全局访问点的方式。由于 JavaScript 的语言特性(如对象字面量、模块系统等),实现单例有多种方式。

常见实现方式

1. 对象字面量(最简单的单例)

const singleton = {property: "value",method: function() {console.log("I am a method");}
};// 使用
singleton.method();

特点

  • 对象字面量本身就是单例

  • 无法延迟初始化

  • 不能私有化成员

2. 闭包实现(带私有成员)

const Singleton = (function() {// 私有变量let instance;function init() {// 私有方法和属性const privateVar = "I am private";const privateRandom = Math.random();return {// 公共方法publicMethod: function() {console.log("Public can see me");},// 公共属性publicProperty: "I am public",// 访问私有变量的方法getPrivateVar: function() {return privateVar;},getRandomNumber: function() {return privateRandom;}};}return {getInstance: function() {if (!instance) {instance = init();}return instance;}};
})();// 使用
const instance1 = Singleton.getInstance();
const instance2 = Singleton.getInstance();console.log(instance1 === instance2); // true
console.log(instance1.getRandomNumber() === instance2.getRandomNumber()); // true

特点

  • 真正的单例实现

  • 可以包含私有成员

  • 延迟初始化

  • 线程安全(JS是单线程)

3. ES6 Class 实现

class Singleton {constructor() {if (!Singleton.instance) {this._data = [];Singleton.instance = this;}return Singleton.instance;}add(item) {this._data.push(item);}get(id) {return this._data.find(item => item.id === id);}
}// 使用
const instance1 = new Singleton();
const instance2 = new Singleton();instance1.add({ id: 1, name: "test" });
console.log(instance2.get(1)); // { id: 1, name: "test" }
console.log(instance1 === instance2); // true

4. 模块模式(现代JS常用方式)

// singleton.js
let instance = null;
let data = []; // 私有变量export default class Singleton {constructor() {if (!instance) {instance = this;}return instance;}add(item) {data.push(item);}get(id) {return data.find(item => item.id === id);}getAll() {return [...data];}
}// 使用
import Singleton from './singleton.js';const instance1 = new Singleton();
const instance2 = new Singleton();instance1.add({ id: 1, name: "Item 1" });
console.log(instance2.getAll()); // [{ id: 1, name: "Item 1" }]

实际应用场景

  1. 全局状态管理

    // 类似Redux的store就是单例
    const store = {state: { count: 0 },increment() {this.state.count++;}
    };
  2. 对话框/模态框管理

    const Modal = (function() {let instance;function createModal() {const modal = document.createElement('div');// 初始化模态框...return {open: () => modal.style.display = 'block',close: () => modal.style.display = 'none'};}return {getInstance: function() {if (!instance) {instance = createModal();}return instance;}};
    })();
  3. 缓存系统

    const Cache = {data: {},set(key, value) {this.data[key] = value;},get(key) {return this.data[key];},clear() {this.data = {};}
    };

注意事项

  1. 模块系统本身就是单例:在现代JavaScript中,ES6模块默认就是单例

    // logger.js
    export default {log(message) {console.log(message);}
    }// 无论多少次import,得到的都是同一个对象
  2. 测试困难:单例可能导致测试困难,因为状态是共享的

  3. 全局污染:过度使用单例可能导致命名冲突和难以追踪的依赖关系

  4. Node.js中的单例:在Node.js中,模块缓存确保require多次返回同一个实例

JavaScript中的单例模式相比传统面向对象语言更灵活,可以根据具体需求选择适合的实现方式。


文章转载自:

http://cV7xvZFD.wqfzx.cn
http://y8WUZLIX.wqfzx.cn
http://b19xaoxQ.wqfzx.cn
http://JcAyQCbo.wqfzx.cn
http://NQ8XShhO.wqfzx.cn
http://KihWTtYZ.wqfzx.cn
http://t9OSS6rl.wqfzx.cn
http://o6Ogq9Ie.wqfzx.cn
http://S5BwhEIN.wqfzx.cn
http://cnvUKocu.wqfzx.cn
http://xGfBoFJj.wqfzx.cn
http://GNekXn8n.wqfzx.cn
http://hnvVFaGX.wqfzx.cn
http://sInvQxaJ.wqfzx.cn
http://vjN4ypMq.wqfzx.cn
http://EnEevd0t.wqfzx.cn
http://PIZYdRcC.wqfzx.cn
http://LwSgRbnR.wqfzx.cn
http://4A67ZRSx.wqfzx.cn
http://j4BFaqzy.wqfzx.cn
http://IPOqRE34.wqfzx.cn
http://arng7s8N.wqfzx.cn
http://clpjcWre.wqfzx.cn
http://ooj3mDAZ.wqfzx.cn
http://TZxRJ3iE.wqfzx.cn
http://Nvn7sRTY.wqfzx.cn
http://xal6wnhu.wqfzx.cn
http://J4507WcB.wqfzx.cn
http://PGCqc5yj.wqfzx.cn
http://7DS5mCbb.wqfzx.cn
http://www.dtcms.com/wzjs/732241.html

相关文章:

  • 麻章手机网站建设公司文员短期电脑培训
  • 广州网站推广哪家强广告设计公司核心优势
  • 做网站ie10缓存软件项目管理是做什么
  • 常德公交网站柳市网站优化
  • wordpress 音乐站主题网站推广软件免费版大全
  • 站长工具seo综合查询怎么使用的微信推广平台怎么做
  • 网站建设公司的职责文章网站哪里建设好
  • 自己做免费网站百度统计网站速度诊断
  • 营销型网站制作流程外贸业务员招聘人才网
  • 网站方案设计温州做网店的网站
  • 娱乐网站制作上海设计展2021室内设计
  • 网站开发里程碑哪里建设网站不会被封
  • 广州建网站培训国内免费saas+crm
  • 网站搭建开发常见网站推广方式
  • 手机网站和app的区别wordpress 手机支付
  • 牛商网网站做seo好么青岛鲁icp 网站制作 牛商网
  • 找人做网站大概多少钱中国建设质量网官方网站
  • 电脑自己做网站可以吗网站空间在哪申请
  • 网站开发设计的阶段六枝特区建设局网站
  • 英文公司网站模板南通网站建设设计
  • 专业网站设计开发公司帝国cms怎么做电影网站
  • 福州英文网站建设做一个网站的市场价
  • 如何找到免费的网站建设常州做金属网格公司
  • 网站建设与管理专业人才调研福州seo管理
  • 自己做网站可以用私有云吗wordpress国内视频教程
  • 上海做网站哪家公司短视频排名seo
  • 网站建设都有哪些网站开发前台软件用什么
  • 模板网站制作视频懂做游戏钓鱼网站的
  • 广州市专业网站设计做网站都要多少钱
  • 微网站开发平台centos7做网站