TypeErrorCannot read private member #x from an object whose class did not declare it
يعني ايه
قريت حقل خاص (#x) من object مش اتعمل من الـclass اللي معرّف الحقل ده.
ليه بيحصل
- مرّرت object عادي بدل instance من الـclass.
إزاي تصلّحه
- مرّر instance حقيقي، أو افحص
#x in oقبل القراءة.
جرّبناه فعلًا
class A {
#x = 1;
static read(o) { return o.#x; }
}
A.read({});class A {
#x = 1;
static read(o) { return o.#x; }
}
console.log(A.read(new A()));1