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?.

3
  • 5
    amazing each case is same, what really do you want??? Commented Aug 8, 2014 at 11:44
  • it's result of copy paste. Commented Aug 8, 2014 at 11:44
  • 1
    possible duplicate of Get object class from string name in javascript Commented Aug 8, 2014 at 11:56

4 Answers 4

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.

Sign up to request clarification or add additional context in comments.

1 Comment

Be warned, this doesn't work if your constructors are in a namespace other than window
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.

4 Comments

You don't need to instantiate every one each time the factory function is called - just reference the constructors and return new countryObject[country];
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...
@Emissary thanx for the catch, updated my answer now it won't create every time.
@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.
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.

Comments

-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.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.