1

I have a class in Javascript based on this Base Class by Josh Gertzen. I'd like the class to self-execute an 'initialize' function for adding event listeners when it's instantiated. Is this possible?

This is in my main application script:

var mainView = new MainView(model)

/*the line I want to remove but don't know how*/
mainView.initialize();

And this is the method setting up the listener inside of the class extension.

initialize : function ()
{
    $(document).on("Loading", this.addPreloader);
}

Any help would be really appreciated. Thank you!

1 Answers1

0
function MainView(someParameter){
        this.property = null;

        this.initialize = function(someParameter){
             /* construction code */
            this.property = 'test';
        };

        this.initialize();
    };


var main = new MainView();
console.log(main.property);

This will help i think. You can also look at this SO Question for more information about javascript and constructors.

jsFiddle DEMO

Community
  • 1
  • 1
Martin
  • 3,096
  • 1
  • 26
  • 46