<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>

                ThinkChat2.0新版上線,更智能更精彩,支持會話、畫圖、視頻、閱讀、搜索等,送10W Token,即刻開啟你的AI之旅 廣告
                >[success] # ES6 -- 寫單例模式 [有參考價值的文章](https://juejin.im/post/5d6105e75188251031357b2b) >[danger] ##### 第一個案例寫法 ~~~ 1.這里有個尷尬的問題就是 在java的單例模式中構造函數是私有的這樣, 這樣該類就不會被實例化,但是現在js 沒有私有屬性這個說法,因此這個 單例你既可以new 出來也可以通過'getInstance' 使用 ~~~ ~~~ class Singleton{ static instance = null constructor(name ){ this.name = name } getName(){ console.log(this.name) } static getInstance(name){ if(!Singleton.instance){ Singleton.instance = new Singleton(name) } return Singleton.instance } } const a = Singleton.getInstance('wang') const b = Singleton.getInstance('yi') a.getName() b.getName() ~~~ >[danger] ##### 透明單例的實現(可以通過new創建就是透明單例) ~~~ 1.可以同new的形式創建了,也比es5的代碼好理解了,利用靜態屬性'instance' 用來存當前類有沒有被實例過 ~~~ ~~~ class Singleton{ static instance = null constructor (name ){ if(Singleton.instance){ return Singleton.instance } this.name = name return Singleton.instance = this } getName(){ console.log(this.name) } } const a = new Singleton('wang') const b = new Singleton('yi') a.getName() b.getName() console.log(a === b) ~~~ >[danger] ##### 代理模式實現單例 ~~~ class Person{ constructor (name ){ this.name = name } getName(){ console.log(this.name) } } // 我是代理 var CreateSinglePerson = (function () { var instance; return function (name) { if (!instance) { instance = new Person(name); } return instance; }; })() var a = new CreateSinglePerson('a'); var b = new CreateSinglePerson('b'); console.log(a === b); // true ~~~ >[danger] ##### 一段比較有想法的代碼 ~~~ class Image {} class Link {} class Text {} class ElementFactory { createElement(type, option){ const ELEMENT = { "image" : Image, "text" : Text, "link" : Link } let ElementConstructor = ELEMENT[type], element = null; if(ElementConstructor){ element = new ElementConstructor(option); } return element; } } ~~~
                  <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>

                              哎呀哎呀视频在线观看