闭包实现一个AMD模块化 Posted on 2019-03-11 12345678910111213141516171819202122232425262728293031323334353637383940414243444546var Module = (function () { let modules = [] function define (mod, dependencies, func) { let depens = {} dependencies.forEach((depend, key) => { depens[depend] = modules[depend] }) modules[mod] = func.call(func, depens) } function require (mod) { return modules[mod] } return { define, require }})()// testModule.define('foo', [], function () { function sayHi () { console.log('say hi') } return { sayHi }})Module.define('bar', ['foo'], function (depends) { function sayHello () { depends.foo.sayHi() console.log('say hello') } return { sayHello }})const bar = Module.require('bar')bar.sayHello() 有点事做,晚上再补充。