astralaaron 17 Report post Posted December 16, 2018 I have a work project that has become quite large, and I am now required to separate each object of the code into it's own file. The following code example is how it is structured currently, all in one giant file. function Main() { var main = this; this.init = function() { // code main.keyboard.init(); main.game_loop.init(); main.physics.init(); } this.keyboard = { init: function() { }, keys: { UP:38, DOWN:40, LEFT:37, RIGHT:39 } input: function(e) { // functions reference 'main' variable } } this.game_loop = { init: function() { }, on_frame: function() { // functions reference 'main' variable } } this.physics = { init: function() { // functions reference 'main' variable } } } $(document).ready(function(){ var m = new Main(); m.init(); }); I have tried separating the objects into their own file's and changing them, for example from: this.keyboard = { } to Main.prototype.keyboard = { } However I then no longer have access to the 'main' variable. Someone has suggested "arrow functions" to me, but I was just confused on how to apply that to my code. I really appreciate anyone who looks at this. Quote Share this post Link to post Share on other sites
justsomeguy 1,134 Report post Posted December 17, 2018 You need to specify that the other objects execute in the same scope as Main. Arrow functions can do that, when you define the function you can also tell it what scope to run in. Quote Share this post Link to post Share on other sites