I'm trying to replicate a "feature" of PHP (And in most languages) in Javascript.
Here it is in PHP:
$objectName = 'ObjectA';
$someObject->$objectName->someMethod();
Basically using a string variable to reference an object variable.
So in Javascript, I was hoping to do something like:
var objectName = "ObjectA";
someObject.[objectName].someMethod();
Anyone know how to do this? Or if its even possible?
I'm trying to replicate a "feature" of PHP (And in most languages) in Javascript.
Here it is in PHP:
$objectName = 'ObjectA';
$someObject->$objectName->someMethod();
Basically using a string variable to reference an object variable.
So in Javascript, I was hoping to do something like:
var objectName = "ObjectA";
someObject.[objectName].someMethod();
Anyone know how to do this? Or if its even possible?
Share Improve this question asked Sep 2, 2010 at 23:28 CharlieCharlie 1,7565 gold badges24 silver badges44 bronze badges1 Answer
Reset to default 14You almost have it, just remove the first .
, like this:
var objectName = "ObjectA";
someObject[objectName].someMethod();
If you want to search for more info around this, it's called bracket notation.