[removed] Best way to declare functions to be used globally?

前端 未结 3 942
长发绾君心
长发绾君心 2021-02-04 06:20

My javascript file is getting pretty big (3000+ lines) and I\'m getting confused as to how to layout my file and delare functions so that they can called anywhere in the

3条回答
  •  离开以前
    2021-02-04 07:20

    The modules section isn't properly defined ... here's a slightly tidied up example.

    window.MainModule = (function($, win, doc, undefined) {
        var modules = {};
    
        // -- Create as many modules as you need ...
        modules["alerter"] = (function(){
            var someFunction = function(){ alert('I alert first'); };
    
            return { 
                init: someFunction
            };
        }());
    
        modules["alerter2"] = (function(){
            var someFunction = function(){ alert('I alert second'); };
    
            return { 
                init: someFunction
            };
        }());
    
        return { 
            init: function(){
                for (var key in modules){
                    modules[key].init();
                }
            }
        };
    }(jQuery, this, document));
    
    $(window.MainModule.init);
    

提交回复
热议问题