Reputation: 13948
var objectliteral = {
func1:fn(){},
func2:fn(){},
.................
funcn:fn(){}
}
I know I can invoke methods from that object literal using dot notation this:
objectliteral.func1();
But I would like to do that using array notation like this:
objectliteral[func1]. .... something something......
How do I do that? I know I can use apply or call methods - but I'm still don't quite get how they work.
Can I just do this?:
objectliteral[func1].apply();
RESOLUTION
based on answers:
objectliteral['func1']()
is all I need. Thanks guys.
Upvotes: 9
Views: 3455
Reputation: 4809
You can do it like this:
var objectLiteral = {
func1: function() { alert('func1'); },
func2: function() { alert('func2'); }
};
// This does the same thing...
objectLiteral['func1']();
// As this does
objectLiteral.func1();
Upvotes: 3
Reputation: 388316
user objectliteral["funct1"].apply()
or objectliteral["funct1"]()
.
You can also use a variable to pass the function name
var funname = "funct1";
objectliteral[funname ]()
Upvotes: 1
Reputation: 45568
No, do this:
objectliteral['func1']();
You can also use a dynamic name:
var myfuncname='func1';
objectliteral[myfuncname]();
Upvotes: 15