tags:

views:

86

answers:

4

If only string is allowed to pass into function process(), what should we do inside the function process() to access the array value. Eval is one of the method, but its usage is not suggested by many people.

function demo2(name2)
{
 var alpha = [];
 alpha["a"] = "test1";
 var bravo = [];
 bravo["a"] = "test2"; 

 function process(name)
 {
  alert(window[name]["a"]);
 }
 process(name2); // error
    }

name2 can be "alpha" or "bravo" or many other arrays' name.

var alpha = [];
 alpha["a"] = "test1";
 var bravo = [];
 bravo["a"] = "test2"; 
    function process(name)
 {
  alert(window[name]["a"]);
 }
    process("alpha");

For the second example, it works fine. I just want to pass the string into the function and use it as the array name in the first example. I have the 2nd example and so I would like to know how I can do this inside a function.

What should I write inside the function process to alert the alpha and bravo variables?

A: 
function demo2()
{
var alpha = [];
alpha["a"] = "test1";
var bravo = [];
bravo["a"] = "test2"; 

function process(name)
{
        var x = eval(name);
        alert(x["a"]);
}
process("alpha");
process("bravo");

}
Anatoliy
Eval should be avoided.
lod3n
Topicstarter's cases should be avoided )Don't understand why not use local variables in closure.
Anatoliy
What I want is like this case.if only string is allowed to pass into function process(), what should we do inside the function process() to access the array value. Eval is one of the method, but its usage is not suggested by many people.
Billy
Sorry, my fault - omit your comment about eval. But I don't know another way to make variable from string (only global).
Anatoliy
+2  A: 

What about this?

function demo2()
{
    var alpha = [];
    alpha["a"] = "test1";
    var bravo = [];
    bravo["a"] = "test2"; 

    function process(name)
    {
        alert(name["a"]);
    }
    process(alpha);
    process(bravo);
}
Miky Dinescu
+2  A: 

process can access alpha and bravo directly (without being passed).

The reason why you can't access those variable via window[name] is because they are not global variables.

function demo2()
{
    var alpha = [];
    alpha["a"] = "test1";
    var bravo = [];
    bravo["a"] = "test2"; 

    function process(name)
    {
        alert(alpha["a"]);
        alert(bravo["a"]);
    }
    process();
}

Also, using alphanumeric array subscripts kinda beats the purpose of using array (not that it's not possible). The elements with non-numeric subscripts are left out of all the built-in array functions (such as join, slice, shift).

You can use plain objects for that purpose.

var obj = {};
obj.a = "test";   //OR
obj["a"] = "test";

[Edit]: Responding to your comment, there's almost never a good reason to refer to variables with a string containing their name. But if you have to do it, you may consider making all your arrays properties of an object. This is same as referring to window["something"] if they were global variables except you're substitution window with your own object.

function demo2()
{
    var container = {};
    container.alpha = [];
    container.alpha["a"] = "test1";
    container.bravo = [];
    container.bravo["a"] = "test2"; 

    function process(name)
    {
        alert(container[name]["a"]);
    }
    process("alpha");
    process("bravo");
}
Chetan Sastry
I know we can access the array directly. I have just changed the question. Hope you understand what I want.
Billy
A: 

To answer your question:

function process(name) {
        alert(eval(name+'["a"]'));
}

Disclaimer: If you don't have a good reason to do this, I'd refactor your scoping - it's pretty ugly.

andersonbd1