优化这段代码 if (this.form.productType != 1) { this.form.orderStatus = ""; this.form.productName = ""; this.form.orderCode = ""; this.form.saleStartDate = ""; this.form.saleEndDate = ""; this.form.playStartDate = ""; this.form.playEndDate = ""; this.form.sessionCode = ""; this.form.showManagementName = ""; } else { this.form.orderStatus = ""; this.form.productName = ""; this.form.orderCode = ""; this.form.saleStartDate = ""; this.form.saleEndDate = ""; this.form.playStartDate = ""; this.form.playEndDate = ""; // this.sessionList =[]; } console.log(this.form, "form", this.sessionList); this.form.salesTime = "sales"; this.selectUsers = []; this.pagination.pageNum = 1; this.pagination.pageSize = 10
时间: 2024-03-01 22:51:22 浏览: 97
优化if/else测试代码包cheng.rar
可以使用对象解构来简化代码,同时可以将重复的部分提取出来,避免代码的重复:
```
const clearFields = {
orderStatus: "",
productName: "",
orderCode: "",
saleStartDate: "",
saleEndDate: "",
playStartDate: "",
playEndDate: "",
};
if (this.form.productType != 1) {
this.form = { ...this.form, ...clearFields, sessionCode: "", showManagementName: "" };
} else {
this.form = { ...this.form, ...clearFields, sessionList: [] };
}
console.log(this.form, "form", this.sessionList);
this.form.salesTime = "sales";
this.selectUsers = [];
this.pagination.pageNum = 1;
this.pagination.pageSize = 10;
```
这样可以使代码更简洁,易于维护。同时,使用对象解构还可以避免手动把每个字段赋值为空字符串,这样代码的可读性也会更好。
阅读全文