最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

JavaScript實(shí)現(xiàn)單例模式的六種方式

 更新時(shí)間:2025年12月10日 08:41:28   作者:willxiao  
單例模式確保一個(gè)類只有一個(gè)實(shí)例,并提供全局訪問(wèn)點(diǎn),文章介紹了JavaScript中實(shí)現(xiàn)單例模式的幾種常見方式,包括對(duì)象字面量、閉包實(shí)現(xiàn)、ES6類實(shí)現(xiàn)、改進(jìn)的class實(shí)現(xiàn)、ES6模塊模式的單例以及ES6模塊本身就是單例,每種方式都有其特點(diǎn)和適用場(chǎng)景,需要的朋友可以參考下

JavaScript 中的單例模式確保一個(gè)類只有一個(gè)實(shí)例,并提供全局訪問(wèn)點(diǎn)。以下是幾種常見的實(shí)現(xiàn)方式:

1.對(duì)象字面量(最簡(jiǎn)單的方式)

const Singleton = {
  property: 'value',
  method() {
    // 使用 this(在方法被解構(gòu)調(diào)用時(shí)會(huì)丟失上下文)
    // console.log(this.property);
    
    // 使用 Singleton(更安全)
    console.log(Singleton.property);
  }
};

// 使用
Singleton.method();

JavaScript 可以使用對(duì)象字面量快速創(chuàng)建一個(gè)對(duì)象,創(chuàng)建的對(duì)象本身就是單例。這種方式最為簡(jiǎn)單,但是需要注意 this 的引用可能出問(wèn)題。

為了解決 this 的問(wèn)題,可以直接引用單例對(duì)象本身。

2.閉包實(shí)現(xiàn)

const Singleton = (function() {
  let instance;
  
  function createInstance() {
    const object = new Object('I am the instance');
    return object;
  }
  
  return {
    getInstance: function() {
      if (!instance) {
        instance = createInstance();
      }
      return instance;
    }
  };
})();

// 使用
const instance1 = Singleton.getInstance();

這里利用了閉包的特性實(shí)現(xiàn)了模塊的封裝和單例對(duì)象的引用,返回一個(gè) getInstance 方法用于獲取實(shí)例對(duì)象。

3.ES6 Class 實(shí)現(xiàn)

class Singleton {
  constructor() {
    if (Singleton.instance) {
      return Singleton.instance;
    }
    
    this.data = 'Singleton Data';
    Singleton.instance = this;
  }
  
  getData() {
    return this.data;
  }
  
  setData(data) {
    this.data = data;
  }
}

// 使用
const s1 = new Singleton();
const s2 = new Singleton();
console.log(s1 === s2); // true
s1.setData('New Data');
console.log(s2.getData()); // 'New Data'

這里的 instance 是一個(gè)靜態(tài)變量,這種方式在 constructor 的最后將 this 賦值給 instance。

4.改進(jìn)的 class 實(shí)現(xiàn)(typescript 版本)

class Singleton {
  private static instance?: Singleton;

  private constructor() {}

  static getInstance() {
    if (!Singleton.instance) {
      Singleton.instance = new Singleton();
    }
    return Singleton.instance;
  }
}

// 使用
const s1 = Singleton.getInstance();
const s2 = Singleton.getInstance();
console.log(s1 === s2); // true

typescript 版本抽取 getInstance 方法,讓代碼更可讀。使用 private 關(guān)鍵字實(shí)現(xiàn)私有屬性和方法,保證代碼不會(huì)被隨意篡改。

5.ES6 模塊模式的單例

// 在模塊文件中
let instance = null;

class Database {
  constructor(config) {
    if (instance) {
      return instance;
    }
    
    this.connection = this.connect(config);
    instance = this;
  }
  
  connect(config) {
    return { connected: true, config };
  }
}

// 導(dǎo)出一個(gè)獲取實(shí)例的函數(shù)
export const getInstance= (() => {
  let instance = null;
  return (config) => {
    if (!instance) {
      instance = new Database(config);
    }
    return instance;
  };
})();

這種方式利用 ES6 模塊變量的引用共享特性,保證 instance 唯一。導(dǎo)出一個(gè) getInstance 方法。

6.ES6 模塊本身就是單例

class Database {
  constructor(config) {
    this.connection = this.connect(config);
  }
  
  connect(config) {
    return { connected: true, config };
  }
}

// 直接導(dǎo)出一個(gè)實(shí)例
export default new Database({ host: 'localhost' });

實(shí)測(cè)證明,導(dǎo)出的實(shí)例在多個(gè)模塊間是共享的。

總結(jié)

JavaScript 實(shí)現(xiàn)單例模式的方式很多,這里介紹常用的6種,主要分4大類:對(duì)象字面量、閉包實(shí)現(xiàn)、 ES6 class 實(shí)現(xiàn)、ES6 模塊模式實(shí)現(xiàn)。

選擇哪種實(shí)現(xiàn)方式取決于具體需求,簡(jiǎn)單場(chǎng)景可以使用對(duì)象字面量,復(fù)雜場(chǎng)景建議使用 ES6 Class 或 ES6 模塊模式實(shí)現(xiàn)。

以上就是JavaScript實(shí)現(xiàn)單例模式的六種方式的詳細(xì)內(nèi)容,更多關(guān)于JavaScript實(shí)現(xiàn)單例模式的資料請(qǐng)關(guān)注腳本之家其它相關(guān)文章!

相關(guān)文章

最新評(píng)論

太白县| 福建省| 靖边县| 新建县| 贵港市| 偃师市| 吉林省| 申扎县| 桐梓县| 明水县| 山丹县| 项城市| 东至县| 巴南区| 阿坝| 宣化县| 乾安县| 那坡县| 耿马| 南丹县| 崇阳县| 星座| 五大连池市| 张掖市| 红桥区| 桦南县| 隆化县| 龙陵县| 仪征市| 麻阳| 霍邱县| 阜南县| 江源县| 麦盖提县| 额尔古纳市| 界首市| 阳山县| 江门市| 勐海县| 江孜县| 游戏|