-4

I have a javascript code that is about 3000 lines long. All of the functions are in one file. I need to know if there is a way to include a function within the same file.

Example:

function renderGameLayout()

This would include all the layout designs.

Now later on in the code, I will be calling back to that function within another function? I tried:

function getGames()
{
    // other js code here
    $(document).ready(function() 
    {
        renderGameLayout();
    });
    // other js code here
}

within another function, but this didn't work. Any suggestions? NOTE: I do not want to include another .js file, please do not suggest that.

jbabey
  • 45,965
  • 12
  • 71
  • 94
user1522901
  • 109
  • 8

2 Answers2

2

For large JS projects, especially if you are working on a JS game engine, you should review using something like RequireJS to manage your files and dependencies.

http://requirejs.org/

I think you'll find your answer and some potentially better ways to organize your project structure.

kevinnuut
  • 116
  • 4
1

It's hard to determine what your issue is, but I think you're expecting $(document).ready() to fire your renderGameLayout function for you. You've wrapped $(document).ready() inside a function, so that's not going to work as you expect. Move that into your file body:

$(document).ready(renderGameLayout());

or use an enclosure to fire renderGameLayout() instead like this:

(function() {
    renderGameLayout();
})();

Your code, as it stands, doesn't really make much sense.

Edit:

After re-reading your question, it sounds like you want to use a function in a separate script file WITHOUT adding the script to your page. In that case, no, you can't. Your JavaScript has to be aware of the existence of the other function. If you don't include the script, how's it going to know where or what the function is doing?

You can either move that function to your entirely-too-large file full of functions or you can add the script to your page. Those are your options.

Edit #2:

Ideally, you'd want to break up your JavaScript into files that make sense and combine and minify them when you go to production. A giant file full of 3000 lines seems like a bad idea.

Yatrix
  • 13,361
  • 16
  • 48
  • 78