0

I saw a lot of answer to call a method from a given object using a string, but no one to get the object itself. I would like something like that

var a
var b
var c

function getObject(objectAsString)
{
return getMyObject(objectAsString);
}

then if I write

var obj=getObject("a")

my result is obj=a

Is there a function "getMyObject"? Thanks

Hermios
  • 622
  • 1
  • 5
  • 20

2 Answers2

2

See following code

<script>
//in one script
var GlobalvarName = 500;

alert(window["GlobalvarName"]); //alert is : 500

</script>
Rahul Bhawar
  • 450
  • 7
  • 17
1

you could do:

var hello = 'Hello World';
this['hello'];

I wouldn't make those variables so global though. here's why Instead put them inside an object like:

var obj = {
   a: 'Hello',
   b: 'World'
}
console.log(obj['a'], obj['b']);
Community
  • 1
  • 1
Nikki Koole
  • 136
  • 1
  • 8