0

I want to get rid of local variables in a certain namespace.

Here is the very top of my library where I set up some local objects to hold other objects - Model, View, Controller.

However I'm told there is a way to organize a library without locals to a namespace.

In this post here, the user Raynos says this might be the best way to organize a library. With zero locals to a namespace.

How can I eliminate locals to my library all together?

(function (window, document) {
    "use strict";
    var Mo = {},
        Vi = {},
        Co = {},
        Su = {};
Community
  • 1
  • 1
  • Technically, these variables are not global, they're local to your self-executing anonymous function. Do you mean that's still "too global" for your tastes? – Frédéric Hamidi Aug 17 '12 at 18:11
  • 1
    Raynos's answer is about modular organization, and he does say you should have "zero globals". Note, however, that both code samples in his [ncore GitHub README](https://github.com/Raynos/ncore/blob/master/README.md) create local variables, just as you do. You may be taking the "global" concept a little too far :) – Frédéric Hamidi Aug 17 '12 at 18:18
  • It is possible because, as I said before, *these variables are not global, they're local*. If you absolutely want to consider them as global, then, you still have to store state somewhere. You can group all these variables into a single object, but that object would still be "global" by your definition. – Frédéric Hamidi Aug 17 '12 at 18:26
  • @HiroProtagonist You are introducing a total of zero global variables when using the self-invoking pattern. I have a feeling that you are asking how to handle public or private properties and methods... – Matthew Blancarte Aug 17 '12 at 18:28
  • @Mathew, Frederic - Sorry for the confusion. I have updated the question, please read (if you have time) the link I posted in the question for a background. –  Aug 17 '12 at 18:32

1 Answers1

1

The closest I can get to your requirements is to "hide" the variables in arguments:

(function(window, document, Mo, Vi, Co, Su) {
    "use strict";
    // The rest of your code here, using the arguments instead of variables...
})(window, document, {}, {}, {}, {});

Two things, however:

  • These arguments have the exact same scope as the variables you're trying to get rid of (i.e. they're local to the anonymous function).

  • This separates the declaration of Mo, Vi, Co and Su (in the function's parameter list, at the top of the file) and their initialization (in the arguments passed to the function, at the bottom of the file). This makes the code less readable and maintainable, which is probably not your goal.

Frédéric Hamidi
  • 258,201
  • 41
  • 486
  • 479
  • Actually....you just reduced the scope chain...I think that {},{},{}, and {} would actually reside in the scope of 'window'. This might be the answer...Mo, Vi, Co, and Su just reference these un-named objects to reduce the scope chain...but not 100% –  Aug 17 '12 at 18:45
  • They do reside in the caller's scope (which may not be the global scope), but that doesn't really matter because there are no reference to them apart from the function's arguments. No other code in the caller can access them. – Frédéric Hamidi Aug 17 '12 at 18:47
  • The caller's scope in this case is window or the global scope....the function expression you wrote is in the global scope for this example. But - can you elaborate on "no other code in the caller can use them" ? –  Aug 17 '12 at 18:49
  • To use them, the caller would have to refer to them by name. But from the caller's point of view, they're just object literals -- they don't have names. They only get names when the anonymous function receives them, and these names are only valid in the function's scope (modulo closures). – Frédéric Hamidi Aug 17 '12 at 18:53
  • So you do agree that in this example....{},{},{},{} "live" in the window scope with out names... –  Aug 17 '12 at 18:56
  • Yes, we agree on that. But since they don't have names, nothing in that scope can see them or act upon them. They live there, but as invisible, intangible ghosts :) – Frédéric Hamidi Aug 17 '12 at 18:57