Let us say there are some widgets:
function Widget1(initData) {
....
}
Widget1.prototype.render = function() {
....
}
function Widget2(initData) {
....
}
Widget2.prototype.render = function() {
....
}
What I need to do is the following:
$.subscribe('/launch', function(widgetName, initData) {
// create a new object of the widget and then call the render method
});
I dont want to write multiple if-else blocks as the number of widgets may become very large. One option is to use eval(), but I believe that there may be better techniques. I am using JQuery framework, so don't want to include any other frameworks that may have a specific feature to support this. A pure Javascript solution will be appreciated.
eval('var x = new ' + widgetName);would be one extremely ugly hackish massively bad way of going about it.new window[widgetName]()