Javascript function parameter to be used as constructor -
i'm trying reusable module, lesser code. work? (well, not going work, it's example give idea)
var test = function(foo) { var bar = new foo(); // keyword foo parameter }
the parameter string. if passed "foobar", foobar
object created.
it's saying:
var test = function('foobar') { var bar = new 'foobar'(); }
are there other elegant ways fit solution i'm looking for?
many thanks!
if foo
constructor function, code work fine (i'm curious why didn't try it). can see work here: http://jsfiddle.net/jfriend00/yt6vk/.
if want advise on other/better ways solve problem, you'll have , describe more you're trying do.
there alternate ways of doing things such factory functions or single function examines it's arguments decide type of object create, without more info on problem you're trying solve, can't whether other methods better/worse have.
a disadvantage of method haven't allowed arguments passed constructor.
Comments
Post a Comment