Totty.js Totty.js - 1 year ago 85
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 + "() {...}";