Totty.js Totty.js - 6 months ago 17
Javascript Question

Dynamic function name in javascript?

I have this:

this.f = function instance(){};


I would like to have this:

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

Answer

update

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);