23

i am new to object oriented javascript. I have a variable whose value i would like to use to call an object's method. like this..

var foo = {
    bar: function() {},
    barr: function() {}
}

now there is a variable whose value can be any of the two method's names bar and barr i want to call them with something like

var myvar = 'bar';
foo.{myVar}();
2
  • If you are starting to learn JavaScript, this may be a nice article to read: John Resig's Learning Advanced Javascript. I know it says "advanced", but it starts from basic examples, and has a nice interactive interface for modifying and running the code. Commented Jul 18, 2011 at 19:04
  • @Groo hey! thanks alot! a am new to object stuff and TBH they are a bit confusing.. :O but i am on it :D Commented Jul 18, 2011 at 19:06

6 Answers 6

52

So I assume you want to call the appropriate function dynamically based on a string. You can do something like this:

var myVar = 'bar';
foo[myVar]();

Or you can also use eval but this is riskier (prone to injection attack) and slower (don't do this! :P):

var myVar = 'bar';
eval('foo.' + myVar + '()');
Sign up to request clarification or add additional context in comments.

6 Comments

thanks.. there are so many answers :O looks like i asked a n00b question :P but hey thanks every body :)
There is no n00b question except the one that was just answered :)
:D so apparently they work like arrays too.. we can call them like we call an array array['key']. interesting. thnx!
@Achshar - In your example, foo is an object. All objects can access their properties with array-type syntax. It isn't really an array (which is why I wrote this comment to make sure you don't think it's an array), but you can use the foo['x'] syntax to access the properties just the same as you can use foo.x.
@jfriend00 oh yeaa i know that right :) i was just wondering that the syntax is same so that devs don't have to remember a bunch of different syntaxes for all the different things in the language :D
|
11

Since you can access elements of an object via subscript notation, the following will do what you're looking for:

var myVar = 'bar';
foo[myVar]();

Comments

8

You can just say:

foo[myVar]();

Since foo is a JavaScript object, this code will reference the member by name contained in the myVar variable.

Comments

5
var foo = {
    bar: function() { alert('bar'); },
    barr: function() { alert('barr'); }
}


var myvar = 'bar';
foo[myvar](); // alert 'bar'

Comments

4

Use something like: foo[myVar]();

1 Comment

calling inside the object methods this way doesn't seem to work. using try/catch the message says 'this[myVar] is not a function'
4

it should be something like this

foo[myvar]();

Comments

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.