模块加载原理

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());
});

 

posted @ 2019-11-18 10:22  littleboyck  阅读(...)  评论(... 编辑 收藏