我無法弄清楚如何綁定類訪問器裝飾器中的實例化類上下文。我簡而言之類:訪問器裝飾器和實例化類上下文
class A {
protected persistProperty(a, b) {
// ...
}
}
class C extends A {
@validatorTest
public set coolStuff(email: string) {
this.persistProperty('company.email', email);
}
}
的裝飾:
let foo = new C();
c.coolStuff = '[email protected]';
我收到以下錯誤:
function validatorTest(
target: any,
propertyKey: string,
descriptor: TypedPropertyDescriptor<string>
) {
const oldSet = descriptor.set;
descriptor.set = (value: string) => {
if (value && !value.match(...)) {
throw new Error();
}
oldSet(value);
}
}
現在,當我使用存取問題出現
TypeError: Cannot read property 'persistProperty' of undefined
所以,正如我所說的,它是eems instanced類的上下文不綁定到裝飾器。我在這裏做錯了什麼?
部分,回答我自己的問題:) – nhaa123
謝謝你,謝謝你,謝謝你。 – JonathanPeel
@madara,在這個例子中,有一個簡單的方法來_forward_參數。– JonathanPeel