zoukankan      html  css  js  c++  java
  • 后盾人js模块化编程实例-01

    <!DOCTYPE html>
    <html lang="en">
      <head>
        <meta charset="UTF-8" />
        <meta http-equiv="X-UA-Compatible" content="IE=edge" />
        <meta name="viewport" content="width=device-width, initial-scale=1.0" />
        <title>Document</title>
      </head>
      <body>
        <script>
          //闭包定义模块
          let module = (function () {
            // 定义模块容器
            const moduleList = [];
    
            // 参数1 模块名称 参数二 被依赖的模块名 参数三 方法
            function define(name, modules, action) {
    
              // 导出被依赖的模块
              modules.map((m, i) => {            
                modules[i] = moduleList[m];
              });
    
              // 往容器里压一个模块
              moduleList[name] = action.apply(null, modules);
            //   console.log(moduleList);
            }
            return { define };
          })(); 
    
          // 当前模块被其它模块依赖 需要return出去,称之为导出
          module.define("hd", [], function () {
            // 定义的模块中 对外暴露两个功能
    
            return {
              // 返回数组中的第一个元素
              first(arr) {
                return arrp[0];
              },
              // 返回数组中的最大值
              max(arr,key) {
                return arr.sort((a, b) => b[key] - a[key])[0];
              },
            };
          });
    
          // lesson 模块依赖于hd模块 这部分称之为导入
          module.define("lesson", ["hd"], function (hd) {
              let data=[
                  {
                      name:'js',
                      price:999
                  },
                  {
                      name:'mysql',
                      price:200
                  }
              ]
              console.log(hd);
              console.log(hd.max(data,"price"));
          });
        </script>
      </body>
    </html>
  • 相关阅读:
    Wooden Sticks(hdu1051)
    Leftmost Digit(hdu1060)(数学题)
    Sum of Remainders(数学题)
    Brain Network (medium)(DFS)
    Brain Network (easy)(并查集水题)
    Collective Mindsets (medium) (逻辑题)
    Collective Mindsets (easy)(逻辑题)
    RMQ with Shifts(线段树)
    Throwing Dice(概率dp)
    圆桌会议
  • 原文地址:https://www.cnblogs.com/ABC-wangyuhan/p/14855721.html
Copyright © 2011-2022 走看看