javascript - Alternative to eval for accessing object by string name? -
i have following
eval(id+' = new myobject()');
and want access resulting object , methods. with
myobject = eval(id); myobject.mymethod();
but elegant alternatives there using eval?
assume id
intended global object , you're executing in browser,
window[id] = new myobject(); myobject = window[id]; myobject.mymethod();
or modify code store id
object.
var objectstore = {}; objectstore[id] = new myobject(); myobject = objectstore[id]; myobject.mymethod();
Comments
Post a Comment