Search code examples
javascriptscope-chain

Force to move up the scope chain in JS


Is there a way to reference the next object up the scope chain in Javascript?

So, for example:

var a = 12;
function run() {
  var a = 34;
  alert( a ); //replace "a" with something like "parent.a" so it shows 12 instead of 34
}
run();

Solution

  • Nope, there is not. Javascript doesn't expose the scope chain at all really (much to my chagrin; I'd love to be able to write a function that adds variables to the local scope, but alas I can't).