0

I have following function

function myFunction(){
var foo="My Foo String";
}

I would like to access my variable in a following way

function myFunction(){
var foo="My Foo String";
console.log(SOMETHINGIDONOTKNOWABOUT["foo"]);
}

I know in javascript its possible to use window["yourvariablename"] to access it, but I am interested to access function variable in similar way, therefor Is there a way to access a javascript variable using a string that contains the name of the variable? is not a duplicate. Eval is not correct answer for me.

trixo
  • 544
  • 4
  • 14
  • 1
    I'm finding it very difficult to figure out what you're asking here. Can't you just use `foo`? – Carcigenicate Jun 07 '18 at 13:01
  • no I cant, because the variable is called dynamically. It comes as a string representation inside an object – trixo Jun 07 '18 at 13:04
  • You cannot access local variables in one function from a context outside that function. From inside the function, you can access the variables by name but not dynamically in any way analogous to `window["someName"]`. – Pointy Jun 07 '18 at 13:07
  • `eval` is the *only* answer for accessing local variables by name. If you don't want to do that (and indeed, you should not), then **don't access local variables by name**. What is your [actual problem](https://meta.stackexchange.com/q/66377)? – Bergi Jun 07 '18 at 13:08
  • @Pointy Well Its the same function. I want to access it from inside my function. I just have no idea about how to access all variables that are inside the function in the same way as window["variablename"] works – trixo Jun 07 '18 at 13:10
  • There is no good way to do it. You can use the `[ ]` to access object properties dynamically, which is what `window["someName"]` involves, but there is no object to reference that way in the case of local variables. (It's possible with `eval()`, but there's generally no good reason for it and it has serious performance implications if you care about stuff like that.) – Pointy Jun 07 '18 at 13:11

3 Answers3

1

Don't do this. Dynamic variables are really annoying to debug and optimize by the browser.

Use a set, a map, create a class for it, or even a simple object to store things you want to be able to access with a string.

function myFunction() {
    var myData = {
        "foo": "My foo string"
    };
    console.log( myData[ "foo" ] );
}
Shilly
  • 8,511
  • 1
  • 18
  • 24
0

If I understand you question properly, you should use foo without quotes:

window[foo]

Just note that putting values on the global scope is a bad practice.

Meir
  • 14,081
  • 4
  • 39
  • 47
  • 1
    I do not think its correct answer as my variable is inside a function therefore I cant access it using window[] – trixo Jun 07 '18 at 13:06
0

Is the following snippet the kind of things you are trying to achieve?

If you want to use a variable as an array element name, you don't have to use quotes. i.e. array["foo"] should be array[foo] if foo is a variable.

var array = [];
array['foo'] = 'bar';

// Code #1
function myFunction(element) {
  console.log(array[element]);
}
myFunction('foo');

// Code #2
function myFunction() {
  element = 'foo';
  console.log(array[element]);
}
myFunction();

Hope it helps, in any way.

Takit Isy
  • 9,688
  • 3
  • 23
  • 47
  • @trixo But where is defined your array? I assumed it was global. I added another code, by the way. – Takit Isy Jun 07 '18 at 13:12
  • Sorry, I havent noticed your comment.There is no array. You can use window["variableName"] to access window variables with a string representing variable name. – trixo Jun 07 '18 at 13:18