模块加载原理(第二种)
程序员文章站
2023-12-24 18:55:51
var kModule = (function(){ var modules = {} ,config = {} ,define = function(deps,factory){ typeof deps == 'function' && ( factory = deps, deps = [] ) ......
var kmodule = (function(){ var modules = {} ,config = {} ,define = function(deps,factory){ typeof deps == 'function' && ( factory = deps, deps = [] ) if(!array.isarray(deps)){ deps = [deps]; } var exports = function(name,impl){ if(!modules[name]){ kmodule[name] = modules[name] = impl; } } typeof factory == 'function' && factory.apply(factory,[exports]) } ,require = function(deps,handle){ typeof deps == 'function' && ( handle = deps, deps = [] ) if(!array.isarray(deps)){ deps = [deps]; } for(let i = 0; i < deps.length; i++){ deps[i] = modules[deps[i]] } handle.apply(handle,deps); } return { define:define, require:require, }; })(); kmodule.define(function(exports){ var inittable = function(){ return "初始化表格模块"; } var tableimpl = { inittable:inittable } exports('table',tableimpl) }); //第一种调用方式 kmodule.require('table',function(){ var table = kmodule.table alert(table.inittable()); }); //第二种调用方式 kmodule.require('table',function(table){ alert(table.inittable()); });