1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45
| <script> ///模块管理工具的开发
//需要有一个容器来管理模块,定义模块,往容器中添加模块,模块之间存在依赖,可以相互使用
let module =(function (){ const modulelist = {} //定义一个容器来存储模块 function define(name,modules,action) { //依赖模块,m是依赖的具体模块,i是模块所在的索引号, modules.map((m,i)=>{ modules[i]=modulelist[m]; // console.log(modulelist[m]); // console.log(modules);
})
//执行并保存模块,保存的是它的结果 modulelist[name]=action.apply(null,modules)
} return {define} })()
//模块的名字,依赖其他模块,模块的功能 module.define('hd', [ ], function(){ return { first(arr){ return arr[0]; }, max(arr,key ){ return arr.sort((a,b)=>{return b[key]-a[key]})[0]; } }; })
module.define('lesson', ['hd'], function(hd){ console.log(hd); let data = [ {name:'js', price: 199}, {name: 'mysql' ,price: 78} ] console.log(hd.max(data,'price')); });
</script>
|