简体   繁体   中英

How can i improve my Javascript Build Config Class?

I am not super good with JavaScript. I am trying to write a build config class that can be used to set settings on the fly. The idea is to pass in the environment that it is to be run in and then the variables set up properly. I have the following:

function BuildConfig(){  
    this.build = 'html5';
    this.server = 'http://someurl',
    this.nfc = true,
    this.barcode = true,
    this.scheduler = true,
    this.getConfig = function(buildType){  

     switch(buildType)
        {
        case "ios":
            this.build = 'ios';
            this.server = 'http://someurl';
            this.nfc = true;
            this.barcode = false;
            this.scheduler = false;
            break;
        case "android":
            this.build = 'android';
            this.server = 'http://someurl';
            this.nfc = false;
            this.barcode = true;
            this.scheduler = false;
            break;
        case "websiteanonymous":
            this.build = 'websiteanonymous';
            this.server = 'http://someurl';
            this.nfc = true;
            this.barcode = false;
            this.scheduler = true;
            break;
        case "website":
            this.build = 'website';
            this.server = 'http://someurl';
            this.nfc = true;
            this.barcode = true;
            this.scheduler = false;
            break;

        default:

        }

    };  
}; 

Does this look OK? Can any improvements be made?

Thanks

Your approach is ok. And the below code is slightly shorter:

function BuildConfig(type) {
    // Defaults
    this.build = 'html5';
    this.server = 'http://someurl';
    this.nfc = true;
    this.barcode = false;
    this.scheduler = false;

    switch (type) {
        case "ios":
            this.build = 'ios';
            this.scheduler = true;
            break;

        case "android":
            this.build = 'android';
            this.server = 'http://anotherurl';
            this.nfc = false;
            break;

        case "websiteanonymous":
            this.build = 'websiteanonymous';
            this.server = 'http://otherurl';
            this.barcode = true;
            break;

        case "website":
            this.build = 'website';
            break;
    }
}

var config = new BuildConfig('android');

It seems you are repeating yourself too much, in all your cases (except default ) you have these with the same values:

        ...
        this.server = 'http://someurl';
        this.nfc = true;
        this.barcode = true;
        this.scheduler = true;
        ...

I recommend to do:

  • create a flag var and apply the changes using it
  • or, do the change automatically and revert in case default

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM