admin管理员组

文章数量:1356866

Working example with global var:

var example_var = 'global var example';

var x = function(){
    var var_name = 'example_var';
    alert('Global var value is: ' + window[var_name]);
}

How can I do same thing with a local variable? Like this (not working example):

var x = function(){
    var example_var = 'Local var example';
    var var_name = 'example_var';
    alert('Local var value is: ' + window[var_name]);
}

Working example with global var:

var example_var = 'global var example';

var x = function(){
    var var_name = 'example_var';
    alert('Global var value is: ' + window[var_name]);
}

How can I do same thing with a local variable? Like this (not working example):

var x = function(){
    var example_var = 'Local var example';
    var var_name = 'example_var';
    alert('Local var value is: ' + window[var_name]);
}
Share Improve this question edited Jan 27, 2010 at 11:06 duffymo 309k46 gold badges375 silver badges567 bronze badges asked Jan 27, 2010 at 10:49 kleshklesh 851 silver badge3 bronze badges 6
  • Duplicate: stackoverflow./questions/39960/javascript-locals – Ignacio Vazquez-Abrams Commented Jan 27, 2010 at 10:55
  • Why? This is usually a sign of dealing with variables the hard way. – Anonymous Commented Jan 27, 2010 at 10:58
  • It's clean example of problem. In fact, this code is used to extend objects by different methods, whose names are contained in the objects themselves. – klesh Commented Jan 27, 2010 at 11:05
  • @oswork: There's no function-scope object you can access from regular Javascript to extend, so that problem doesn't apply here. What problem are you trying to solve? – Anonymous Commented Jan 27, 2010 at 11:06
  • Possible duplicate of JavaScript: Get local variable dynamically by name string – Michał Perłakowski Commented Jan 12, 2016 at 0:30
 |  Show 1 more ment

4 Answers 4

Reset to default 7

If you have no other way, you can try eval it

var x = function(){
    var example_var = 'Local var example';
    var var_name = 'example_var';
    alert('Local var value is: ' + eval(var_name));
}

You don't want to use eval; a locally scoped object might be your best option:

var x = function(){
    var self = {};
    self.example_var = 'Local var example';

    var var_name = 'example_var';

    alert('Local var value is: ' + self[var_name]);
}

possibly:

var x = function(){
    var example_var = 'Local var example';
    var var_name = example_var;
    alert('Local var value is: ' + var_name);
}

or:

  var x = function(){
        var example_var = 'Local var example';
       window.var_name = example_var;
        alert('Local var value is: ' + window[var_name]);
    }

or

  var x = function(){
            var var_name = 'Local var example';    
            alert('Local var value is: ' + var_name);
        }

At the moment there are two solutions to this problem.
1. eval(), but i really don't like to use evil()
2. we can change variable declaration from var to this:

var x = function(){
    this.example_var = 'this.var example';
    this.var_name = 'example_var';
    alert('Local variable value is: ' + this[var_name]);
}

本文标签: javascriptHow to get local variable by its name in JSStack Overflow