1

Is there a way in javascript to create an object from a string?

Here is an example :

configuation object:

var formatter = {
    telephone : {
        length : 10,
        formatClass : "telephoneFormatter"
        },
    email : {
        length : 255,
        formatClass : "emailFormatter"
    }
}

In the fields creation I could use the following method :

function createFormatter(element){
    if(formatter[element].formatClass == "telephoneFormatter"){
        var formatObj = new telephoneFormatter()
    }
    if(formatter[element].formatClass == "emailFormatter"){
        var formatObj = new emailFormatter()
    }
    return formatObj;
}

But I would like to create a the object dynamically, something like

function createFormatter(element){
    return new formatter[element].formatClass();
}

The constructors are not available as properties of the window object, as presented in the solution to "Dynamic Instantiation In JavaScript". The class files are loaded with the page but I cannot find the object developer's tool in chrome. Thus I do not have a handle currently on the classes.

Why do I need to do that ? The application loads form dynamically, and the field are created from an elaborate JSON. While the form is beeing created, the validation is added depending on the structure of the JSON. We never know what validation must be added to a certain field. To complexify the whole thing the validation is different on the locale.

Can that be done?

Community
  • 1
  • 1
David Laberge
  • 15,435
  • 14
  • 53
  • 83
  • 3
    Just have a map of constructor functions: `var constructors = {telephoneFormatter: telephoneFormatter, ...};` and later: `new constructors[element.formatClass]()`. That's the same as in the linked question, only that you have to create the mapping explicitly. – Felix Kling Aug 29 '14 at 18:36
  • Good idea, but the project is build a bit more complex then the example above. The different class are created in different files ... :( – David Laberge Aug 29 '14 at 18:42
  • 1
    Sounds like a use case for AMD. – Shmiddty Aug 29 '14 at 18:43
  • You can have a simply global registry where classes can register themselves. This can be as simply as `Registry.name = constructor;` or `Register.setClass(name, constructor);` or you can go ahead and use a proper module system. – Felix Kling Aug 29 '14 at 18:43
  • @Shmiddty AMD like require.js? – David Laberge Aug 29 '14 at 18:45
  • @DavidLaberge Yes, exactly. – Shmiddty Aug 29 '14 at 18:47
  • How, exactly, does the other Q&A not apply? Please edit clarification into your question. – outis Aug 29 '14 at 19:01
  • @outis I edit the question, hope it is more clear now. Good comment by the way. – David Laberge Aug 29 '14 at 19:07
  • @DavidLaberge: Sorry, but the situation still isn't clear. Part of my confusion is the language; I've rewritten the "what" part to be clearer, but the "why" doesn't seem to preclude the other solution (the browser's developer tools won't affect whether or not a function is global). Are the constructors declared locally in some other function? Do you not have write access to the code that declares the constructors? It might be best to work out the clarification in [chat](http://chat.stackoverflow.com/rooms/42482/web-developers). – outis Aug 29 '14 at 19:22

1 Answers1

0

you can create a custom factory function which checks the configurationObjects formaterClass and than initializes a class of that type. than pass the configurations object as constructor parameter to initialize the object.

register the available classes so the factory function is not a multi condition block.

var formatterClasses = {
    'telephoneFormatter': telephoneFormatter,
    'emailFormatter': emailFormatter
}

function formatterFactory(configurationObject)
{
    return new formatterClasses[configurationObject.formatClass](configurationObject);
}
Votto
  • 71
  • 5