JS-模块化

参数形式的模块化

        function BaseComponent(){
        }
        function OtherComponent(){
        }
        var application = function(){
            //private variables and functions
            var components = new Array();
            //initialization
            components.push(new BaseComponent());
            //create a local copy of application
            var app = new BaseComponent();
            //public interface
            app.getComponentCount = function(){
                return components.length;
            };
            app.registerComponent = function(component){
                if (typeof component == "object"){
                    components.push(component);
                }
            };
            //return it
            return app;
        }();
        console.log(application instanceof BaseComponent);
        application.registerComponent(new OtherComponent());
        console.log(application.getComponentCount());  //2

接口形式的模块化

        function BaseComponent(){
        }
        function OtherComponent(){
        }
        var application = function(){
            //private variables and functions
            var components = new Array();
            //initialization
            components.push(new BaseComponent());
            //public interface
            return {
                getComponentCount : function(){
                    return components.length;
                },
                registerComponent : function(component){
                    if (typeof component == "object"){
                        components.push(component);
                    }
                }
            };
        }();
        application.registerComponent(new OtherComponent());
        alert(application.getComponentCount());  //2