当使用#
定义一个私有(private)成员,然后重新定义某个使用这个私有(private)成员的成员时,你会发现不能再使用了:
class Foo {
#secret = "Keyboard Cat";
method() {
console.log(this.#secret);
}
}
const target = Foo.prototype;
const key = "method";
const desc = Object.getOwnPropertyDescriptor(target, key);
const og = desc.value;
desc.value = function (...args) {
return og.apply(target, args);
};
Object.defineProperty(target, key, desc);
new Foo().method();
为什么?在这种情况下,我所做的就是围绕原始方法。请注意,此示例是在 TypeScript 中使用装饰器的显着简化。我怎样才能解决这个问题,同时仍然能够重新定义和“更改”方法?Uncaught TypeError: Cannot read private member #secret from an object whose class did not declare it
这是同样的事情,但使用了 TypeScript 装饰器:
const Curse: MethodDecorator = (target, _, desc) => {
const og = desc.value as Function;
desc.value = function (...args: any[]) {
return og.apply(target, args);
} as any;
return desc;
};
class Foo {
#secret = "Keyboard Cat";
@Curse
method() {
console.log(this.#secret);
}
}
new Foo().method();
最佳答案
您犯的错误是将方法应用于目标
,即Foo.prototype
,而不是通过以下方式应用于new Foo
实例this
关键字:
class Foo {
#secret = "Keyboard Cat";
method() {
console.log(this.#secret);
}
}
const target = Foo.prototype;
const key = "method";
const desc = Object.getOwnPropertyDescriptor(target, key);
const orig = desc.value;
desc.value = function (...args) {
return orig.apply(this, args);
// ^^^^
};
Object.defineProperty(target, key, desc);
new Foo().method();
Foo.prototype
没有 #secret
私有(private)字段。你会得到同样的错误
class Foo {
#secret = "Keyboard Cat";
method() {
console.log(this.#secret);
}
}
Foo.prototype.method();
关于javascript - 重新定义类的方法时无法访问私有(private)成员,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/74010006/