Stories
Slash Boxes
Comments
NOTE: use Perl; is on undef hiatus. You can read content, but you can't post it. More info will be forthcoming forthcomingly.

All the Perl that's Practical to Extract and Report

use Perl Log In

Log In

[ Create a new account ]

TorgoX (1933)

TorgoX
  sburkeNO@SPAMcpan.org
http://search.cpan.org/~sburke/

"Il est beau comme la retractilité des serres des oiseaux rapaces [...] et surtout, comme la rencontre fortuite sur une table de dissection d'une machine à coudre et d'un parapluie !" -- Lautréamont

Journal of TorgoX (1933)

Monday March 07, 2005
04:44 AM

Block scope in JavaScript

[ #23532 ]
Dear Log,

JavaScript has scoped variables -- but no block-scope. So:

var x = 123;

function zot () {
  var x = 456;

  {
    var x = 789;
  }

  alert("oboy " + x.toString());
  return;
}

zot();

// pops up "oboy 789"

But there's function scope, so you can fake block-scope by making and then calling an anonymous function:

var x = 123;

function zot () {
  var x = 456;

  (function () {
    var x = 789;
  })();

  alert("oboy " + x.toString());
  return;
}

zot();

// pops up 456.

The Fine Print: The following comments are owned by whoever posted them. We are not responsible for them in any way.
 Full
 Abbreviated
 Hidden
More | Login | Reply
Loading... please wait.