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 ?

有帮助吗?

解决方案

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.

许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top