Search code examples
javascriptobject-literal

How can I invoke a member function using bracket notation?


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.


Solution

  • No, do this:

    objectliteral['func1']();
    

    You can also use a dynamic name:

    var myfuncname='func1';
    objectliteral[myfuncname]();