Suppose I pass a string as an argument to a function, but I want that string to represent a variable or a function (assume no scope issues).
How can I accomplish this?
Suppose I pass a string as an argument to a function, but I want that string to represent a variable or a function (assume no scope issues).
How can I accomplish this?
Can you post some code so we can see a little more clearer?
eval(“blah”) or window[“blah”] for globals.
eval(“xyz(” + i + “)”);
assuming i = 10, this is equivalent to
xyz(10);
I knew of eval, will that also work for function names?
Say I pass “Math.sin” to a function argument of myFunc.
Would: eval(myFunc(Math.PI)); give me 0 as it should?
Also, what about this situation:
var a = 123;
var b = 456;
function testFunc(var1, var2) {
}
If I pass “a” or “b” as var1 or var2, how can I get them to point to the variables of the same name and not be strings? I could put everything in an object and handle it that way, but I’d rather not. I’ve also used a switch with a third variable:
switch(var1) {
case “a”: value = 123;
case “b”: value = 456;
}
value being the extra variable.
Is there a more efficient way to accomplish that?
Thanks,
Shane