Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 4006398
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 20, 20262026-05-20T08:30:36+00:00 2026-05-20T08:30:36+00:00

My javascript file is getting pretty big (3000+ lines) and I’m getting confused as

  • 0

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 file.

To summarise my JS file looks a little like this at the moment:

//ALL GLOBAL VARIABLES FIRST DECLARED HERE
var var1 , var2 ,var3

$(document).ready(function(){

//JQUERY STUFF

});

//ALL FUNCTIONS THAT NEED TO BE GLOBAL DECLARED HERE
function myFunction(){
//do some stuff here
}

I am running into problems with this as some functions I call in places don’t seem to be declared at the time of calling or aren’t available globaly. It’s all very confusing now!

Could someone suggest the best way to layout a big js/jquery file with certain JS Functions, Objects and Variables available to be referenced anywhere in the file.

UPDATE:

So to simplify it this correct (see my comments)?

window.MainModule = (function($, win, doc, undefined) {//WHAT IS BEING PASSED IN HERE?
    var foo, bar, modules; //VARIABLES ACCESSIBLE ANYWHERE

    var modules["foobar"] = (function() {//WHAT IS A MODULE? WHEN WOULD I USE A SEPERATE MODULE?
        var someFunction = function() { ... };//DECLARING MY FUNCTIONS?

        ...

        return { 
            init: someFunction,//IS THIS WHERE I USE/BIND MY FUNCTIONS TO EVENTS AND ELEMENTS?
            ... 
        };
    }());

    // hoist a variable into global scope
    window.Global = someLocal;

    return { 
        init: function() {//FUNCTION TO INIT ALL MODULES?
            for (var key in modules) {
                modules[key].init();
            }
        }
    };
}(jQuery, this, document));
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-20T08:30:37+00:00Added an answer on May 20, 2026 at 8:30 am

    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);
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a javascript file that reads another file which may contain javascript fragments
How can I load an external JavaScript file using a bookmarklet? This would overcome
I have some code in a javascript file that needs to send queries back
How can you reliably and dynamically load a JavaScript file? This will can be
I have added a simple .js file to my page that has some pretty
Say I have a fairly hefty JavaScript file, packed down to roughly 100kb or
I want to create a compiled JavaScript file for my website. For development I
Say I need to call a javascript file in the <head> of an ERb
I am include a huge javascript file(500K) in my HTML. Is there a smart
I have several update methods in a javascript file, used for updating my ajax

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.