2012-05-26 28 views
1

我對JavaScript並不擅長。我正在嘗試編寫一個可用於即時設置設置的構建配置類。這個想法是通過它要運行的環境,然後正確設置變量。我有以下幾種:我該如何改進我的Javascript構建配置類?

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: 

     } 

    }; 
}; 

這看起來好嗎?可以進行任何改進嗎?

感謝

+0

您可能想要在http://codereview.stackexchange.com/中發佈此內容 – ajax333221

回答

1

您的方法是好的。並且下面的代碼略短:

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'); 
0

您似乎正在重複自己太多,在所有的情況下(除了默認)你有這些具有相同的價值觀:

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

我建議做:

  • 創建一個標誌var並應用使用它的更改
  • 或者,更改auto在case default
+0

對不起,這些值有意改變。我只是展示了總體結構。 – jini