4

I have a rails app with the following JavaScript file structure:

- assests/javascripts
  - /application
    - module1.js
        .
        .
        .
    - moduleN.js
  - application.js

Each file looks like this:

var module1 = (function(){

    //Lots of code

    return {
        //Public stuff
    }

})();

Modules need access to each other and therefore expose some stuff.

application.js

//= require_tree ./application

I am wondering if there is a way to compile all of the code into an anonymous function; something like:

(function(){
//= require_tree ./application
})();

Which obviously doesn't work. What I'm currently doing is:

application.js

//= require ./begin.js
//= require_tree ./application
//= require ./end.js

begin.js is just (function(){ and end.js is })();.

Not the neatest solution.

This not only eliminates the global variables, but lets the compiler change their name to shorter ones for a lighter code and more unreadable. I am concerned with code confidentiality and don't want to expose module names, to make reverse engineering more difficult.

3
  • 1
    It's not a closure, it's an anonymous function Commented Apr 9, 2013 at 1:21
  • Right, thanks. I changed the question. Commented Apr 9, 2013 at 1:27
  • An unspoken requirement here is why it's so important that your code in inaccessible from window. I think you need to expand on that, because I've never seen a case where a handful of global namespaces would be a problem. Commented Apr 9, 2013 at 1:54

1 Answer 1

1

Use a global namespace.

application.js

//= require_self
//= require_tree ./application

App = {};

require_self is important here, because by default Sprockets puts the contents of this file at the bottom.

application/first_module.js

App.FirstModule = (function () {
  //blah blah

  return {
    //public methods
  }
})();

What you're trying to do is an abuse of an anonymous function, and kind of defeats the purpose. If you need to share private variables across two modules, they should be one module. If a module is too large to maintain, you need to break it up into separate modules with public interfaces.

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

7 Comments

But then App would belong to window and not be self contained. i.e. App (and all its modules) would be accessible from the console, right?
"you need to break it up into separate modules with public interfaces" That is exactly what I did, the thing is that I a) want to have them in separate files and b) don't want them to be global.
Yes, it would. And in most cases that's just fine.
My main concern with that is is code confidentiality and avoiding reverse engineering. Exposing module names is a problem for me. I think I would still go with the original way instead.
Sounds like you forgot require_self
|

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.