0

I have these two javascript files:

test1.js:
(function() {
  console.log(global_var_test2);
})();

test2.js:
(function() {
  global_var_test2 = "test";
})();

Obviously if i use the var keyword in test2.js the global_var_test2 variable will not be available in test1.js..but i thought that when you were wrapping all your code in a file inside a self-executing anonymous functions that you created a separate scope so that variables that were created without the var keyword would still not be visible outside ? When running the code above im able to access global_var_test2 inside of test1.js.

If im remembering correct the use of a self-executing anonymous function is almost always used when writing javascript modules to isolate it from the other possibly installed modules you have..but that doesnt seem to work with the code above.. could someone explain why not ?

exceed
  • 459
  • 7
  • 19
  • The order matters. If you execute code in that order, `global_var_test2` hasn't been initialized yet. – zzzzBov Mar 28 '15 at 15:04
  • Related: [What is the function of the var keyword and when to use it (or omit it)?](http://stackoverflow.com/questions/1470488/what-is-the-function-of-the-var-keyword-and-when-to-use-it-or-omit-it) – Jonathan Lonowski Mar 28 '15 at 15:06

2 Answers2

3

Your understanding is incorrect. If you assign to a variable without declaring it with var, you're creating a global variable, wrapper or no wrapper.

In "strict" mode, that would be an error. Therefore, if you really want to make sure you're not polluting the global environment — which is smart — you put your code in "strict" mode:

(function() {
  "use strict";
   // ... your code here
})();

If you accidentally forget var, you get an error. If you want a global variable, you can check for it:

  if ("myGlobalSymbol" in window)
    throw new Error("Something stole myGlobalSymbol from me!");

or whatever.

Pointy
  • 405,095
  • 59
  • 585
  • 614
  • Ok... then if you are writing a javascript module, what do you do if another module is using the same variable name ? – exceed Mar 28 '15 at 15:08
  • @sundance put your code in "strict" mode and then the system won't let you accidentally create a global variable. – Pointy Mar 28 '15 at 15:09
  • So basically you should always be using an anonymous self-executing function even if you are writing some validation to avoid breaking other code..and always use var inside of it. I've been using strict some times for other languages before but find it a bit too strict sometimes which is why i usually end up turning it off – exceed Mar 28 '15 at 15:15
  • @sundance That is best practice yes. You can also pass libraries & window to your function so that when you minify the JavaScript the longer names can be compressed. – Lexi Mar 28 '15 at 15:17
  • 1
    @sundance "strict" mode really isn't very strict; anything you're doing that "strict" mode disallows is *probably* a bad idea. – Pointy Mar 28 '15 at 15:17
  • @sundance Which features of strict mode cause you issues? – Lexi Mar 28 '15 at 15:19
  • @Lex R i havent been using strict mode in javascript but for languages ive been having a number of problems, dont really remember the problems now as its a while ago since i used strict mode for any language but perl was the last language i used it with i think – exceed Mar 28 '15 at 15:28
  • @sundance there is absolutely no relationship between the concept of "strict" mode in JavaScript and any sort of "strict" mode in other languages. It's meant as a way to patch over old, bad design decisions in JavaScript specifically, and has nothing to do with any general notions of strictness. – Pointy Mar 28 '15 at 15:31
  • @Pointy MDN says its a restricted set of javascript with even different semantics though which would appear to be more strict, source https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Strict_mode – exceed Mar 28 '15 at 15:44
  • @sundance Yes, it's restricted. If you have no idea what the restrictions are, or if you don't understand why they're valuable, you should probably study some JavaScript tutorials. Strict mode in JavaScript does not make anything harder other than some bad (or *very* bad) practices. – Pointy Mar 28 '15 at 15:48
1

Variables created with var outside of a function are global - the same as if you had not used var.
Since you should be defining all of your variables via either var myvar or window.myvar = stuff, immediately invoked functions are used to prevent your var statements from polluting the global environment and potentially causing clashes.

Lexi
  • 1,670
  • 1
  • 19
  • 35