When I load a page I am calling addEventListener to initialize deviceReady, inside that addEventListener I want to call a function which is inside a class. See the following example:
Example class
var HomePageModel = function(){
this.initModule = function(){
//doing some process;
};
};
I want to call the above initModule function in addEventListener.
Like
document.addEventListener("deviceready", HomePageModel.initModule, false);
Is it possible to call class function inside a eventlistener which is outside of a class?
You need an instance first:
Note that if your
initModulemethod usesthis, it won’t work, becausethiswill be set to the element that triggered the event. To avoid that, you can create a new function bound to a fixed value ofthis:Finally, you can always use a wrapper function as the event handler, and call your module initializer from there (not sure why you haven’t considered that):