<ruby id="bdb3f"></ruby>

    <p id="bdb3f"><cite id="bdb3f"></cite></p>

      <p id="bdb3f"><cite id="bdb3f"><th id="bdb3f"></th></cite></p><p id="bdb3f"></p>
        <p id="bdb3f"><cite id="bdb3f"></cite></p>

          <pre id="bdb3f"></pre>
          <pre id="bdb3f"><del id="bdb3f"><thead id="bdb3f"></thead></del></pre>

          <ruby id="bdb3f"><mark id="bdb3f"></mark></ruby><ruby id="bdb3f"></ruby>
          <pre id="bdb3f"><pre id="bdb3f"><mark id="bdb3f"></mark></pre></pre><output id="bdb3f"></output><p id="bdb3f"></p><p id="bdb3f"></p>

          <pre id="bdb3f"><del id="bdb3f"><progress id="bdb3f"></progress></del></pre>

                <ruby id="bdb3f"></ruby>

                ??一站式輕松地調用各大LLM模型接口,支持GPT4、智譜、豆包、星火、月之暗面及文生圖、文生視頻 廣告
                ## 介紹 從本章開始,我們會逐步介紹在JavaScript里使用的各種設計模式實現,在這里我不會過多地介紹模式本身的理論,而只會關注實現。OK,正式開始。 在傳統開發工程師眼里,單例就是保證一個類只有一個實例,實現的方法一般是先判斷實例存在與否,如果存在直接返回,如果不存在就創建了再返回,這就確保了一個類只有一個實例對象。在JavaScript里,單例作為一個命名空間提供者,從全局命名空間里提供一個唯一的訪問點來訪問該對象。 ## 正文 在JavaScript里,實現單例的方式有很多種,其中最簡單的一個方式是使用對象字面量的方法,其字面量里可以包含大量的屬性和方法: ~~~ var mySingleton = { property1: "something", property2: "something else", method1: function () { console.log('hello world'); } }; ~~~ 如果以后要擴展該對象,你可以添加自己的私有成員和方法,然后使用閉包在其內部封裝這些變量和函數聲明。只暴露你想暴露的public成員和方法,樣例代碼如下: ~~~ var mySingleton = function () { /* 這里聲明私有變量和方法 */ var privateVariable = 'something private'; function showPrivate() { console.log(privateVariable); } /* 公有變量和方法(可以訪問私有變量和方法) */ return { publicMethod: function () { showPrivate(); }, publicVar: 'the public can see this!' }; }; var single = mySingleton(); single.publicMethod(); // 輸出 'something private' console.log(single.publicVar); // 輸出 'the public can see this!' ~~~ 上面的代碼很不錯了,但如果我們想做到只有在使用的時候才初始化,那該如何做呢?為了節約資源的目的,我們可以另外一個構造函數里來初始化這些代碼,如下: ~~~ var Singleton = (function () { var instantiated; function init() { /*這里定義單例代碼*/ return { publicMethod: function () { console.log('hello world'); }, publicProperty: 'test' }; } return { getInstance: function () { if (!instantiated) { instantiated = init(); } return instantiated; } }; })(); /*調用公有的方法來獲取實例:*/ Singleton.getInstance().publicMethod(); ~~~ 知道了單例如何實現了,但單例用在什么樣的場景比較好呢?其實單例一般是用在系統間各種模式的通信協調上,下面的代碼是一個單例的最佳實踐: ~~~ var SingletonTester = (function () { //參數:傳遞給單例的一個參數集合 function Singleton(args) { //設置args變量為接收的參數或者為空(如果沒有提供的話) var args = args || {}; //設置name參數 this.name = 'SingletonTester'; //設置pointX的值 this.pointX = args.pointX || 6; //從接收的參數里獲取,或者設置為默認值 //設置pointY的值 this.pointY = args.pointY || 10; } //實例容器 var instance; var _static = { name: 'SingletonTester', //獲取實例的方法 //返回Singleton的實例 getInstance: function (args) { if (instance === undefined) { instance = new Singleton(args); } return instance; } }; return _static; })(); var singletonTest = SingletonTester.getInstance({ pointX: 5 }); console.log(singletonTest.pointX); // 輸出 5 ~~~ ## 其它實現方式 ### 方法1: ~~~ function Universe() { // 判斷是否存在實例 if (typeof Universe.instance === 'object') { return Universe.instance; } // 其它內容 this.start_time = 0; this.bang = "Big"; // 緩存 Universe.instance = this; // 隱式返回this } // 測試 var uni = new Universe(); var uni2 = new Universe(); console.log(uni === uni2); // true ~~~ ### 方法2: ~~~ function Universe() { // 緩存的實例 var instance = this; // 其它內容 this.start_time = 0; this.bang = "Big"; // 重寫構造函數 Universe = function () { return instance; }; } // 測試 var uni = new Universe(); var uni2 = new Universe(); uni.bang = "123"; console.log(uni === uni2); // true console.log(uni2.bang); // 123 ~~~ ### 方法3: ~~~ function Universe() { // 緩存實例 var instance; // 重新構造函數 Universe = function Universe() { return instance; }; // 后期處理原型屬性 Universe.prototype = this; // 實例 instance = new Universe(); // 重設構造函數指針 instance.constructor = Universe; // 其它功能 instance.start_time = 0; instance.bang = "Big"; return instance; } // 測試 var uni = new Universe(); var uni2 = new Universe(); console.log(uni === uni2); // true // 添加原型屬性 Universe.prototype.nothing = true; var uni = new Universe(); Universe.prototype.everything = true; var uni2 = new Universe(); console.log(uni.nothing); // true console.log(uni2.nothing); // true console.log(uni.everything); // true console.log(uni2.everything); // true console.log(uni.constructor === Universe); // true ~~~ ### 方式4: ~~~ var Universe; (function () { var instance; Universe = function Universe() { if (instance) { return instance; } instance = this; // 其它內容 this.start_time = 0; this.bang = "Big"; }; } ()); //測試代碼 var a = new Universe(); var b = new Universe(); alert(a === b); // true a.bang = "123"; alert(b.bang); // 123 ~~~ ## 參考資料 https://github.com/shichuan/javascript-patterns/blob/master/design-patterns/singleton.html http://www.addyosmani.com/resources/essentialjsdesignpatterns/book/#singletonpatternjavascript
                  <ruby id="bdb3f"></ruby>

                  <p id="bdb3f"><cite id="bdb3f"></cite></p>

                    <p id="bdb3f"><cite id="bdb3f"><th id="bdb3f"></th></cite></p><p id="bdb3f"></p>
                      <p id="bdb3f"><cite id="bdb3f"></cite></p>

                        <pre id="bdb3f"></pre>
                        <pre id="bdb3f"><del id="bdb3f"><thead id="bdb3f"></thead></del></pre>

                        <ruby id="bdb3f"><mark id="bdb3f"></mark></ruby><ruby id="bdb3f"></ruby>
                        <pre id="bdb3f"><pre id="bdb3f"><mark id="bdb3f"></mark></pre></pre><output id="bdb3f"></output><p id="bdb3f"></p><p id="bdb3f"></p>

                        <pre id="bdb3f"><del id="bdb3f"><progress id="bdb3f"></progress></del></pre>

                              <ruby id="bdb3f"></ruby>

                              哎呀哎呀视频在线观看