Jump to content

Is there any way to split one main.js to few?


swat0284
 Share

Recommended Posts

You can use JavaScript objects as namespaces or use a module system.

 

If you go the namespace route, you'll have one global object containing the parts of your application. Your gui.js file could then look like this for example:

if (typeof GAME === "undefined") {    var GAME = {};}GAME.gui = (function () {        // define some GUI functions here    function doSomeGuiStuff () {        GAME.other.doSomeStuff();        // ...    }        return {        // Put the functions you want to be "public" into this object:        doSomeGuiStuff: doSomeGuiStuff    };}());

This approach uses only one global variable (GAME) which is much better than having everything globally, but you always need to include your script files in the right order if one file depends on another file. This can get frustrating if your code gets bigger and you use more and more files. Therefore there are so-called module systems that help you with this, like require.js or using.js (disclaimer: this is my own library). In these libraries you either have a specific folder structure or a file that tells the module system where to find your modules. In using.js, you would include the using.js file and a file containing the script paths in your HTML file. The paths file can look like this:

// File "paths.js"(function () {        // This assumes you have using.js in your main directory    // and your scripts in a subfolder of the main directory    // called "js/"    var base = using.path + "js/";        using.modules.gui = base + "gui.js";    using.modules.player = base + "player.js";    using.modules.other = base + "other.js";    }());
// File "gui.js"using("other").define("gui", function (other) {        // Define your GUI functions here, using some stuff from the "other" module    function doGuiStuff () {        other.doSomeStuff();        // ...    }    return {        // Put all the functions you need to use somewhere else into this object:        doGuiStuff: doGuiStuff    };    });

If you're only developing a simple game, the namespace objects will suffice. For anything else you should probably read up on module systems. The important thing here is that you don't use global variables as advised in another answer because it can lead to hard-to-find bugs, especially in JavaScript.

Link to comment
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.
Note: Your post will require moderator approval before it will be visible.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

Loading...
 Share

  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...