I have a situation in which I need at some point in my application the name of the constructor
instance.constructor.name
However, when I create the constructor I have its name stored in a variable. So, to give you an idea of what I want
var nameOfTheFunction = "Test" ;
var BarFoo = function() { /* do stuff */ } ;
BarFoo.name = nameOfTheFunction ;
This of course doesn't work, because the name property is read-only.
So is it possible to dynamically create a constructor function with the end result that the instances it creates will have
instance.constructor.name === nameOfTheFunction
UPDATE: checkout my comment below, because it does what I was looking for. For this questions though, thanks to the answer, I initially was looking for this
(new Function( 'base', 'return function ' + Foo.name + '(){ base.apply(this, arguments); };'))(Foo) ;
The problem with this solution is that the thing created here isn't an instanceof Foo, except when you do something like this, which brought me to the solution in my comment below!