javascript - "Expected 'this' to be used by class method" - Stack Overflow

I have an error from ESlint, but I don´t understand why. I have read these:Expected this to be used by

I have an error from ESlint, but I don´t understand why. I have read these:

  • Expected 'this' to be used by class method
  • Eslint : Expected 'this' to be used by class method
  • How can I fix 'warning Expected 'this' to be used by class method ' eslint error?

and this:

I still don't understanding what am I doing wrong.

My class

/* 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
}

I have an error from ESlint, but I don´t understand why. I have read these:

  • Expected 'this' to be used by class method
  • Eslint : Expected 'this' to be used by class method
  • How can I fix 'warning Expected 'this' to be used by class method ' eslint error?

and this:

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

I still don't understanding what am I doing wrong.

My class

/* 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
}

Error:
Expected 'this' to be used by class method 'obstacle

The obstacle method isn't settled on the full cases just on two of them.

Share Improve this question edited Nov 22, 2022 at 18:47 boj 11.4k5 gold badges41 silver badges58 bronze badges asked Feb 4, 2020 at 20:31 MarfaldoMarfaldo 531 gold badge1 silver badge5 bronze badges 3
  • @Ambrown Expected 'this' to be used by class method 'obstacle – Marfaldo Commented Feb 4, 2020 at 21:21
  • I guess you could have obstacle() and use this.x and this.y instead of asking a new position!? – B. Go Commented Feb 5, 2020 at 0:18
  • @B.Go Thanks. I thought the same, but when I used this.x the player does not detect the obstacle. On the other hand, I need to know the new position to avoid the obstacles. – Marfaldo Commented Feb 5, 2020 at 8:59
Add a ment  | 

1 Answer 1

Reset to default 5

The linter is plaining that a method that doesn't use the instance it was called on (this) shouldn't be an instance method in the first place. It's a bad practice.

You either

  • should make it a static method, to be called as Player.obstacle(x, y) (and probably be renamed to checkGlobalMapForObstacle)
  • should move the method to the Map class where it belongs, as it is checking coordinates against the map contents (this.mapGame[x][y]).

发布者:admin,转转请注明出处:http://www.yc00.com/questions/1744187433a4562274.html

相关推荐

发表回复

评论列表(0条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

工作时间:周一至周五,9:30-18:30,节假日休息

关注微信