At the moment I am using eval()
to create an object dynamically, function constructor called is actually in closure.
The string in eval()
, in my real app code is created based on a condition.
I would like to know if it is possible to achieve the same result avoid using eval()
.
I would be interested in a solution which avoiding adding Icon
and Logo
to window
global, and using syntax like window[myDesiredFunction]
when declaring the new object. Also I cannot use any hard-coded switch case statements.
(function(window){
var Icon = function(id){
this.id = id;
};
var Logo = function(id){
this.id = id;
};
var app = {
start:function(){
var item1 = eval("new Icon('iconA')"); // i need replace eval()
var item2 = eval("new Logo('logoB')"); // just as example string will be created with some condition dynamically
console.log(item1 .id);
console.log(item2 .id);
}
};
app.start();
})(window);