I need to dynamically start objects with JavaScript
Ex:
Sample class:
function sayhello() {
this.init = function() {
alert('Hello');
}
}
Function to load and instantiate classes dynamically:
function iniciaClasse(nomeDaClasse) {
return new nomeDaClasse();
}
Final (execution) code:
var variavelqualquer = iniciaClasse('sayhello');
The problem:
This is returning me an error, and I understand more or less why, since I pass a string in the parameter nomeDaClasse
, since I do not have the class constructor to be instantiated from immediate.
The function iniciaClasse()
is a loader module, and will load, start and return the class I am reporting, so when I call that function, the constructor ( sayhello
) does not yet exists.
Being so ...
If I do: iniciaClasse(sayHello)
- I get an error because sayHello()
does not exist yet
Is there any way to fix this?