优化代码 if (e.target.matches) { if ( this.istopSearch && this.istopSearch != '' && !e.target.matches('#my-km-popper') && this.zhName === '综合' && !e.target.matches('#ywbd-km-popper') ) { this.istopSearch = false this.isHistyqry = false } } else { if ( this.istopSearch && this.istopSearch != 'g' && !e.target.msMatchesSelector('#my-km-popper') && this.zhName === '综合' && !e.target.msMatchesSelector('#ywbd-km-popper') ) { this.istopSearch = false this.isHistyqry = false } }
时间: 2024-03-05 14:53:46 浏览: 94
可以将重复的判断条件提取出来,避免重复代码。同时可以使用三元运算符来简化代码。重构后的代码如下:
```
const isTopSearch = this.istopSearch && this.istopSearch !== '';
const isZhName = this.zhName === '综合';
const isMyKmPopper = e.target.matches('#my-km-popper') || e.target.msMatchesSelector('#my-km-popper');
const isYwbdKmPopper = e.target.matches('#ywbd-km-popper') || e.target.msMatchesSelector('#ywbd-km-popper');
if (e.target.matches ? (!e.target.matches('#my-km-popper') && isTopSearch && isZhName && !isYwbdKmPopper) : (!isMyKmPopper && isTopSearch && isZhName && !isYwbdKmPopper)) {
this.istopSearch = false;
this.isHistyqry = false;
}
```
阅读全文