1

Using Node.

Trying to avoid global vars, but I need to share a variable(integer) between two functions that are not nested. Both functions are declared directly under the global scope.

Is something like this considered a good practice for what I'm trying to accomplish? If not, is there a better pattern to follow?

function doStuffWithDataFromEventListener(){           
    var a = inner2().num;
}();

var fakeListener = function(){ //make believe event listener that is only called once.
   var num = 7;
   return {num: num};
};
  • Why not `return 7` directly? – elclanrs Apr 03 '16 at 23:02
  • 2
    Calling a function to return a value doesn't solve your global issue because you still have `globalNum` which you would have if you just did `var globalNum = 7;`. You could wrap your code in an IIFE and then nothing would leak to global. – Andy Apr 03 '16 at 23:08
  • @elclanrs I was under the impression that objects are returned as references, and primitives are returned as values, – cheesenthusiast Apr 03 '16 at 23:34
  • @Andy I see what you mean, my example solves nothing. I'm using Node. Will update example later because I realize in hindsight it's too vague – cheesenthusiast Apr 03 '16 at 23:41
  • @cheesenthusiast: Actually in node you hardly ever have global variables; all variables declared top-level in a module are in that module's own scope - and perfectly fine to be used. – Bergi Apr 04 '16 at 00:20
  • @Bergi so the measures I am going through in the above [updated] example are overkil/unnecessary? I should just write it at the top? Thank you – cheesenthusiast Apr 04 '16 at 00:24
  • @cheesenthusiast: Yes, exactly. See also [In what scope are module variables stored in node.js?](http://stackoverflow.com/q/15406062/1048572) – Bergi Apr 04 '16 at 00:30
  • @Bergi Thanks, I see where I went wrong now. I was reading about JS design patterns and trying to apply what I learned to refactor a small Node app. I think the articles I read were more FE-centric, which led to some confusion. The other answers would definitely be useful in front-end dev. If you made your comment into an answer I could accept it. – cheesenthusiast Apr 04 '16 at 03:45

2 Answers2

0

Use the module pattern:

 // Global scope

 (function(){

    // Function (module) scope

    var x = 5;  // Module level (not global) variable


    function a(){
         return ++x;
    }

    function b(){
         return ++x;
    }

    a();  // 6
    b();  // 7

 }());
Scott Marcus
  • 64,069
  • 6
  • 49
  • 71
0

There are several ways to control/prevent global variables is to create a closure function and put all your code in there. All code will be in the DOM as an anonymous function. You can also give that function a name so it will be easier to test. You can also create a global object and attach all your methods, variables, and properties onto that. I'm sure you've seen this before.

var myAppObj = myAppObj || {};
myAppObj.name = "tommy";
MyAppObj.odStuffFunc = function() {
      // do stuff
};
andre mcgruder
  • 1,120
  • 1
  • 9
  • 12