javascript - How do I access a local variable dynamically (via a String form of its name) from a closure scope? -


this question has answer here:

in javascript, i'm used being able access variables within known namespace "dynamically" (correct me if i'm using wrong word here) using [] operator. instance (from global namespace):

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

or object-type namespace:

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

and i'm familiar basics of how this determined:

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

but within function itself, how access local variables i've instantiated (or redefined) using var?

namely, want following function call return 1 without calling a:

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

you can't use window['a'] because a defined locally, , can't use this['a'] because this changes depending on context function called.

in realistic setting, i'd refactor avoid dynamically creating , accessing local variables because it's bad idea anyway, academic question, i'm curious whether it's possible access a via string name @ all.

you're mixing local variables (which not properties of object) properties (which not local variables). there no answer question, or, rather, answer "it can't done".


Comments

Popular posts from this blog

Javascript line number mapping -

c# - Is it possible to remove an existing registration from Autofac container builder? -

php - Mysql PK and FK char(36) vs int(10) -