views:

27

answers:

1

Please assume the following contrived JavaScript:

function do_something() {

  var x = 5;

  function alert_x() {
    alert(x);
  }

  alert_x();

}

do_something();

The variable x is local to the function do_something. It isn't a global variable because it's not available in every scope (i.e., outside of either of the functions, such as where do_something is called).

However, would it be proper to say that "the variable x is global to the function alert_x? Can "global" be used as a relative term in this sense?

+2  A: 

I recommend against doing this: it goes against convention and invites confusion. If you're doing this as part of the standard Javascript object idiom, just refer to x as a "member variable", or else as a "closure variable" (since it's captured by closure in alert_x).

JSBangs
Great to know, and that answers my question - thanks, JSBangs!
Bungle