Question

I'm using some kind of module pattern as described here :

var Module = function(){
  function foo(){}
  return{
    foo:foo,
  }
}();

it can be called with

Module.foo();

However, it only works when the call is made after the declaration.

For readability purpose, is there any way to call it before the declaration ?

Was it helpful?

Solution

You could wait for the document ready event. If you're using jquery it could be something like

$(document).ready(function () {
    Module.foo();
});


var Module = (function () {
    //Definition for Module
})();

The document ready function will wait for the document to be loaded to call your function. So it will parse all the javascript before it executes.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top