user3024007
user3024007

Reputation: 283

Translating string into object

I'm constructing what needs to be a name of var on the fly using

var myNameToConstruct = 'prefix_' + t; // where t is a value passed as string from a function

Using this name I need to initiate a method of an object earlier defined as

var prefix_someName = $("#ele").getObjInstance(); // where getObjInstance is a method used by this particular object.

prefix_someName from my second line of code is an object in itself but the name I'm constructing is a string so even though it looks the same, it is not the same thing (what I gather from this code blowing up)

Is there a way to turn prefix_someName(string) into prefix_someName(object) on the go so that its recognized properly and the method can be called on it or do I have to rewrite code on a deeper level to make this happen?

Upvotes: 0

Views: 45

Answers (1)

bagonyi
bagonyi

Reputation: 3328

If your objects are defined globally:

window[myNameToConstruct].methodToCall();

Upvotes: 1

Related Questions