<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中的沙箱并非傳統意義上的沙箱,只是一種語法上的hack寫法而已,javascript中處理模塊依賴關系的閉包被稱之為沙箱,和 ajax一樣,這種sandbox coding風格是一種現象,而不是本質,本身并無對錯之分,要看你怎么用,因此,理解并合理運用才是我們對“js沙箱”的一個正確的基本態度。 ——沙箱是一個工具。就和鍵盤和鼠標一樣,我們需要他,但更要看我們怎么用他。 沙箱模式(Sandbox Pattern),顧名思義沙箱模式是創建了一個"沙箱",可以理解為創建了一個黑盒,我們不管在里面做什么都不會影響到外面。而在JavaScript中就意味著,在沙箱中的操作被限死在當前作用域,不會對其他模塊和個人沙箱造成任何影響。 ## 二、固定格式的寫法 modules必須是數組,callback必須是函數,兩個參數都要傳不然會報錯。 ~~~ (function(w) { var Sandbox = function(modules, callback) { if(!(this instanceof Sandbox)) { return new Sandbox(modules, callback); } for(var i=0, len=modules.length; i<len; i++) { Sandbox.modules[modules[i]](this); } callback(this); } Sandbox.modules = {}; w.Sandbox = Sandbox; })(this); Sandbox.modules.get = function() { alert('123123'); } Sandbox(['get'], function() {}); ~~~ ## 三、兼容寫法 ~~~ function Sandbox() { var args = Array.prototype.slice.call(arguments), // 參數轉成數組形式 callback = args.pop(), // 取出數組最后一個元素(回調函數) modules = (args[0] && typeof args[0]==='string') ? args : args[0]; // 判斷數組剩下內容的第一個元素 if(!(this instanceof Sandbox)) { // 如果不是沙箱實例,則實例化 return new Sandbox(modules, callback); } if(!modules || modules[0]==='*') { // 判斷模塊參數是否存在或者第一個參數是否是* modules = []; for(var i in Sandbox.modules) { if(Sandbox.modules.hasOwnProperty(i)) { modules.push(i); } } } for(var i=0; i<modules.length; i++) { // 循環模塊調用 Sandbox.modules[modules[i]](this); } callback(this); // 回調 } Sandbox.prototype = { // 原型 name: 'sandbox', version: '1.0.0', getName: function() { return this.name; } } // 定義模塊及調用 Sandbox.modules = {}; Sandbox.modules.event = function(box) { box.addEvent = function(selector, evtype, callback) { selector.addEventListener(evtype, callback); } } Sandbox(['event'], function(box) { box.addEvent(document, 'click', function(e) { alert(e.clientX+'px, '+e.clientY+'px'); }); }); ~~~ 沙箱是一種設計模式,用來避免過多的全局變量,而且能起到模塊化開發,清晰思路的效果。。用不用不強求,好讀書不求甚解。。
                  <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>

                              哎呀哎呀视频在线观看