javascriptfunction

Dynamic function name in JavaScript


I have this:

this.f = function instance(){};

I would like to have this:

this.f = function ["instance:" + a](){};

Solution

  • As others mentioned, this is not the fastest nor most recommended solution. Marcosc's solution below is the way to go.

    You can use eval:

    var code = "this.f = function " + instance + "() {...}";
    eval(code);