1

Here, i am implementing an GeoMap asset which has all the countries. in this, all countries has their own class such as BTNMongolia, BTNHungary etc. i have only countries name to get object and based on countries name, i want to get object of the country's class. i'm doing like this.

function countryObject(country) {
    switch (country) {
        case 'mongolia':
            return new BTNMongolia();
        case 'mongolia':
            return new BTNHungary();
        case 'mongolia':
            return new BTNComoros();
        case 'mongolia':
            return new BTNAntiguaandBarbuda();
        case 'mongolia':
            return new BTNSouthKorea();
        case 'mongolia':
            return new BTNAustralia();
        case 'mongolia':
            return new BTNTajikistan();
        default:
            return null;
    }
}

here, to get object, i want to get object of the country'class from the country'name itself. is there is a short way to do that?.

4 Answers4

3

The main idea as below, you will need to check whether it exist to return null :

return new window["BTN" + country.capitalize()]();

If your class under a namespace, you may do it this way:

return new myspaces["BTN" + country.capitalize()]();

As for string capitalize you can refer this post.

Community
  • 1
  • 1
Wenbing Li
  • 12,289
  • 1
  • 29
  • 41
2

This could be a solution. You can use a dictionary like bellow.

function countryObject(country){
    var countryObject = {
        mongolia:  BTNMongolia,
        hungary:   BTNHungary,
        comoros:   BTNComoros,
        australia: BTNAustralia
        // ...
    };
    return new countryObject[country]();
}

I've not specified the full code. So please extend it.

Emissary
  • 9,954
  • 8
  • 54
  • 65
Mritunjay
  • 25,338
  • 7
  • 55
  • 68
  • 2
    You don't need to instantiate ***every*** one each time the factory function is called - just reference the constructors and `return new countryObject[country];` – Emissary Aug 08 '14 at 11:51
  • that doesnt really answer his question, its exactly the same as switch-case logic, he still needs to map everything while he asks how to do it blind without mapping... – Banana Aug 08 '14 at 11:51
  • @Emissary thanx for the catch, updated my answer now it won't create every time. – Mritunjay Aug 08 '14 at 11:55
  • 1
    @Emissary ohhh I got it, What I was thinking they both are same, but when you used the terms then I realized, many many thanx. – Mritunjay Aug 08 '14 at 12:02
0

I believe this questions was already answered here Get object class from string name in javascript. Basically don't use eval, store the list of classes in a map and then look them up in the map.

Community
  • 1
  • 1
Eric Maibach
  • 328
  • 3
  • 14
-2

You could try something like return eval('new BTN'+country+'()'). That doesn't seem like a good use of eval, but I can't think of another way of doing it without explicitly listing all the possible classes.

icke
  • 1,568
  • 1
  • 19
  • 31