Question

I am trying to break my code into modules, and I have problems referencing the other modules of my app.

MYAPP = (function(my_app)
{

    var funcs = my_app.funcs;

    var module2 =
    {
         stupid_function = function ()
         {
            var some_var = "auwesome!";

            //let's call something from the other module...
            funcs.do_whatever();
         };

    };

    my_app.module2 = module2;

    return my_app;

})(MYAPP);

The problem comes when MYAPP.funcs changes. For example, when I initialize it and add new methods... since "funcs" was created inside of the closure, it has a copy of MYAPP.funcs and does not have the new stuff I need.

This is the way "funcs" gets more methods... when I execute the method MYAPP.funcs.init() the MYAPP.funcs is re-written by itself.

var MYAPP = (function (my_app, $)
{

    'use_strict';

     my_app.funcs = {};

         my_app.funcs.init = function ()
         {
            panel = my_app.dom.panel;
            funcs = my_app.funcs;
                query_fns = funcs.query;

                 my_app.funcs =
                 {
                       some_method: function () { ... },
                       do_whatever: function () { ... }
                 };

          };


    return my_app;

}(APP, jQuery));

Thanks in advance!!


In case it is interesting for anybody...

The method I am using for moduling is the "tight augmentation" http://webcache.googleusercontent.com/search?hl=es-419&q=cache%3Aadequatelygood.com%2F2010%2F3%2FJavaScript-Module-Pattern-In-Depth&btnG=

Was it helpful?

Solution

Nothing to do with modules or global... the difference is if you are changing object variable points to or what variable is pointing to:

var first = {my:"test"};
var second = first; // second points to object {my:"test"}
first.foo = 42; // updating the object, second points to it and can see the change
first = {my:"other"}; // new object, second still points to old  {my:"test", foo:42}
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top