0

Quick question - It is possible to reference a shadow variable from a IIFE? I would like to reference to the a in global, is that possible?

var a = 2;

(function foo(){

    var a = 3;
    console.log( a ); // 3
    console.log( this.a ); // I want to reference to the var a = 2 in global
})();

console.log( a ); // 2
Six80
  • 677
  • 1
  • 9
  • 17

1 Answers1

0

It is possible, (though not the best thing to do). You could reference the window object:

window.a = 2;

(function foo(){
    var a = 3;
    console.log(window.a);  // 2
...
typologist
  • 394
  • 3
  • 10