模塊化進化史教程

模塊化進化史教程

  1. 全局function模式
  • module1.js
    //數(shù)據(jù)
    let data = 'atguigu.com'
    
    //操作數(shù)據(jù)的函數(shù)
    function foo() {
      console.log(`foo() ${data}`)
    }
    function bar() {
      console.log(`bar() ${data}`)
    }
    
  • module2.js
    let data2 = 'other data'
    
    function foo() {  //與另一個模塊中的函數(shù)沖突了
      console.log(`foo() ${data2}`)
    }
    
  • test1.html
    <script type="text/javascript" src="module1.js"></script>
    <script type="text/javascript" src="module2.js"></script>
    <script type="text/javascript">
    
      let data = "修改后的數(shù)據(jù)"
      foo()
      bar()
    </script>
    
  • 說明:
    • 全局函數(shù)模式: 將不同的功能封裝成不同的全局函數(shù)
    • 問題: Global被污染了, 很容易引起命名沖突
  1. namespace模式
  • module1.js
    let myModule = {
      data: 'atguigu.com',
      foo() {
        console.log(`foo() ${this.data}`)
      },
      bar() {
        console.log(`bar() ${this.data}`)
      }
    }
    
  • module2.js
    let myModule2 = {
      data: 'atguigu.com2222',
      foo() {
        console.log(`foo() ${this.data}`)
      },
      bar() {
        console.log(`bar() ${this.data}`)
      }
    }
    
  • test2.html
    <script type="text/javascript" src="module2.js"></script>
    <script type="text/javascript" src="module22.js"></script>
    <script type="text/javascript">
      myModule.foo()
      myModule.bar()
    
      myModule2.foo()
      myModule2.bar()
    
      myModule.data = 'other data' //能直接修改模塊內(nèi)部的數(shù)據(jù)
      myModule.foo()
    
    </script>
    
  • 說明
    • namespace模式: 簡單對象封裝
    • 作用: 減少了全局變量
    • 問題: 不安全
  1. IIFE模式
  • module3.js
    (function (window) {
      //數(shù)據(jù)
      let data = 'atguigu.com'
    
      //操作數(shù)據(jù)的函數(shù)
      function foo() { //用于暴露有函數(shù)
        console.log(`foo() ${data}`)
      }
    
      function bar() {//用于暴露有函數(shù)
        console.log(`bar() ${data}`)
        otherFun() //內(nèi)部調(diào)用
      }
    
      function otherFun() { //內(nèi)部私有的函數(shù)
        console.log('otherFun()')
      }
    
      //暴露行為
      window.myModule = {foo, bar}
    })(window)
    
  • test3.html
    <script type="text/javascript" src="module3.js"></script>
    <script type="text/javascript">
      myModule.foo()
      myModule.bar()
      //myModule.otherFun()  //myModule.otherFun is not a function
      console.log(myModule.data) //undefined 不能訪問模塊內(nèi)部數(shù)據(jù)
      myModule.data = 'xxxx' //不是修改的模塊內(nèi)部的data
      myModule.foo() //沒有改變
    
    </script>
    
  • 說明:
    • IIFE模式: 匿名函數(shù)自調(diào)用(閉包)
    • IIFE : immediately-invoked function expression(立即調(diào)用函數(shù)表達式)
    • 作用: 數(shù)據(jù)是私有的, 外部只能通過暴露的方法操作
    • 問題: 如果當前這個模塊依賴另一個模塊怎么辦?
  1. IIFE模式增強
  • 引入jquery到項目中
  • module4.js
    (function (window, $) {
      //數(shù)據(jù)
      let data = 'atguigu.com'
    
      //操作數(shù)據(jù)的函數(shù)
      function foo() { //用于暴露有函數(shù)
        console.log(`foo() ${data}`)
        $('body').css('background', 'red')
      }
    
      function bar() {//用于暴露有函數(shù)
        console.log(`bar() ${data}`)
        otherFun() //內(nèi)部調(diào)用
      }
    
      function otherFun() { //內(nèi)部私有的函數(shù)
        console.log('otherFun()')
      }
    
      //暴露行為
      window.myModule = {foo, bar}
    })(window, jQuery)
    
  • test4.html
    <script type="text/javascript" src="jquery-1.10.1.js"></script>
    <script type="text/javascript" src="module4.js"></script>
    <script type="text/javascript">
      myModule.foo()
    </script>
    
  • 說明
    • IIFE模式增強 : 引入依賴
    • 這就是現(xiàn)代模塊實現(xiàn)的基石
  1. 頁面加載多個js的問題
  • 頁面:
    <script type="text/javascript" src="module1.js"></script>
    <script type="text/javascript" src="module2.js"></script>
    <script type="text/javascript" src="module3.js"></script>
    <script type="text/javascript" src="module4.js"></script>
    
  • 說明
    • 一個頁面需要引入多個js文件
    • 問題:
      • 請求過多
      • 依賴模糊
      • 難以維護
    • 這些問題可以通過現(xiàn)代模塊化編碼和項目構建來解決
?著作權歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時請結合常識與多方信息審慎甄別。
平臺聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點,簡書系信息發(fā)布平臺,僅提供信息存儲服務。

相關閱讀更多精彩內(nèi)容

  • 全局function模式 module1.js//數(shù)據(jù)let data = 'atguigu.com'//操作數(shù)據(jù)...
    cxq要努力閱讀 197評論 0 0
  • 模塊化進化史教程 全局function模式 module1.js//數(shù)據(jù)let data = 'atguigu.c...
    不會飛的fish閱讀 188評論 0 0
  • 為什么迫切需要js模塊化 隨著2006年ajax概念被提出,前端的業(yè)務的業(yè)務越來越多,代碼也越來越多,并逐漸發(fā)展成...
    神奇丶右手閱讀 742評論 0 50
  • 第一章:模塊化發(fā)展史 什么是模塊? 一個模塊的組成 模塊化 模塊化進化史 1. 全局function模式 modu...
    楚金_86e4閱讀 352評論 0 0
  • js模塊化 什么是模塊化將復雜的程序,按照一定的規(guī)范封裝成幾個文件,并組合在一起。實現(xiàn)數(shù)據(jù)內(nèi)部的數(shù)據(jù)、函數(shù)實現(xiàn)是私...
    labjss閱讀 190評論 0 0

友情鏈接更多精彩內(nèi)容