zoukankan      html  css  js  c++  java
  • js模块化方案【转】

    (function(){
        var CENTER = new EvtCenter();
        var Loaded={};
        var Modules={};
        function loadScript(name,url){
            if(!(name in Loaded)){
                Loaded[name]=!0;
                var js=document.createElement("script");
                js.src=url;
                document.head.appendChild(js);
            }
        }
    
        function require(name){return Modules[name]}
        function exports(obj){
            var name=this.name;
            this.exports=obj;
            if(name in Modules){
                for(var key in obj){
                    Modules[name]=obj[key];
                }
            }else{
                Modules[name]=obj;
            }
            this.trigger(CENTER,"load");
        }
    
        function Module(name,factory){
            var self = this;
            if(!(self instanceof Module)) return new Module(name,factory);
            if(factory===undefined){factory=name;name=factory.name}
            self.name = name||"anonymous";
            self.factory=factory;
            self.inited=false;
            Loaded[self.name]=!0;
            self.process();
            if(self.depends){
                self.on(CENTER,"load",function(e){
                    if((!self.inited)&&(self.depends[e.sender.name])){
                        self.check()&&self.init();
                    }
                });
            }else{
                setTimeout(function(){
                    self.init();
                },0)
            }
        }
        Eventable(Module);
        Module.prototype.init=function(){
            this.inited=true;
            this.factory(require.bind(this),exports.bind(this));
            if(!/exports(/.test(this.txt)){
                Modules[this.name]=null;
                this.trigger(CENTER,"load");
            }
            delete this.txt;
        }
        Module.prototype.check=function(){
            var k,flag=true;
            for(k in this.depends){
                if(!(k in Modules)){
                    flag=false;
                    break;
                }
            }
            return flag;
        }
        Module.prototype.process=function(){
            var txt,url,name,i,list,tmp,reg = /require(([^)]+))/;
            this.txt=this.factory.toString();
            list=this.txt.split(/
    ?
    /);
            for(i=0,l=list.length;i<l;i++){
                txt=list[i];
                if(reg.test(txt)){
                    tmp = reg.exec(txt)[1].replace(/^s*|s*$/g,'').split(/s*,s*/);
                    url = tmp[1]&&(tmp[1].replace(/'|"/g,''));
                    name= tmp[0].replace(/'|"/g,'');
                    if(url&&!/.js$/.test(url))url=url+'.js';
                    if(!(name in Loaded) && url) setTimeout(function(){loadScript(name,url)},0);
                    if(!this.depends) this.depends={};
                    this.depends[name]=!0;
                }
            }
        }
    
        window.Module=Module;
    })();//module
    复制代码

    使用简单的依赖预处理(无法辨别是否在注释或者字符串中)

    转载自:http://www.cnblogs.com/ss0102113/p/6114485.html

  • 相关阅读:
    JStack分析cpu消耗过高问题
    Machine Learning in Action – PCA和SVD
    Machine Learning in Action -- FP-growth
    Machine Learning in Action -- 树回归
    Machine Learning in Action -- 回归
    Kafka 0.8 配置参数解析
    统计学习方法笔记 -- 隐马尔可夫模型
    Machine Learning in Action -- AdaBoost
    统计学习方法笔记 -- Boosting方法
    Andrew Ng机器学习公开课笔记–Reinforcement Learning and Control
  • 原文地址:https://www.cnblogs.com/zhao-bo/p/6115325.html
Copyright © 2011-2022 走看看