Using a string to refer to an object name

I am trying to replicate a "function" of PHP (and in most languages) in Javascript.

Here it is in PHP:

$objectName = 'ObjectA';

$someObject->$objectName->someMethod();

Mostly a string variable is used to refer to an object variable.

So, in Javascript, I was hoping to do something like:

var objectName = "ObjectA";

someObject.[objectName].someMethod();

Does anyone know how to do this? Or if it is even possible?

+5
source share
1 answer

You almost have this, just delete the first one ., for example:

var objectName = "ObjectA";
someObject[objectName].someMethod();

If you want to find more information about this, it is called a notation bracket .

+14
source

All Articles