我有以下一段完美运行的代码。
但是,我下面的项目列表将增长为数十个对象,我想知道是否可以删除 switch 子句并拥有更小和更少的代码行。
“type”参数是数组、视口、容器、网格面板等的类型,“component”参数是数组中的对象本身。
viewport: {},
container: {},
gridpanel: {},
panel: {},
treepanel: {},
window: {},
button: {},
add: function (component, type) {
switch (component.getType() != undefined ? component.getType() : type) {
case 'container':
this.container[component.getId()] = component;
break;
case 'gridpanel':
this.gridpanel[component.getId()] = component;
break;
case 'panel':
this.panel[component.getId()] = component;
break;
case 'treepanel':
this.treepanel[component.getId()] = component;
break;
case 'viewport':
this.viewport[component.getId()] = component;
break;
case 'window':
this.window[component.getId()] = component;
break;
case 'button':
this.button[component.getId()] = component;
break;
default:
break;
}
},