Movatterモバイル変換


[0]ホーム

URL:


MDN Web Docs

TypeError: can't access/set private field or method: object is not the right class

The JavaScript exception "can't access private field or method: object is not the right class" or "can't set private field: object is not the right class" occurs when a private field or method is get or set on an object that does not have thisprivate element defined.

Message

TypeError: Cannot read private member #x from an object whose class did not declare it (V8-based)TypeError: Cannot write private member #x to an object whose class did not declare it (V8-based)TypeError: can't access private field or method: object is not the right class (Firefox)TypeError: can't set private field: object is not the right class (Firefox)TypeError: Cannot access invalid private field (evaluating 'this.#x') (Safari)

Error type

What went wrong?

You are trying to get or set a private field or method on an object, but that object does not contain this private element. Private instance properties can only be accessed on instances of the class (including its subclasses) that declares them; private static properties can only be accessed on the class itself that declares them, and not on subclasses.

This error occurs when the private name exists in the class scope but the object it's accessed on is invalid, If the private name does not exist, you will get asyntax error instead.

Examples

Mismatched static/instance fields

You may have declared the field as a static field, but are trying to access it on an instance, or vice versa.

js
class MyClass {  static #x = 0;  doSomething() {    console.log(this.#x);  }}const obj = new MyClass();obj.doSomething();// TypeError: can't access private field: object is not the right class

To fix this, either change the field to be an instance field, or access the field on the class itself, or declare another field on the instance. Note that the private namespace is shared between static and instance properties, so you cannot have a static and instance private element with the same name.

js
class MyClass {  #x = 0;  doSomething() {    console.log(this.#x);  }}class MyClass2 {  static #x = 0;  doSomething() {    console.log(MyClass2.#x);  }}

Wrong object used

Perhaps you have a method that accessthis.#x, but it is called with anotherthis value.

js
class JSONReplacer {  #count = 0;  func(key, value) {    if (typeof value === "object") {      this.#count++;    }    return value;  }}JSON.stringify({ a: 1, b: { c: 2 } }, new JSONReplacer().func);// TypeError: can't access private field: object is not the right class

This is becauseJSON.stringify() calls the replacer function with the object containingvalue asthis, so the private field is not accessible. To fix this, you can bind the method to the object, or use an arrow function, to ensure thatreplacer.func is called with the correctthis value.

js
const replacer = new JSONReplacer();JSON.stringify({ a: 1, b: { c: 2 } }, replacer.func.bind(replacer));JSON.stringify({ a: 1, b: { c: 2 } }, (...args) => replacer.func(...args));

Most of the time, if you accidentally unbound a method, the method would be called withundefined asthis, which would result in a different error (TypeError: can't convert undefined to object). This error only occurs when the method is called with a different object asthis, either by usingcall() orapply(), or by passing the method as a callback to a function that calls it with a differentthis value.

If you don't know for sure that the object will contain the private element, as in the following code:

js
class MyClass {  #x = 0;  static doSomething(obj) {    console.log(obj.#x); // Throws if obj is not an instance of MyClass  }}

You can use thein operator to perform abranded check first.

js
class MyClass {  #x = 0;  static doSomething(obj) {    if (!(#x in obj)) {      return;    }    console.log(obj.#x);  }}

Accessing static elements on subclasses

If you have aprivate static property, you can only access it on the class that declares it, not on subclasses.

js
class MyClass {  static #x = 0;  doSomething() {    console.log(this.#x);  }}class MySubClass extends MyClass {}MySubClass.doSomething();// TypeError: can't access private field: object is not the right class

To fix this, never access private static properties throughthis. Instead, always explicitly specify the class's name.

js
class MyClass {  static #x = 0;  doSomething() {    console.log(MyClass.#x);  }}

Accessing same-name private elements on another class

Unlike normal string or symbol properties, private names are not shared between classes. If you have a private element with the same name in two classes, they are still not the same element, and you cannot access one class's private element from another class.

js
class MyClass {  #x = 0;}class MyOtherClass {  #x = 1;  doSomething(o) {    console.log(o.#x);  }}const obj = new MyClass();new MyOtherClass().doSomething(obj);// TypeError: can't access private field: object is not the right class

Adding private elements to unrelated objects

You cannot dynamicallyadd private elements to unrelated objects.

js
class MyClass {  #x = 0;  static stamp(obj) {    obj.#x = 1;  }}MyClass.stamp({});// TypeError: can't set private field: object is not the right class

If you really want to do this, consider thereturn override trick. However, in general, you would probably want to use aWeakMap instead.

js
class MyClass {  static #objToX = new WeakMap();  static stamp(obj) {    MyClass.#objToX.set(obj, 1);  }}MyClass.stamp({});

See also

Help improve MDN

Learn how to contribute.

This page was last modified on byMDN contributors.


[8]ページ先頭

©2009-2025 Movatter.jp