0

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 ?

4
  • 1
    No, how would that increase readability? Commented Jan 21, 2014 at 19:59
  • Why have a special method foo for that, if you want to execute it right away? Just execute the code during the module instantiation. Commented Jan 21, 2014 at 20:01
  • foo is not meant to be called right away. I just want the module code to be at the bottom of my single page script. Commented Jan 21, 2014 at 20:56
  • 1
    Why would it then be above the declaration if it is not intended to be executed before it? Maybe you could show your "whole" single-page script (all relevants part of it in expected order). Commented Jan 21, 2014 at 21:04

1 Answer 1

1

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.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.