Question about OOP 'flow'

Hello,

I am currently ‘translating’ a web app from ‘normal’ javascript to OOP javascript.

I have read a lot of theory about functions, objects, methods, inheritance and so on.

But now I am thinking more about what routine to use.

Basicly, I used to have a lot of functions to setup my UI:

checkForFirstLaunch();
loadLanguage();
loadNavigation();
loadImages();

I would just call checkForFirstLaunch(), and if it returned false, I would go to the next step, loadLanguage(), and at the end of that function I would call loadNavigation().

But now, I am using just one object named setup, which contains methods like checkForFirstlaunch, loadLanguage etc.

Here’s an example:


var setup = {

	run: function() {
	
		// We check if the app has just been launched for the first time. If true, we have to start downloading some images.
	
		( ls.get( "start" ) == null ) ? this.downloadImages() : this.loadLanguage();
	
	},

	loadLanguage: function () {
	
		...
		
		loadNavigation();
	
	},
	
	...

}

But this doesn’t look very sexy, and I feel I am not really using much of the OOP potential here, just translating functions into methods. I thought about perhaps having the methods return true if they where successful, but then my code would just be clustered with if/else code. So what’s a good way to control the ‘flow’ of an application?

Thanks! :slight_smile:

JavaScript is OOP. Practically everything is an object.

I think what you are looking for is more of an automated way of doing this. GOOD FOR YOU! You can do this:


var myScript  = {
  init: function() {
    //this would need to change but I'll leave it as so you can at least get the concept
    var isReady = true;

    if (isReady) {
      for (var x in this) {
        //dont run init more than once and only functions!
        if (x != "init" && typeof x == "function") {
          x(); //lets run all the functions
        }
      }
    }
  },

  loadLanguage: function() {
    //etc...
  },

  feelSexy: function() {
    //make the function feel sexy
  }
};