PHP-style $GLOBALS in Javascript?

March 8th, 2008. Tagged: JavaScript

Javascript has implied globals. When you skip the var in var a = 1; and go a = 1;, then a becomes a global variable. Some consider this an error in the language. Global variables should be avoided because they tend to overwrite each other in unexpected places, especially if the project grows in LOC and number of developers.

In PHP on the other hand, variables are local. If you need a global variable, then you have to have to be explicit about it using the $GLOBALS superglobal array.

So how about this: adopt the $GLOBALS convention in your JavaScripts? At the top of the script you go:

$GLOBALS = {};

Then every time you need a global variable, you do:

$GLOBALS['myglob'] = 1; // very PHP-like

or if you prefer:

$GLOBALS.myglob = 1;

Benefits of the approach:

  • global variables easy to spot (even from an aeroplane)
  • if it's not $GLOBAL, it's meant to be local. If it's missing the var, it's an error

Drawback:

  • It's a convention, so it can only help, but not enforce any coding practices

How many globals

Here's a quick test to check how many globals you have in a page.

(function(){
  var globs = 0;
  for (var i in window){
    globs++;
  }
  alert(globs);
})()

Run this script in your page without the scripts. Then add the scripts to the page run again. The result should be only one more global var if you followed the $GLOBALS convention.

Tell your friends about this post: Facebook, Twitter, Google+

12 Responses

  1. Its actually a nice idea, I quite like it.

    What I really do like though, is that the stuff you want to be global, isn’t mixed in with all the clutter that the browser (especially IE) wants to be global. e.g. if you run your globs code on a blank window in IE, you’ll get a bunch of objects and properties before adding a single variable.

  2. Thanks Steve! Worst of all, <input id="tmp" /> declares a global var tmp

  3. Stoyan Stefanov’s Blog: PHP-style $GLOBALS in Javascript?…

    In a new post to his blog today, Stoyan Stefanov has ……

  4. [...] In a new post to his blog today, Stoyan Stefanov has a proposal to being something PHP users are very used to – superglobals – over to Javascript. Javascript has implied globals. When you skip the var in var a = 1; and go a = 1;, then a becomes a global variable. Some consider this an error in the language. […] In PHP on the other hand, variables are local. […] So how about this: adopt the $GLOBALS convention in your JavaScripts? [...]

  5. They aren’t really globals in JavaScript. The scope actually comes from the parent. Undefined variables appear to be global, that’s true.

    Scoping is really “weird” in JS if you’ve never worked in another language that does closures. See:
    http://en.wikipedia.org/wiki/Closure_(computer_science)

    Not saying you’re wrong, of course, but thinking about JavaScript scopes the same way you think about PHP scopes is likely to lead to maintenance/development trouble down the road (-:

    S

  6. [...] In a new post to his blog today, Stoyan Stefanov has a proposal to being something PHP users are very used to – superglobals – over to Javascript. Javascript has implied globals. When you skip the var in var a = 1; and go a = 1;, then a becomes a global variable. Some consider this an error in the language. […] In PHP on the other hand, variables are local. […] So how about this: adopt the $GLOBALS convention in your JavaScripts? [...]

  7. [...] In a new post to his blog today, Stoyan Stefanov has a proposal to being something PHP users are very used to – superglobals – over to Javascript. Javascript has implied globals. When you skip the var in var a = 1; and go a = 1;, then a becomes a global variable. Some consider this an error in the language. […] In PHP on the other hand, variables are local. […] So how about this: adopt the $GLOBALS convention in your JavaScripts? [...]

  8. Interesting idea Stoyan!

    Just out of curiosity though: why use a dollar sign? Just because it’s more PHP-ish or is there another reason?

    Also: In a YUI context you may want to use YAHOO.GLOBALS maybe? :)

  9. Hi Marco,

    I used the dollar sign just for the sake of the PHP analogy, and a little bit as a joke in the spirit of “let’s make JS like our other favorite language” (looking at you, prototype.js).

    Using a namespace and a GLOBALS property is definitely a good way to do the same.

  10. PHP GLOBALS suck. ANd bcos of this PHP suckz… how many noobz here use GLOBALS… my server always has register_globals turned off…. now the scripts of these basturds wont run… ha haaaaaaaaaaaa

  11. Välj mellan mat, ståtlighet, hälsa, småbarn, husdjur, hus riktning, böcker ytterligare tidningar, kontorsmaterial, verktyg ytterligare mycket mer.
    erbjudande köpenhamn
    Till exempel: Ett exempel är vid onlinebooks.

  12. Beware of using print_r( ) or var_dump( ) on a recursive structure such as $GLOBALS (which has an entry for GLOBALS that points back to itself). The print_r( ) function loops infinitely, while var_dump( ) cuts off after visiting the same element three times.

Leave a Reply