如何改进我的 Javascript Build Config 类
How can i improve my Javascript Build Config Class?
我对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:
}
};
};
这看起来还行吗?可以进行任何改进吗?
谢谢
你的方法还可以。下面的代码略短:
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');
似乎你重复自己太多了,在所有情况下(除了默认值),你都有这些具有相同的值:
...
this.server = 'http://someurl';
this.nfc = true;
this.barcode = true;
this.scheduler = true;
...
我建议这样做:
- 创建一个标志 var 并使用它应用更改
- 或者,自动执行更改并还原
case default
相关文章:
- What does requirejs.config() do?
- 使用Phonegap Build后,谷歌地图将不会显示在手机上
- 从Middleman中的Javascript访问config.rb
- 使用AddThis-config-data_ga_property向两个Google Analytics配置文件报告
- JS vs JSON for config in Node js
- Web.config: Values for ClientValidationEnabled and Unobtrusi
- Dojo Build System - 使用带有谷歌地图API的异步插件
- EmberJS config prod img src url
- 在Require.js中,如果Require()是一个函数,那么Require.config()是如何存在的
- 在Asp.net的空网站中,如何改变"Web.config”;使内容(.js)文件显示在浏览器中,而不是下载
- Can't get Sublime Javascript Build to work
- RequireJS Config映射整个目录
- 我没有'我不了解require.js是如何处理加载路径的.每次定义模块时是否需要使用require.config
- Cordova InAppBrowser无法通过build.phonegap.com运行
- 页面更改无法在android上使用Phonegap Build的JqueryMobile中工作
- 找不到模块 './build/Release/bson'] 代码: 'MODULE_NOT_FOUND' } js-bs
- 有角度的“;未知提供者”;在约曼应用程序中使用Grunt Build缩小后出错
- Dojo Build-copyOnly在概要文件中不起作用
- 如何改进我的 Javascript Build Config 类
- PhoneGap:如何在Config.xml中为iOS设置Build和Version属性