欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  IT编程

模块加载原理

程序员文章站 2022-05-18 20:16:16
var depMods = (function(){ var modules = {}; var defined = function(name,deps,impl){ typeof deps == 'function' && ( impl = deps, deps = [] ) if(deps.l ......
var depmods = (function(){
    var modules = {};
    var defined = function(name,deps,impl){
        typeof deps == 'function' && (
            impl = deps,
            deps = []
        )
        if(deps.length > 0){
            for(let i = 0; i < deps.length; i++){
                deps[i] = modules[deps[i]]
            }
        }
        !modules[name] && (modules[name] = impl.apply(impl,deps))
    }
    var require = function(deps,handle){
        if(!array.isarray(deps)){
            deps = [deps];
        }
        for(let i = 0; i < deps.length; i++){
            deps[i] = modules[deps[i]]
        }
        handle.apply(handle,deps);
    }
    return {
        defined:defined,
        require:require
    }
})();


depmods.defined('table',function(){
    var inittable = function(){
        return "初始化表格模块";
    }
    return {
        inittable:inittable
    }
});

depmods.require('table',function(table){
    alert(table.inittable());
});