0

I have a situation in which the name of my object will be dynamic..

i.e.

txtGrantAccess_5.GetValue();

i want to know how can I call this dynamically?

e.g.

var abcd = 'txtGrantAccess_5';
abcd.GetValue();

Please note that txtGrantAccess_5 already exists, please can you also avoid the usage of eval in your answer? I have tried understanding other questions almost similar to this but they are not the same thing...

5
  • Do you mean you will have some global variable with a name you don't know yet? Commented Oct 15, 2013 at 23:40
  • If you don't know where the function is stored eval is the only option. Commented Oct 15, 2013 at 23:44
  • How would you know the name of this dynamically named variable? Commented Oct 15, 2013 at 23:45
  • @promanski i have a keyValue so i will know for definite the name will begin with 'txtGrantAccess_'+ keyValue will give me the actual object name that has already been created by server side programming for me toaccess on client side Commented Oct 15, 2013 at 23:50
  • @Musa read my comment above Commented Oct 15, 2013 at 23:53

3 Answers 3

2

You cannot get local variables dynamically. However, if it's a property of an object you can get it with the [] syntax.

var obj = {};
obj.foo = function() {};
obj.foo();
obj['foo'](); // same as above.

But in this case a local variable cannot be fetched dynamically at all.

var fn = function() {
  var foo = '123';
  // no way to get foo dynamically.
}

One exception is global variables. In the global scope, local variables are created as properties of the window object:

// global scope, outside of any function.
var foo = function() {};
window.foo();
window['foo'](); // same as above

Just keep in mind that lots of global variables are usually frowned upon, espcially if there is enough that you need to find dynamically like this. You probably just want a container object to keep these values in, like the first example I posted.

Sign up to request clarification or add additional context in comments.

5 Comments

-1, You can get local variables dynamically, just because people don't like eval doesn't mean you can't use it.
@Connor OP actually said avoid the usage of eval in the post
@Musa i know, this question states nothing about eval but does say that it's not possible to get local variables dynamically.
Basically, all I'm saying is "You cannot get local variables dynamically." is false and "no way to get foo dynamically." is false
@Conor I tend to not think of eval because I've been burned by it too many times :) So yeah, it's possible with eval, but sure as hell ain't advisable.
0

Try this snippet

var abcd = 'txtGrantAccess_5';
this[abcd].GetValue();

but be careful with value of "this". If it's in a browser maybe this will help

var abcd = 'txtGrantAccess_5';
window[abcd].GetValue();

2 Comments

works like a charm thanks! would this compatible with all browsers? i am talking about the solution with "this"
@inN0Cent the problem with "this" in JavaScript is that it differs from "this" keyword known in most of other languages. Please google for this this :)
0

How about:

var container = {};
var name = 'dynamicName';
container[name] = new MyObject();
container[name].getValue()

1 Comment

the issue is that its not a new object.. its an existing object txtGrantAccess_5 already exists dynamically created by server side programming..

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.