Javascript 重构。处理重复代码
Javascript refactoring. Dealing with repeat code
我有以下代码:
switch(equipmentAttachment.AttachmentPosition)
{
case 'AttachFront':
{
if(equipmentAttachment.ProductCategoryDesc!='')
{
attachments.frontAttachment=equipmentAttachment.ProductCategoryDesc;
}
else
{
attachments.frontAttachment=equipmentAttachment.ProductCategoryName;
}
break;
}
case 'AttachRear':
{
if(equipmentAttachment.ProductCategoryDesc!='')
{
attachments.backAttachment=equipmentAttachment.ProductCategoryDesc;
}
else
{
attachments.backAttachment=equipmentAttachment.ProductCategoryName;
}
break;
}
case 'Tertiary':
{
if(equipmentAttachment.ProductCategoryDesc!='')
{
attachments.thirdAttachment=equipmentAttachment.ProductCategoryDesc;
}
else
{
attachments.thirdAttachment=equipmentAttachment.ProductCategoryName;
}
break;
}
}
return attachments;
请注意,大部分代码都是重复接受对象中不同属性的设置 attachments
。无论如何摆脱重复的代码?还是它就是这样?
var posMap = {
"AttachFront": "frontAttachment",
"AttachRear": "backAttachment",
"Tertiary": "thirdAttachment"
};
if(posMap[equipmentAttach.AttachmentPosition])
{
var target = posMap[equipmentAttach.AttachmentPosition];
attachments[target] = (equipmentAttachment.ProductCategoryDesc || equipmentAttachment.ProductCategoryName);
}
return attachments;
更新:稍微简洁一点:
var target = {
"AttachFront": "frontAttachment",
"AttachRear": "backAttachment",
"Tertiary": "thirdAttachment"
}[equipmentAttach.AttachmentPosition];
if(target)
{
attachments[target] = (equipmentAttachment.ProductCategoryDesc || equipmentAttachment.ProductCategoryName);
}
return attachments;
我有以下代码:
switch(equipmentAttachment.AttachmentPosition)
{
case 'AttachFront':
{
if(equipmentAttachment.ProductCategoryDesc!='')
{
attachments.frontAttachment=equipmentAttachment.ProductCategoryDesc;
}
else
{
attachments.frontAttachment=equipmentAttachment.ProductCategoryName;
}
break;
}
case 'AttachRear':
{
if(equipmentAttachment.ProductCategoryDesc!='')
{
attachments.backAttachment=equipmentAttachment.ProductCategoryDesc;
}
else
{
attachments.backAttachment=equipmentAttachment.ProductCategoryName;
}
break;
}
case 'Tertiary':
{
if(equipmentAttachment.ProductCategoryDesc!='')
{
attachments.thirdAttachment=equipmentAttachment.ProductCategoryDesc;
}
else
{
attachments.thirdAttachment=equipmentAttachment.ProductCategoryName;
}
break;
}
}
return attachments;
请注意,大部分代码都是重复接受对象中不同属性的设置 attachments
。无论如何摆脱重复的代码?还是它就是这样?
var posMap = {
"AttachFront": "frontAttachment",
"AttachRear": "backAttachment",
"Tertiary": "thirdAttachment"
};
if(posMap[equipmentAttach.AttachmentPosition])
{
var target = posMap[equipmentAttach.AttachmentPosition];
attachments[target] = (equipmentAttachment.ProductCategoryDesc || equipmentAttachment.ProductCategoryName);
}
return attachments;
更新:稍微简洁一点:
var target = {
"AttachFront": "frontAttachment",
"AttachRear": "backAttachment",
"Tertiary": "thirdAttachment"
}[equipmentAttach.AttachmentPosition];
if(target)
{
attachments[target] = (equipmentAttachment.ProductCategoryDesc || equipmentAttachment.ProductCategoryName);
}
return attachments;