SyntaxError: getter and setter for private name #x should either be both static or non-static

Ngoại lệ JavaScript "mismatched placement" xảy ra khi một gettersetter riêng tư không khớp nhau về việc có phải là static hay không.

Thông báo

SyntaxError: Identifier '#x' has already been declared (V8-based)
SyntaxError: getter and setter for private name #x should either be both static or non-static (Firefox)
SyntaxError: Cannot declare a private non-static getter if there is a static private setter with used name. (Safari)

Loại lỗi

SyntaxError

Nguyên nhân?

Các gettersetter riêng tư có cùng tên phải đều là static, hoặc đều không phải static. Giới hạn này không tồn tại đối với các phương thức public.

Ví dụ

Vị trí không khớp

js
class Test {
  static set #foo(_) {}
  get #foo() {}
}

// SyntaxError: getter and setter for private name #foo should either be both static or non-static

fooriêng tư, các phương thức phải đều là static:

js
class Test {
  static set #foo(_) {}
  static get #foo() {}
}

hoặc đều không phải static:

js
class Test {
  set #foo(_) {}
  get #foo() {}
}

Xem thêm