How to access a local variable dynamically (via the String form of its name) from the closing area?

In Javascript, I'm used to having access to variables in a known namespace โ€œdynamicallyโ€ (correct me if I use the wrong word here) using an operator []. For example (from the global namespace):

var a = 1;
window['a']; # => 1

Or from an object type namespace:

var a = { b: 1 };
a['b']; # => 1

And I am familiar with the basics of the definition this:

var a = function(){ return this['c']; };
var b = { c: 1 };
a.apply(b); # => 1;

But inside the function itself, how do I access the local variables that I just created (or redefined) using var?

Namely, I want the next function call to return 1, but without calling a:

function(){
  var a = 1;
  return a;
}

window['a'], a , this['a'], this , .

, , , , , , a .

+3
1

( ) ( ). , , , " ".

+3

Source: https://habr.com/ru/post/1794216/


All Articles