문제

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