Skip to content
← Back to rules

unicorn/no-array-method-this-argument 스타일

An auto-fix is available for this rule.

동작 방식

map, filter, some, every 및 유사한 배열 반복 메서드에서 thisArg 매개변수 사용을 금지합니다.

왜 좋지 않은가?

thisArg 매개변수는 코드를 더 이해하기 어렵고 추론하기 어려워집니다. 대신 화살표 함수를 사용하거나 명시적으로 바인딩하는 더 명확한 방식을 선호하세요. 화살표 함수는 렉시컬 스코프로부터 this를 상속하므로, 더 직관적이고 오류 발생 가능성이 낮습니다.

예시

이 규칙에 부적절한 코드 예시:

js
array.map(function (x) {
  return x + this.y;
}, this);
array.filter(function (x) {
  return x !== this.value;
}, this);

이 규칙에 적절한 코드 예시:

js
array.map((x) => x + this.y);
array.filter((x) => x !== this.value);
const self = this;
array.map(function (x) {
  return x + self.y;
});

사용 방법

구성 파일 또는 CLI를 통해 이 규칙을 활성화하려면 다음을 사용할 수 있습니다:

json
{
  "rules": {
    "unicorn/no-array-method-this-argument": "error"
  }
}
bash
oxlint --deny unicorn/no-array-method-this-argument

참고 자료