changeClickStatus() { //试下 this.clickStatus = !this.clickStatus; this.sliderIsDisabled = !this.sliderIsDisabled; if (!this.clickStatus == false) { this.headStatus = 3; //未选中状态 this.backDisabledBtn = true; this.forwardDisabledBtn = true; this.removeMoveNumber(); } else { this.headStatus = 1; if ( this.sliderValue <= this.sgfList.totalHead && this.sliderValue > 0 ) { this.backDisabledBtn = false; } else { this.backDisabledBtn = true; } if (this.sliderValue == this.sgfList.totalHead) { this.forwardDisabledBtn = true; } else { this.forwardDisabledBtn = false; } let board = this.player.board; for (let i = 1; i < this.nodes.length; i++) { let item = this.nodes[i]; board.removeObject({ type: "LB", text: i, x: item.x, y: item.y, }); } } //开启编辑功能 this._editable = this._editable || new WGo.Player.Editable(this.player, this.player.board); this._editable.set(!this._editable.editMode); },怎么优化这段代码
时间: 2023-12-03 16:02:25 浏览: 122
这段代码可以进行如下优化:
1. 将 `!this.clickStatus == false` 改为 `this.clickStatus`,以提高代码的可读性。
2. 将 `this.headStatus` 的赋值移到 `if` 语句块中,避免重复赋值。
3. 将 `this.sliderValue <= this.sgfList.totalHead && this.sliderValue > 0` 提取出来,避免重复计算。
4. 将 `let board = this.player.board` 的赋值提取出来,避免重复获取。
优化后的代码如下:
```
changeClickStatus() {
this.clickStatus = !this.clickStatus;
this.sliderIsDisabled = !this.sliderIsDisabled;
if (this.clickStatus) {
this.headStatus = 3; //未选中状态
this.backDisabledBtn = true;
this.forwardDisabledBtn = true;
this.removeMoveNumber();
} else {
const sliderInRange = this.sliderValue > 0 && this.sliderValue <= this.sgfList.totalHead;
this.headStatus = 1;
this.backDisabledBtn = !sliderInRange;
this.forwardDisabledBtn = this.sliderValue === this.sgfList.totalHead;
const board = this.player.board;
for (let i = 1; i < this.nodes.length; i++) {
const item = this.nodes[i];
board.removeObject({
type: "LB",
text: i,
x: item.x,
y: item.y,
});
}
}
//开启编辑功能
this._editable = this._editable || new WGo.Player.Editable(this.player, this.player.board);
this._editable.set(!this._editable.editMode);
}
```
阅读全文