Search code examples
javascriptdata-structuresmodularization

Javascript modularising large objects


I've got an object defined as a function, and it's got a lot of methods and properties in it, what are some ways to modularise this to make it more manageable? It is possible to put functions inside the object into external files?

Edit:

Someone mentioned in one answer to include other files, but this would have an unweildy HTML page, IE:

<script type="text/javascript" src="script1.js"></script>
<script type="text/javascript" src="script2.js"></script>
<script type="text/javascript" src="script3.js"></script>
....
<script type="text/javascript" src="script76.js"></script>

Is there also any way to structure the code so only one reference is required the the javascript file?


Solution

  • var myobj = {
      somefunc: function() { /* function code */ },
      prop: "some string",
      // etc.
    };
    

    and in another js file included after the above code

    myobj.anotherfunc = function () { /* function code */ };