Dealing with a dilemma here. I am attempting to dynamically create an instance of a class from its superclass. Essentially, I have an AbstractClass and multiple classes that inherit from it.
Check out this functional example in the TypeScript playground :
class AbstractGreeter {
static get(the_msg) {
let class_name = (<any>this).name;
// ... do other things here !
var instance = Object.create(window[class_name].prototype);
instance.constructor.apply(instance, [the_msg]);
return instance;
}
}
class Greeter extends AbstractGreeter {
greeting: string;
constructor(message: string) {
super();
this.greeting = message;
}
greet() {
return "Hello, " + this.greeting;
}
}
let second_greeter = Greeter.get("it's me");
let button = document.createElement('button');
button.textContent = "Say Hello";
button.onclick = function() {
alert(second_greeter.greet());
}
document.body.appendChild(button);
While this code functions smoothly in the playground, I am facing issues implementing it in my ionic2/angular2 application.
The problem arises when window[class_name] is undefined. Even when I tried using a hard-coded string, it didn't work as expected.
Any suggestions on how to rectify this issue?
Appreciate your help!
Julian