脚踏实地

心 勿噪

js模块编程

<script type="text/javascript">
 
        void function(global)
        {
            var mapping = {}, cache = {};
            global.define = function(id, func){
                mapping[id] = func;
            };
            
            global.require = function(id){
                if(cache[id])
                    return cache[id];
                else
                    return cache[id] = mapping[id]({});
            };
        }(this);
        
        define("moduleA", function(exports)
        {
            function ClassA(){
            }
            
            ClassA.prototype.print = function(){
                alert("moduleA.ClassA")
            }
            
            exports.New = function(){
                return new ClassA();
            }
        
            return exports;
            
        });
        
        define("moduleB", function(exports)
        {
            function ClassB(){
            }
        
            ClassB.prototype.print = function(){
                alert("moduleB.ClassB")
            }
            
            exports.New = function(){
                return new ClassB();
            }
            
            return exports;
        });
        
        define("moduleC", function(exports)
        {
            function ClassC(){
            }
        
            ClassC.prototype.print = function(){
                var classA = require("moduleA").New();
                classA.print();
                    
                var classB = require("moduleB").New();
                classB.print();
                    
                alert("moduleC.ClassC")
            }
            
            exports.New = function(){
                return new ClassC();
            }
            
            return exports;
        });
        
        var classC = require("moduleC").New();
        classC.print();
        
      </script>

posted on 2016-10-02 20:33 LSH 阅读(212) 评论(0)  编辑 收藏 引用


只有注册用户登录后才能发表评论。
网站导航: 博客园   IT新闻   BlogJava   知识库   博问   管理