Totty.js Totty.js - 2 years ago 100
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 Source


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 + "() {...}";
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download