"Expected 'this' to be used by class method"



我有一个来自 ESlint 的错误,但我不明白为什么。我读过这些:

  • 预计"this"将由类方法使用
  • Eslint : 期望"this"被类方法使用
  • 如何修复"类方法预期使用的警告'this'"eslint 错误?

而这个:

  • https://eslint.org/docs/rules/class-methods-use-this

我仍然不明白我做错了什么。

我的班级

/* eslint-disable no-plusplus */
/* eslint-disable no-undef */
class Player {
constructor(imagePlayer, name, score, positionY, positionX) {
this.imagePlayer = imagePlayer;
this.name = name;
this.score = score;
this.x = positionX;
this.y = positionY;
}
drawPlayer() {
app.map.mapGame[this.y][this.x] = this.imagePlayer;
}
obstacle(y, x) {
let colision = false;
if (app.map.mapGame[y][x] === 1) {
console.log("evaluación");
colision = true;
}
return colision;
}
lastPosition(oldPosition, direction) {
if (direction === left || direction === right) {
app.map.mapGame[this.y][this.x - oldPosition] = 0;
} else {
app.map.mapGame[this.y - oldPosition][this.x] = 0;
}
}
// movements players
movement(direction) {
switch (direction) {
case up:
if (this.y > 0) {
if (this.obstacle(this.y - 1, this.x) === false) {
this.y--;
this.lastPosition(-1, direction);
}
}
break;
case down:
if (this.y < 9) {
if (this.obstacle(this.y + 1, this.x) === false) {
this.y++;
this.lastPosition(+1, direction);
}
}
break;
case left:
if (this.x > 0) {
this.x--;
this.lastPosition(-1, direction);
}
break;
case right:
if (this.x < 14) {
this.x++;
this.lastPosition(+1, direction);
}
break;
default:
console.log("muro");
break;
}
} // movement
}

错误:
类方法"障碍"应使用"this">

障碍方法不仅仅针对其中两个案例解决了完整的情况。

linter抱怨不使用调用它的实例的方法(this(首先不应该是实例方法。这是一种不好的做法。

你要么

  • 应该使其成为static方法,称为Player.obstacle(x, y)(并可能重命名为checkGlobalMapForObstacle(
  • 应该将方法移动到它所属的Map类,因为它正在根据映射内容检查坐标 (this.mapGame[x][y](。

相关内容

最新更新