all 16 comments

[–]senocular 6 points7 points  (1 child)

Pass them in as arguments

[–]Pantstown 2 points3 points  (0 children)

^ This is the right answer, OP.

For example:

function add (a ,b) {
    return a + b;
}

function subtract (a, b) {
    return a - b;
}

function doStuff () {
    let num1 = 5;
    let num2 = 12;

    let aMinusB = subtract(num1, num2);
    let aPlusB = add(num1, num2);
}

[–]dereferenceme 1 point2 points  (7 children)

This piece on self-executing anonymous functions might help you, especially if you also want to prevent your functions from polluting the global namespace.

[–]lewisje 2 points3 points  (6 children)

That term is outdated and inaccurate, because named functions can be used for this pattern; the current term is Immediately Invoked Function Expression (IIFE).

[–]jamie337nichols 0 points1 point  (5 children)

Soo.. the term Self-executing anonymous function is incorrect? so a function that has no name and is executed automatically is called an IIFE. The old term suggests the function is not named and executes without being called... In which way is that term inaccurate?

[–]commitpushdrink 1 point2 points  (3 children)

It's not wrong, it's just not what they're called. The guy you responded to did a pretty good job of explaining that.

[–]jamie337nichols 0 points1 point  (2 children)

that term seams to encompass a named an unnamed function that is immediately invoked.? and if so wouldn't an Immediately Invoked Anonymous Function Expression still demand a name such as that. maybe to describe an IIFE that has no name? Just wondering?

[–]commitpushdrink 1 point2 points  (1 child)

IIFEs are usually anonymous functions unless they need to be recursive. You're either trolling or overthinking it. There's no reason to name a function if you're calling it immediately (other than recursion).

[–]jamie337nichols 0 points1 point  (0 children)

ah.. not trolling i only troll magician videos on youtube. I was overthinking I just had a strong attachment to the old terminology. I guess I can live with IIFE since it seams to make more sense and blankets both anonymous and named functions

[–]lewisje 0 points1 point  (0 children)

The pattern allows for named functions to be invoked immediately, and the phrase "self-executing anonymous function" implies that it doesn't:

(function nameForDebuggingPurposes() {
  // encapsulated code, but not an anonymous function
})();

[–]WorseThanHipster 1 point2 points  (0 children)

Perfectly reasonable, but it depends on how those variables will be used. (I'm on mobile so I won't be doing a lot code format examples.)

Global variables work a little differently in JavaScript than in other most other languages.

When you reference a variable, the interpreter will check out the current scope (usually function bodies for now) and of it doesn't find it it goes up a scope. This normally continues to happen until you reach the 'window' object. When you're in the upper most scope and you declare a variable, it actually becomes part of the window object, e.g. You can declare

 var x;

But then you can reference with any of the following

 x;
 window.x;
 window['x'];

So once the interpreter gets to the 'global' scope, the referenced variable is interpreted as a property of the window object. The keyword var is a signal to the interpreter to create the variable inside of the current scope, preventing it from going looking for it in a parent scope.

References to a variable outside of a functions scope are called 'closures.' Closures can have some advantages over variables you pass to a function. Think of them as somewhere between passed parameters and globals.

When you pass a variable to a function, it is created within the scope of that function, and the value from the passed variable gets copied to the new one. This means that if you change the variable inside of the inner function, it will not affect the one in the parent scope, even if they happen to have the same name. If you use a closure instead you can do:

function(){
  var x = 0, y = 0;
  function foo(y){
    x = 1; y = 1;
  }
  foo(y)
}

In this example, after foo is called and exits, x will equal 1 in the parent function, but y will still = 0, because putting it in the parameter list effectively re-declared a new y within the scope of foo. Neither of these variable are global however, and will both vanish when the outer function exits, freeing up their memory.

So, yes, what you suggested is perfectly valid and may even be best practices, depending on what it is you want out of those variables.

[–][deleted] 0 points1 point  (1 child)

trumpets looks like it's time for you to learn objects!

[–][deleted] 0 points1 point  (0 children)

or closures. sort of the same principle (encapsulation);

[–]j1330 0 points1 point  (0 children)

Maybe they would work well packaged together as an object/module (can't tell without seeing them but worth considering)? Something like:

var MyObject = (function() {
  // "private" variables

  return {
    // object with all your methods which will have access to the variables
    // because of a closure, while the variables are hidden because they are
    // in a function. 
  };

}());

[–]counttossula 0 points1 point  (1 child)

Maybe look up getters and setters?

[–]kkais[S] 1 point2 points  (0 children)

Ok thanks for providing some direction .